Skip to content

Commit

Permalink
Fixing DisableExecuteApiEndpoint property for REST and adding integ t… (
Browse files Browse the repository at this point in the history
#2272)

* Fixing DisableExecuteApiEndpoint property for REST and adding integ tests.

* Fixing DisableExecuteApiEndpoint property for REST and adding integ tests.

* Fixing output templates to match py2 hashing.
  • Loading branch information
sattigar authored Dec 21, 2021
1 parent 3808cc4 commit 8b89575
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 3 deletions.
6 changes: 3 additions & 3 deletions samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ def _construct_rest_api(self):
self._add_binary_media_types()
self._add_models()

if self.disable_execute_api_endpoint is not None:
self._add_endpoint_extension()

if self.definition_uri:
rest_api.BodyS3Location = self._construct_body_s3_dict()
elif self.definition_body:
Expand All @@ -292,9 +295,6 @@ def _construct_rest_api(self):
if self.mode:
rest_api.Mode = self.mode

if self.disable_execute_api_endpoint is not None:
self._add_endpoint_extension()

return rest_api

def _add_endpoint_extension(self):
Expand Down
22 changes: 22 additions & 0 deletions tests/translator/input/api_with_disable_api_execute_endpoint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DisableExecuteApiEndpoint: True
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}
130 changes: 130 additions & 0 deletions tests/translator/output/api_with_disable_api_execute_endpoint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"Resources": {
"ApiGatewayApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"info": {
"version": "1.0",
"title": {
"Ref": "AWS::StackName"
}
},
"paths": {
"/": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ApiFunction.Arn}/invocations"
}
},
"responses": {}
}
}
},
"swagger": "2.0",
"x-amazon-apigateway-endpoint-configuration": {
"disableExecuteApiEndpoint": true
}
}
}
},
"ApiGatewayApiDeploymenta13ba42368": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ApiGatewayApi"
},
"Description": "RestApi deployment id: a13ba42368cb482635c1d715f5e0199e94d79222",
"StageName": "Stage"
}
},
"ApiFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"ApiFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.handler",
"Code": {
"ZipFile": "def handler(event, context):\n return {'body': 'Hello World!', 'statusCode': 200}"
},
"Role": {
"Fn::GetAtt": [
"ApiFunctionRole",
"Arn"
]
},
"Runtime": "python3.7",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"ApiFunctionApiEventPermissionprod": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "ApiFunction"
},
"SourceArn": {
"Fn::Sub": [
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
{
"__Stage__": "*",
"__ApiId__": {
"Ref": "ApiGatewayApi"
}
}
]
}
}
},
"ApiGatewayApiprodStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ApiGatewayApiDeploymenta13ba42368"
},
"RestApiId": {
"Ref": "ApiGatewayApi"
},
"StageName": "prod"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{
"Resources": {
"ApiGatewayApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"info": {
"version": "1.0",
"title": {
"Ref": "AWS::StackName"
}
},
"paths": {
"/": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws-cn:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ApiFunction.Arn}/invocations"
}
},
"responses": {}
}
}
},
"swagger": "2.0",
"x-amazon-apigateway-endpoint-configuration": {
"disableExecuteApiEndpoint": true
}
},
"EndpointConfiguration": {
"Types": [
"REGIONAL"
]
},
"Parameters": {
"endpointConfigurationTypes": "REGIONAL"
}
}
},
"ApiGatewayApiDeployment31ce724e39": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ApiGatewayApi"
},
"Description": "RestApi deployment id: 31ce724e3998cb16b80266cfb4868670a87430bf",
"StageName": "Stage"
}
},
"ApiFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"ApiFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.handler",
"Code": {
"ZipFile": "def handler(event, context):\n return {'body': 'Hello World!', 'statusCode': 200}"
},
"Role": {
"Fn::GetAtt": [
"ApiFunctionRole",
"Arn"
]
},
"Runtime": "python3.7",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"ApiFunctionApiEventPermissionprod": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "ApiFunction"
},
"SourceArn": {
"Fn::Sub": [
"arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
{
"__Stage__": "*",
"__ApiId__": {
"Ref": "ApiGatewayApi"
}
}
]
}
}
},
"ApiGatewayApiprodStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ApiGatewayApiDeployment31ce724e39"
},
"RestApiId": {
"Ref": "ApiGatewayApi"
},
"StageName": "prod"
}
}
}
}
Loading

0 comments on commit 8b89575

Please sign in to comment.