From 7c622a5d6a5122230463b539e2a649da61c9a66a Mon Sep 17 00:00:00 2001 From: Wilton Wang Date: Mon, 10 Jan 2022 13:17:38 -0800 Subject: [PATCH 1/7] Added Python3 Support for Translate CLI --- bin/sam-translate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/sam-translate.py b/bin/sam-translate.py index 3e961031b..d20eb5f2f 100755 --- a/bin/sam-translate.py +++ b/bin/sam-translate.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python2 - """Convert SAM templates to CloudFormation templates. Known limitations: cannot transform CodeUri pointing at local directory. @@ -27,6 +25,8 @@ import boto3 from docopt import docopt +if sys.version_info.major == 3: + from functools import reduce my_path = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, my_path + "/..") From 8a226ce0744ae0e7618ab1c3b1e2ff4f5da6aa47 Mon Sep 17 00:00:00 2001 From: Wilton Wang Date: Mon, 10 Jan 2022 13:17:51 -0800 Subject: [PATCH 2/7] Fixed disable_execute_api_endpoint: false Case --- samtranslator/model/api/http_api_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samtranslator/model/api/http_api_generator.py b/samtranslator/model/api/http_api_generator.py index 6089b5bb3..9701605a2 100644 --- a/samtranslator/model/api/http_api_generator.py +++ b/samtranslator/model/api/http_api_generator.py @@ -142,7 +142,7 @@ def _add_endpoint_configuration(self): For this reason, we always put DisableExecuteApiEndpoint into openapi object. """ - if self.disable_execute_api_endpoint and not self.definition_body: + if self.disable_execute_api_endpoint is not None and not self.definition_body: raise InvalidResourceException( self.logical_id, "DisableExecuteApiEndpoint works only within 'DefinitionBody' property." ) From 01c667f4d773e1df44978047c8107217cb14d328 Mon Sep 17 00:00:00 2001 From: Wilton Wang Date: Mon, 10 Jan 2022 13:28:07 -0800 Subject: [PATCH 3/7] Formatted with Black --- bin/sam-translate.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/sam-translate.py b/bin/sam-translate.py index d20eb5f2f..6c5ab229d 100755 --- a/bin/sam-translate.py +++ b/bin/sam-translate.py @@ -25,6 +25,7 @@ import boto3 from docopt import docopt + if sys.version_info.major == 3: from functools import reduce From 4454977c0c6a1c5c217d0955908db80151f0ad86 Mon Sep 17 00:00:00 2001 From: Wilton Wang Date: Mon, 10 Jan 2022 14:28:30 -0800 Subject: [PATCH 4/7] Added Fix for HttpApi --- samtranslator/model/api/api_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samtranslator/model/api/api_generator.py b/samtranslator/model/api/api_generator.py index c1a07fcdf..514f71512 100644 --- a/samtranslator/model/api/api_generator.py +++ b/samtranslator/model/api/api_generator.py @@ -306,7 +306,7 @@ def _add_endpoint_extension(self): https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-definitionbody For this reason, we always put DisableExecuteApiEndpoint into openapi object irrespective of origin of DefinitionBody. """ - if self.disable_execute_api_endpoint and not self.definition_body: + if self.disable_execute_api_endpoint is not None and not self.definition_body: raise InvalidResourceException( self.logical_id, "DisableExecuteApiEndpoint works only within 'DefinitionBody' property." ) From 7eec4d55d0a3906dbb5348e264a57f99ff89d6c9 Mon Sep 17 00:00:00 2001 From: Wilton Wang Date: Mon, 10 Jan 2022 14:29:16 -0800 Subject: [PATCH 5/7] Added Unit Tests --- ...th_disable_api_execute_endpoint_false.yaml | 24 +++++++++++++++++++ ...th_disable_api_execute_endpoint_false.yaml | 24 +++++++++++++++++++ ...th_disable_api_execute_endpoint_false.json | 3 +++ ...th_disable_api_execute_endpoint_false.json | 3 +++ 4 files changed, 54 insertions(+) create mode 100644 tests/translator/input/error_api_with_disable_api_execute_endpoint_false.yaml create mode 100644 tests/translator/input/error_httpapi_with_disable_api_execute_endpoint_false.yaml create mode 100644 tests/translator/output/error_api_with_disable_api_execute_endpoint_false.json create mode 100644 tests/translator/output/error_httpapi_with_disable_api_execute_endpoint_false.json diff --git a/tests/translator/input/error_api_with_disable_api_execute_endpoint_false.yaml b/tests/translator/input/error_api_with_disable_api_execute_endpoint_false.yaml new file mode 100644 index 000000000..e9e66b3cd --- /dev/null +++ b/tests/translator/input/error_api_with_disable_api_execute_endpoint_false.yaml @@ -0,0 +1,24 @@ +Resources: + ApiGatewayApi: + Type: AWS::Serverless::Api + Properties: + StageName: prod + DisableExecuteApiEndpoint: False + DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json + + ApiFunction: # Adds a GET api endpoint at "/" to the ApiGatewayApi via an Api event + Type: AWS::Serverless::Function + Properties: + Events: + ApiEvent: + Type: Api + Properties: + Path: / + Method: get + RestApiId: + Ref: ApiGatewayApi + Runtime: python3.7 + Handler: index.handler + InlineCode: | + def handler(event, context): + return {'body': 'Hello World!', 'statusCode': 200} \ No newline at end of file diff --git a/tests/translator/input/error_httpapi_with_disable_api_execute_endpoint_false.yaml b/tests/translator/input/error_httpapi_with_disable_api_execute_endpoint_false.yaml new file mode 100644 index 000000000..942d42981 --- /dev/null +++ b/tests/translator/input/error_httpapi_with_disable_api_execute_endpoint_false.yaml @@ -0,0 +1,24 @@ +Resources: + ApiGatewayApi: + Type: AWS::Serverless::HttpApi + Properties: + StageName: prod + DisableExecuteApiEndpoint: False + DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json + + ApiFunction: # Adds a GET api endpoint at "/" to the ApiGatewayApi via an Api event + Type: AWS::Serverless::Function + Properties: + Events: + ApiEvent: + Type: Api + Properties: + Path: / + Method: get + RestApiId: + Ref: ApiGatewayApi + Runtime: python3.7 + Handler: index.handler + InlineCode: | + def handler(event, context): + return {'body': 'Hello World!', 'statusCode': 200} \ No newline at end of file diff --git a/tests/translator/output/error_api_with_disable_api_execute_endpoint_false.json b/tests/translator/output/error_api_with_disable_api_execute_endpoint_false.json new file mode 100644 index 000000000..5adf87eaf --- /dev/null +++ b/tests/translator/output/error_api_with_disable_api_execute_endpoint_false.json @@ -0,0 +1,3 @@ +{ + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [ApiGatewayApi] is invalid. DisableExecuteApiEndpoint works only within 'DefinitionBody' property." +} \ No newline at end of file diff --git a/tests/translator/output/error_httpapi_with_disable_api_execute_endpoint_false.json b/tests/translator/output/error_httpapi_with_disable_api_execute_endpoint_false.json new file mode 100644 index 000000000..5adf87eaf --- /dev/null +++ b/tests/translator/output/error_httpapi_with_disable_api_execute_endpoint_false.json @@ -0,0 +1,3 @@ +{ + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [ApiGatewayApi] is invalid. DisableExecuteApiEndpoint works only within 'DefinitionBody' property." +} \ No newline at end of file From b33373d011f5bbd1fe563dae0b958f799000001b Mon Sep 17 00:00:00 2001 From: Wilton Wang Date: Mon, 10 Jan 2022 14:34:16 -0800 Subject: [PATCH 6/7] Removed Function from Test Template --- ...th_disable_api_execute_endpoint_false.yaml | 19 +-------------- ...th_disable_api_execute_endpoint_false.yaml | 7 ++++++ ...th_disable_api_execute_endpoint_false.yaml | 24 ------------------- ...h_disable_api_execute_endpoint_false.json} | 0 4 files changed, 8 insertions(+), 42 deletions(-) create mode 100644 tests/translator/input/error_http_api_with_disable_api_execute_endpoint_false.yaml delete mode 100644 tests/translator/input/error_httpapi_with_disable_api_execute_endpoint_false.yaml rename tests/translator/output/{error_httpapi_with_disable_api_execute_endpoint_false.json => error_http_api_with_disable_api_execute_endpoint_false.json} (100%) diff --git a/tests/translator/input/error_api_with_disable_api_execute_endpoint_false.yaml b/tests/translator/input/error_api_with_disable_api_execute_endpoint_false.yaml index e9e66b3cd..d419bc577 100644 --- a/tests/translator/input/error_api_with_disable_api_execute_endpoint_false.yaml +++ b/tests/translator/input/error_api_with_disable_api_execute_endpoint_false.yaml @@ -4,21 +4,4 @@ Resources: Properties: StageName: prod DisableExecuteApiEndpoint: False - DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json - - ApiFunction: # Adds a GET api endpoint at "/" to the ApiGatewayApi via an Api event - Type: AWS::Serverless::Function - Properties: - Events: - ApiEvent: - Type: Api - Properties: - Path: / - Method: get - RestApiId: - Ref: ApiGatewayApi - Runtime: python3.7 - Handler: index.handler - InlineCode: | - def handler(event, context): - return {'body': 'Hello World!', 'statusCode': 200} \ No newline at end of file + DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json \ No newline at end of file diff --git a/tests/translator/input/error_http_api_with_disable_api_execute_endpoint_false.yaml b/tests/translator/input/error_http_api_with_disable_api_execute_endpoint_false.yaml new file mode 100644 index 000000000..64e4eb7fe --- /dev/null +++ b/tests/translator/input/error_http_api_with_disable_api_execute_endpoint_false.yaml @@ -0,0 +1,7 @@ +Resources: + ApiGatewayApi: + Type: AWS::Serverless::HttpApi + Properties: + StageName: prod + DisableExecuteApiEndpoint: False + DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json \ No newline at end of file diff --git a/tests/translator/input/error_httpapi_with_disable_api_execute_endpoint_false.yaml b/tests/translator/input/error_httpapi_with_disable_api_execute_endpoint_false.yaml deleted file mode 100644 index 942d42981..000000000 --- a/tests/translator/input/error_httpapi_with_disable_api_execute_endpoint_false.yaml +++ /dev/null @@ -1,24 +0,0 @@ -Resources: - ApiGatewayApi: - Type: AWS::Serverless::HttpApi - Properties: - StageName: prod - DisableExecuteApiEndpoint: False - DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json - - ApiFunction: # Adds a GET api endpoint at "/" to the ApiGatewayApi via an Api event - Type: AWS::Serverless::Function - Properties: - Events: - ApiEvent: - Type: Api - Properties: - Path: / - Method: get - RestApiId: - Ref: ApiGatewayApi - Runtime: python3.7 - Handler: index.handler - InlineCode: | - def handler(event, context): - return {'body': 'Hello World!', 'statusCode': 200} \ No newline at end of file diff --git a/tests/translator/output/error_httpapi_with_disable_api_execute_endpoint_false.json b/tests/translator/output/error_http_api_with_disable_api_execute_endpoint_false.json similarity index 100% rename from tests/translator/output/error_httpapi_with_disable_api_execute_endpoint_false.json rename to tests/translator/output/error_http_api_with_disable_api_execute_endpoint_false.json From ccc2dba305533f8df9c13fa7d4f13233feee38c2 Mon Sep 17 00:00:00 2001 From: Wilton Wang Date: Mon, 17 Jan 2022 10:14:26 -0800 Subject: [PATCH 7/7] Updated Executable to be Python 3 Only --- bin/sam-translate.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/sam-translate.py b/bin/sam-translate.py index 6c5ab229d..e533e2949 100755 --- a/bin/sam-translate.py +++ b/bin/sam-translate.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + """Convert SAM templates to CloudFormation templates. Known limitations: cannot transform CodeUri pointing at local directory. @@ -24,10 +26,9 @@ import sys import boto3 -from docopt import docopt -if sys.version_info.major == 3: - from functools import reduce +from docopt import docopt +from functools import reduce my_path = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, my_path + "/..")