From 9c04f5bafe93cfae3cd1af98781a35a9e5221b79 Mon Sep 17 00:00:00 2001 From: Jake Herrmann Date: Wed, 10 May 2023 15:01:31 -0800 Subject: [PATCH 1/6] Add job_urls field to post_jobs response --- README.md | 4 ++-- apps/api/api-cf.yml.j2 | 1 + apps/api/src/hyp3_api/handlers.py | 5 +++++ tests/cfg.env | 1 + 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 13ed88bd2..73a984d9e 100644 --- a/README.md +++ b/README.md @@ -114,8 +114,8 @@ The API can be run locally to verify changes, but must be connected to a set of 1. Set up AWS credentials in your environment as described [here](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#configuration). Also see our [wiki page](https://github.com/ASFHyP3/.github-private/wiki/AWS-Access#aws-access-keys). -2. Edit `tests/cfg.env` to specify the names of existing DynamoDB tables from a particular HyP3 deployment. - Delete all of the `AWS_*` variables. +2. Edit `tests/cfg.env` to specify the names of existing DynamoDB tables from a particular HyP3 deployment, + as well as the corresponding API domain name. Delete all of the `AWS_*` variables. 3. Run the API (replace `` with the AWS config profile that corresponds to the HyP3 deployment): ```sh AWS_PROFILE= make run diff --git a/apps/api/api-cf.yml.j2 b/apps/api/api-cf.yml.j2 index 6af66f9d3..e3ff81caa 100644 --- a/apps/api/api-cf.yml.j2 +++ b/apps/api/api-cf.yml.j2 @@ -189,6 +189,7 @@ Resources: Type: AWS::Lambda::Function Properties: Environment: + # TODO add domain name as env var Variables: JOBS_TABLE_NAME: !Ref JobsTable USERS_TABLE_NAME: !Ref UsersTable diff --git a/apps/api/src/hyp3_api/handlers.py b/apps/api/src/hyp3_api/handlers.py index 6d8a7fd28..a20698d61 100644 --- a/apps/api/src/hyp3_api/handlers.py +++ b/apps/api/src/hyp3_api/handlers.py @@ -1,3 +1,4 @@ +import os from http.client import responses from uuid import UUID @@ -31,6 +32,8 @@ def is_uuid(val): return True +# TODO add or update unit test(s) +# TODO update response schema def post_jobs(body, user): print(body) @@ -46,6 +49,8 @@ def post_jobs(body, user): body['jobs'] = dynamo.jobs.put_jobs(user, body['jobs']) except dynamo.jobs.QuotaError as e: abort(problem_format(400, str(e))) + domain_name = os.environ['DOMAIN_NAME'] + body['job_urls'] = [f'{domain_name}/jobs/{job["job_id"]}' for job in body['jobs']] return body diff --git a/tests/cfg.env b/tests/cfg.env index c85cff093..24c9e5e11 100644 --- a/tests/cfg.env +++ b/tests/cfg.env @@ -1,4 +1,5 @@ FLASK_DEBUG=true +DOMAIN_NAME=hyp3-domain-name JOBS_TABLE_NAME=hyp3-db-table-job USERS_TABLE_NAME=hyp3-db-table-user SUBSCRIPTIONS_TABLE_NAME=hyp3-db-table-subscriptions From 6cff42efa0f869a68d924615505142468ef49caf Mon Sep 17 00:00:00 2001 From: Jake Herrmann Date: Wed, 10 May 2023 15:41:52 -0800 Subject: [PATCH 2/6] get base url directly --- README.md | 4 ++-- apps/api/api-cf.yml.j2 | 1 - apps/api/src/hyp3_api/handlers.py | 10 ++++++++-- tests/cfg.env | 1 - 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 73a984d9e..13ed88bd2 100644 --- a/README.md +++ b/README.md @@ -114,8 +114,8 @@ The API can be run locally to verify changes, but must be connected to a set of 1. Set up AWS credentials in your environment as described [here](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#configuration). Also see our [wiki page](https://github.com/ASFHyP3/.github-private/wiki/AWS-Access#aws-access-keys). -2. Edit `tests/cfg.env` to specify the names of existing DynamoDB tables from a particular HyP3 deployment, - as well as the corresponding API domain name. Delete all of the `AWS_*` variables. +2. Edit `tests/cfg.env` to specify the names of existing DynamoDB tables from a particular HyP3 deployment. + Delete all of the `AWS_*` variables. 3. Run the API (replace `` with the AWS config profile that corresponds to the HyP3 deployment): ```sh AWS_PROFILE= make run diff --git a/apps/api/api-cf.yml.j2 b/apps/api/api-cf.yml.j2 index e3ff81caa..6af66f9d3 100644 --- a/apps/api/api-cf.yml.j2 +++ b/apps/api/api-cf.yml.j2 @@ -189,7 +189,6 @@ Resources: Type: AWS::Lambda::Function Properties: Environment: - # TODO add domain name as env var Variables: JOBS_TABLE_NAME: !Ref JobsTable USERS_TABLE_NAME: !Ref UsersTable diff --git a/apps/api/src/hyp3_api/handlers.py b/apps/api/src/hyp3_api/handlers.py index a20698d61..b6a72976a 100644 --- a/apps/api/src/hyp3_api/handlers.py +++ b/apps/api/src/hyp3_api/handlers.py @@ -1,4 +1,5 @@ import os +import urllib.parse from http.client import responses from uuid import UUID @@ -32,6 +33,11 @@ def is_uuid(val): return True +def base_url() -> str: + parts = urllib.parse.urlparse(request.base_url) + return urllib.parse.urlunparse((parts.scheme, parts.netloc, '', '', '', '')) + + # TODO add or update unit test(s) # TODO update response schema def post_jobs(body, user): @@ -49,8 +55,8 @@ def post_jobs(body, user): body['jobs'] = dynamo.jobs.put_jobs(user, body['jobs']) except dynamo.jobs.QuotaError as e: abort(problem_format(400, str(e))) - domain_name = os.environ['DOMAIN_NAME'] - body['job_urls'] = [f'{domain_name}/jobs/{job["job_id"]}' for job in body['jobs']] + base = base_url() + body['job_urls'] = [f'{base}/jobs/{job["job_id"]}' for job in body['jobs']] return body diff --git a/tests/cfg.env b/tests/cfg.env index 24c9e5e11..c85cff093 100644 --- a/tests/cfg.env +++ b/tests/cfg.env @@ -1,5 +1,4 @@ FLASK_DEBUG=true -DOMAIN_NAME=hyp3-domain-name JOBS_TABLE_NAME=hyp3-db-table-job USERS_TABLE_NAME=hyp3-db-table-user SUBSCRIPTIONS_TABLE_NAME=hyp3-db-table-subscriptions From cfc95fbe372e48bee8c10665ba33dd51a8c41597 Mon Sep 17 00:00:00 2001 From: Jake Herrmann Date: Wed, 10 May 2023 15:45:56 -0800 Subject: [PATCH 3/6] remove unused import --- apps/api/src/hyp3_api/handlers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/api/src/hyp3_api/handlers.py b/apps/api/src/hyp3_api/handlers.py index b6a72976a..67d9c2d5d 100644 --- a/apps/api/src/hyp3_api/handlers.py +++ b/apps/api/src/hyp3_api/handlers.py @@ -1,4 +1,3 @@ -import os import urllib.parse from http.client import responses from uuid import UUID From dc76eb6da36aec832cf4cb1a99825aa8bb7eda4e Mon Sep 17 00:00:00 2001 From: Jake Herrmann Date: Wed, 10 May 2023 15:52:07 -0800 Subject: [PATCH 4/6] add todo --- apps/api/src/hyp3_api/handlers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/api/src/hyp3_api/handlers.py b/apps/api/src/hyp3_api/handlers.py index 67d9c2d5d..93a1d6105 100644 --- a/apps/api/src/hyp3_api/handlers.py +++ b/apps/api/src/hyp3_api/handlers.py @@ -59,6 +59,7 @@ def post_jobs(body, user): return body +# TODO add job_urls field def get_jobs(user, start=None, end=None, status_code=None, name=None, job_type=None, start_token=None, subscription_id=None): try: From 600419eb2d8a9a3351cae86a52580c70de3ecf49 Mon Sep 17 00:00:00 2001 From: Jake Herrmann Date: Wed, 10 May 2023 15:56:27 -0800 Subject: [PATCH 5/6] update jobs_response schema --- apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 | 4 ++++ apps/api/src/hyp3_api/handlers.py | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 b/apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 index 65453940b..e4000c1df 100644 --- a/apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 +++ b/apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 @@ -208,6 +208,10 @@ components: properties: validate_only: $ref: "#/components/schemas/validate_only" + job_urls: + type: array + items: + type: string jobs: $ref: "#/components/schemas/list_of_jobs" next: diff --git a/apps/api/src/hyp3_api/handlers.py b/apps/api/src/hyp3_api/handlers.py index 93a1d6105..807ba5da0 100644 --- a/apps/api/src/hyp3_api/handlers.py +++ b/apps/api/src/hyp3_api/handlers.py @@ -38,7 +38,6 @@ def base_url() -> str: # TODO add or update unit test(s) -# TODO update response schema def post_jobs(body, user): print(body) From 16056a794a6f7ef18b09849fb14fe4fe8511ead2 Mon Sep 17 00:00:00 2001 From: Jake Herrmann Date: Wed, 10 May 2023 16:08:27 -0800 Subject: [PATCH 6/6] add example job url in api spec --- apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 b/apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 index e4000c1df..b9fb3e611 100644 --- a/apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 +++ b/apps/api/src/hyp3_api/api-spec/openapi-spec.yml.j2 @@ -203,15 +203,14 @@ components: description: List of submitted jobs. type: object required: + - job_urls - jobs additionalProperties: false properties: validate_only: $ref: "#/components/schemas/validate_only" job_urls: - type: array - items: - type: string + $ref: "#/components/schemas/job_urls" jobs: $ref: "#/components/schemas/list_of_jobs" next: @@ -447,6 +446,13 @@ components: type: boolean default: false + job_urls: + type: array + items: + type: string + format: url + example: https://hyp3-api.asf.alaska.edu/jobs/27836b79-e5b2-4d8f-932f-659724ea02c3 + job_or_subscription_id: description: Unique identifier for a job or subscription type: string