Skip to content

Commit ed5adfd

Browse files
committed
add some comments to the code, and more test cases
1 parent e25c8ae commit ed5adfd

14 files changed

+885
-1
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,9 @@ def _openapi_postprocess(self, definition_body):
999999
):
10001000
if definition_body.get("securityDefinitions"):
10011001
components = definition_body.get("components", Py27Dict())
1002+
# In the previous line, the default value `Py27Dict()` will be only returned only if `components`
1003+
# property is not in definition_body dict, but if it exist, and its value is None, so None will be
1004+
# returned and not the default value. That is why the below line is required.
10021005
components = components if components else Py27Dict()
10031006
components["securitySchemes"] = definition_body["securityDefinitions"]
10041007
definition_body["components"] = components
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Resources:
2+
GetHtmlFunction:
3+
Type: AWS::Serverless::Function
4+
Properties:
5+
CodeUri: s3://sam-demo-bucket/member_portal.zip
6+
Handler: index.handler
7+
Runtime: nodejs12.x
8+
ExplicitApi:
9+
Type: AWS::Serverless::Api
10+
Properties:
11+
StageName: Prod
12+
DefinitionBody:
13+
info:
14+
version: '1.0'
15+
title:
16+
Ref: AWS::StackName
17+
securityDefinitions: # 1 Add security definition
18+
CognitoAuthorizer:
19+
type: "apiKey"
20+
name: "Authorization"
21+
in: "header"
22+
x-amazon-apigateway-authtype: "cognito_user_pools"
23+
x-amazon-apigateway-authorizer:
24+
providerARNs:
25+
- # userPool ARN
26+
type: "cognito_user_pools"
27+
paths:
28+
"/{proxy+}":
29+
x-amazon-apigateway-any-method:
30+
x-amazon-apigateway-integration:
31+
httpMethod: POST
32+
type: aws_proxy
33+
uri:
34+
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations
35+
responses: { }
36+
components:
37+
schemas:
38+
Error:
39+
type: Object
40+
properties:
41+
message:
42+
type: string
43+
openapi: '3.0.0'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Resources:
2+
GetHtmlFunction:
3+
Type: AWS::Serverless::Function
4+
Properties:
5+
CodeUri: s3://sam-demo-bucket/member_portal.zip
6+
Handler: index.handler
7+
Runtime: nodejs12.x
8+
ExplicitApi:
9+
Type: AWS::Serverless::Api
10+
Properties:
11+
StageName: Prod
12+
DefinitionBody:
13+
info:
14+
version: '1.0'
15+
title:
16+
Ref: AWS::StackName
17+
securityDefinitions: # 1 Add security definition
18+
CognitoAuthorizer:
19+
type: "apiKey"
20+
name: "Authorization"
21+
in: "header"
22+
x-amazon-apigateway-authtype: "cognito_user_pools"
23+
x-amazon-apigateway-authorizer:
24+
providerARNs:
25+
- # userPool ARN
26+
type: "cognito_user_pools"
27+
paths:
28+
"/{proxy+}":
29+
x-amazon-apigateway-any-method:
30+
x-amazon-apigateway-integration:
31+
httpMethod: POST
32+
type: aws_proxy
33+
uri:
34+
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations
35+
responses: { }
36+
openapi: '3.0.0'
File renamed without changes.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{
2+
"Resources": {
3+
"GetHtmlFunction": {
4+
"Type": "AWS::Lambda::Function",
5+
"Properties": {
6+
"Code": {
7+
"S3Bucket": "sam-demo-bucket",
8+
"S3Key": "member_portal.zip"
9+
},
10+
"Handler": "index.handler",
11+
"Role": {
12+
"Fn::GetAtt": [
13+
"GetHtmlFunctionRole",
14+
"Arn"
15+
]
16+
},
17+
"Runtime": "nodejs12.x",
18+
"Tags": [
19+
{
20+
"Key": "lambda:createdBy",
21+
"Value": "SAM"
22+
}
23+
]
24+
}
25+
},
26+
"GetHtmlFunctionRole": {
27+
"Type": "AWS::IAM::Role",
28+
"Properties": {
29+
"AssumeRolePolicyDocument": {
30+
"Version": "2012-10-17",
31+
"Statement": [
32+
{
33+
"Action": [
34+
"sts:AssumeRole"
35+
],
36+
"Effect": "Allow",
37+
"Principal": {
38+
"Service": [
39+
"lambda.amazonaws.com"
40+
]
41+
}
42+
}
43+
]
44+
},
45+
"ManagedPolicyArns": [
46+
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
47+
],
48+
"Tags": [
49+
{
50+
"Key": "lambda:createdBy",
51+
"Value": "SAM"
52+
}
53+
]
54+
}
55+
},
56+
"ExplicitApiDeployment195f5bf5d0": {
57+
"Type": "AWS::ApiGateway::Deployment",
58+
"Properties": {
59+
"Description": "RestApi deployment id: 195f5bf5d07bf7af9c64f0649d2724425b106350",
60+
"RestApiId": {
61+
"Ref": "ExplicitApi"
62+
},
63+
"StageName": "Stage"
64+
}
65+
},
66+
"ExplicitApiProdStage": {
67+
"Type": "AWS::ApiGateway::Stage",
68+
"Properties": {
69+
"DeploymentId": {
70+
"Ref": "ExplicitApiDeployment195f5bf5d0"
71+
},
72+
"RestApiId": {
73+
"Ref": "ExplicitApi"
74+
},
75+
"StageName": "Prod"
76+
}
77+
},
78+
"ExplicitApi": {
79+
"Type": "AWS::ApiGateway::RestApi",
80+
"Properties": {
81+
"Body": {
82+
"info": {
83+
"version": "1.0",
84+
"title": {
85+
"Ref": "AWS::StackName"
86+
}
87+
},
88+
"paths": {
89+
"/{proxy+}": {
90+
"x-amazon-apigateway-any-method": {
91+
"x-amazon-apigateway-integration": {
92+
"httpMethod": "POST",
93+
"type": "aws_proxy",
94+
"uri": {
95+
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations"
96+
}
97+
},
98+
"responses": {}
99+
}
100+
}
101+
},
102+
"openapi": "3.0.0",
103+
"components": {
104+
"securitySchemes": {
105+
"CognitoAuthorizer": {
106+
"x-amazon-apigateway-authtype": "cognito_user_pools",
107+
"type": "apiKey",
108+
"name": "Authorization",
109+
"x-amazon-apigateway-authorizer": {
110+
"providerARNs": [
111+
null
112+
],
113+
"type": "cognito_user_pools"
114+
},
115+
"in": "header"
116+
}
117+
},
118+
"schemas": {
119+
"Error": {
120+
"type": "Object",
121+
"properties": {
122+
"message": {
123+
"type": "string"
124+
}
125+
}
126+
}
127+
}
128+
}
129+
}
130+
}
131+
}
132+
}
133+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"Resources": {
3+
"GetHtmlFunction": {
4+
"Type": "AWS::Lambda::Function",
5+
"Properties": {
6+
"Code": {
7+
"S3Bucket": "sam-demo-bucket",
8+
"S3Key": "member_portal.zip"
9+
},
10+
"Handler": "index.handler",
11+
"Role": {
12+
"Fn::GetAtt": [
13+
"GetHtmlFunctionRole",
14+
"Arn"
15+
]
16+
},
17+
"Runtime": "nodejs12.x",
18+
"Tags": [
19+
{
20+
"Key": "lambda:createdBy",
21+
"Value": "SAM"
22+
}
23+
]
24+
}
25+
},
26+
"GetHtmlFunctionRole": {
27+
"Type": "AWS::IAM::Role",
28+
"Properties": {
29+
"AssumeRolePolicyDocument": {
30+
"Version": "2012-10-17",
31+
"Statement": [
32+
{
33+
"Action": [
34+
"sts:AssumeRole"
35+
],
36+
"Effect": "Allow",
37+
"Principal": {
38+
"Service": [
39+
"lambda.amazonaws.com"
40+
]
41+
}
42+
}
43+
]
44+
},
45+
"ManagedPolicyArns": [
46+
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
47+
],
48+
"Tags": [
49+
{
50+
"Key": "lambda:createdBy",
51+
"Value": "SAM"
52+
}
53+
]
54+
}
55+
},
56+
"ExplicitApiProdStage": {
57+
"Type": "AWS::ApiGateway::Stage",
58+
"Properties": {
59+
"DeploymentId": {
60+
"Ref": "ExplicitApiDeployment407993a935"
61+
},
62+
"RestApiId": {
63+
"Ref": "ExplicitApi"
64+
},
65+
"StageName": "Prod"
66+
}
67+
},
68+
"ExplicitApiDeployment407993a935": {
69+
"Type": "AWS::ApiGateway::Deployment",
70+
"Properties": {
71+
"Description": "RestApi deployment id: 407993a9358b76c8e74599b2c0b914409ee0da64",
72+
"RestApiId": {
73+
"Ref": "ExplicitApi"
74+
},
75+
"StageName": "Stage"
76+
}
77+
},
78+
"ExplicitApi": {
79+
"Type": "AWS::ApiGateway::RestApi",
80+
"Properties": {
81+
"Body": {
82+
"info": {
83+
"version": "1.0",
84+
"title": {
85+
"Ref": "AWS::StackName"
86+
}
87+
},
88+
"paths": {
89+
"/{proxy+}": {
90+
"x-amazon-apigateway-any-method": {
91+
"x-amazon-apigateway-integration": {
92+
"httpMethod": "POST",
93+
"type": "aws_proxy",
94+
"uri": {
95+
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHtmlFunction.Arn}/invocations"
96+
}
97+
},
98+
"responses": {}
99+
}
100+
}
101+
},
102+
"openapi": "3.0.0",
103+
"components": {
104+
"securitySchemes": {
105+
"CognitoAuthorizer": {
106+
"x-amazon-apigateway-authtype": "cognito_user_pools",
107+
"type": "apiKey",
108+
"name": "Authorization",
109+
"x-amazon-apigateway-authorizer": {
110+
"providerARNs": [
111+
null
112+
],
113+
"type": "cognito_user_pools"
114+
},
115+
"in": "header"
116+
}
117+
}
118+
}
119+
}
120+
}
121+
}
122+
}
123+
}
File renamed without changes.

0 commit comments

Comments
 (0)