Skip to content

Commit ffb3e35

Browse files
authored
feat: add support for Conditions in Serverless API (#742)
1 parent b5d9fa1 commit ffb3e35

File tree

6 files changed

+62
-12
lines changed

6 files changed

+62
-12
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class ApiGenerator(object):
2727
def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variables, depends_on,
2828
definition_body, definition_uri, name, stage_name, endpoint_configuration=None,
2929
method_settings=None, binary_media=None, cors=None, auth=None, access_log_setting=None,
30-
canary_setting=None, tracing_enabled=None):
30+
canary_setting=None, tracing_enabled=None, resource_attributes=None,
31+
passthrough_resource_attributes=None):
3132
"""Constructs an API Generator class that generates API Gateway resources
3233
3334
:param logical_id: Logical id of the SAM API Resource
@@ -42,6 +43,8 @@ def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variab
4243
:param access_log_setting: Whether to send access logs and where for Stage
4344
:param canary_setting: Canary Setting for Stage
4445
:param tracing_enabled: Whether active tracing with X-ray is enabled
46+
:param resource_attributes: Resource attributes to add to API resources
47+
:param passthrough_resource_attributes: Attributes such as `Condition` that are added to derived resources
4548
"""
4649
self.logical_id = logical_id
4750
self.cache_cluster_enabled = cache_cluster_enabled
@@ -60,14 +63,16 @@ def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variab
6063
self.access_log_setting = access_log_setting
6164
self.canary_setting = canary_setting
6265
self.tracing_enabled = tracing_enabled
66+
self.resource_attributes = resource_attributes
67+
self.passthrough_resource_attributes = passthrough_resource_attributes
6368

6469
def _construct_rest_api(self):
6570
"""Constructs and returns the ApiGateway RestApi.
6671
6772
:returns: the RestApi to which this SAM Api corresponds
6873
:rtype: model.apigateway.ApiGatewayRestApi
6974
"""
70-
rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on)
75+
rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on, attributes=self.resource_attributes)
7176
rest_api.BinaryMediaTypes = self.binary_media
7277

7378
if self.endpoint_configuration:
@@ -132,7 +137,8 @@ def _construct_deployment(self, rest_api):
132137
:returns: the Deployment to which this SAM Api corresponds
133138
:rtype: model.apigateway.ApiGatewayDeployment
134139
"""
135-
deployment = ApiGatewayDeployment(self.logical_id + 'Deployment')
140+
deployment = ApiGatewayDeployment(self.logical_id + 'Deployment',
141+
attributes=self.passthrough_resource_attributes)
136142
deployment.RestApiId = rest_api.get_runtime_attr('rest_api_id')
137143
deployment.StageName = 'Stage'
138144

@@ -150,7 +156,8 @@ def _construct_stage(self, deployment, swagger):
150156
# This will NOT create duplicates because we allow only ONE stage per API resource
151157
stage_name_prefix = self.stage_name if isinstance(self.stage_name, string_types) else ""
152158

153-
stage = ApiGatewayStage(self.logical_id + stage_name_prefix + 'Stage')
159+
stage = ApiGatewayStage(self.logical_id + stage_name_prefix + 'Stage',
160+
attributes=self.passthrough_resource_attributes)
154161
stage.RestApiId = ref(self.logical_id)
155162
stage.update_deployment_ref(deployment.logical_id)
156163
stage.StageName = self.stage_name
@@ -294,15 +301,16 @@ def _get_permission(self, authorizer_name, authorizer_lambda_function_arn):
294301
:returns: the permission resource
295302
:rtype: model.lambda_.LambdaPermission
296303
"""
297-
rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on)
304+
rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on, attributes=self.resource_attributes)
298305
api_id = rest_api.get_runtime_attr('rest_api_id')
299306

300307
partition = ArnGenerator.get_partition_name()
301308
resource = '${__ApiId__}/authorizers/*'
302309
source_arn = fnSub(ArnGenerator.generate_arn(partition=partition, service='execute-api', resource=resource),
303310
{"__ApiId__": api_id})
304311

305-
lambda_permission = LambdaPermission(self.logical_id + authorizer_name + 'AuthorizerPermission')
312+
lambda_permission = LambdaPermission(self.logical_id + authorizer_name + 'AuthorizerPermission',
313+
attributes=self.passthrough_resource_attributes)
306314
lambda_permission.Action = 'lambda:invokeFunction'
307315
lambda_permission.FunctionName = authorizer_lambda_function_arn
308316
lambda_permission.Principal = 'apigateway.amazonaws.com'

samtranslator/model/sam_resources.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,9 @@ def to_cloudformation(self, **kwargs):
467467
auth=self.Auth,
468468
access_log_setting=self.AccessLogSetting,
469469
canary_setting=self.CanarySetting,
470-
tracing_enabled=self.TracingEnabled)
470+
tracing_enabled=self.TracingEnabled,
471+
resource_attributes=self.resource_attributes,
472+
passthrough_resource_attributes=self.get_passthrough_resource_attributes())
471473

472474
rest_api, deployment, stage, permissions = api_generator.to_cloudformation()
473475

tests/translator/input/intrinsic_functions.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ Parameters:
1414
Type: String
1515
Default: SomeName
1616

17+
Conditions:
18+
TrueCondition:
19+
Fn::Equals:
20+
- true
21+
- true
22+
1723
Resources:
1824
MyFunction:
1925
Type: 'AWS::Serverless::Function'
@@ -74,6 +80,7 @@ Resources:
7480

7581
ApiWithExplicitS3Uri:
7682
Type: AWS::Serverless::Api
83+
Condition: TrueCondition
7784
Properties:
7885
StageName: dev
7986
DefinitionUri:

tests/translator/output/aws-cn/intrinsic_functions.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616
"Default": "PassThrough",
1717
"Type": "String"
1818
}
19-
},
19+
},
20+
"Conditions": {
21+
"TrueCondition": {
22+
"Fn::Equals": [
23+
true,
24+
true
25+
]
26+
}
27+
},
2028
"Resources": {
2129
"MyFunction": {
2230
"Type": "AWS::Lambda::Function",
@@ -105,6 +113,7 @@
105113
},
106114
"ApiWithExplicitS3UridevStage": {
107115
"Type": "AWS::ApiGateway::Stage",
116+
"Condition": "TrueCondition",
108117
"Properties": {
109118
"DeploymentId": {
110119
"Ref": "ApiWithExplicitS3UriDeploymenta227798f00"
@@ -193,6 +202,7 @@
193202
},
194203
"ApiWithExplicitS3UriDeploymenta227798f00": {
195204
"Type": "AWS::ApiGateway::Deployment",
205+
"Condition": "TrueCondition",
196206
"Properties": {
197207
"RestApiId": {
198208
"Ref": "ApiWithExplicitS3Uri"
@@ -360,6 +370,7 @@
360370
},
361371
"ApiWithExplicitS3Uri": {
362372
"Type": "AWS::ApiGateway::RestApi",
373+
"Condition": "TrueCondition",
363374
"Properties": {
364375
"EndpointConfiguration": {
365376
"Types": [

tests/translator/output/aws-us-gov/intrinsic_functions.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616
"Default": "PassThrough",
1717
"Type": "String"
1818
}
19-
},
19+
},
20+
"Conditions": {
21+
"TrueCondition": {
22+
"Fn::Equals": [
23+
true,
24+
true
25+
]
26+
}
27+
},
2028
"Resources": {
2129
"MyFunction": {
2230
"Type": "AWS::Lambda::Function",
@@ -105,6 +113,7 @@
105113
},
106114
"ApiWithExplicitS3UridevStage": {
107115
"Type": "AWS::ApiGateway::Stage",
116+
"Condition": "TrueCondition",
108117
"Properties": {
109118
"DeploymentId": {
110119
"Ref": "ApiWithExplicitS3UriDeploymenta227798f00"
@@ -193,6 +202,7 @@
193202
},
194203
"ApiWithExplicitS3UriDeploymenta227798f00": {
195204
"Type": "AWS::ApiGateway::Deployment",
205+
"Condition": "TrueCondition",
196206
"Properties": {
197207
"RestApiId": {
198208
"Ref": "ApiWithExplicitS3Uri"
@@ -360,6 +370,7 @@
360370
},
361371
"ApiWithExplicitS3Uri": {
362372
"Type": "AWS::ApiGateway::RestApi",
373+
"Condition": "TrueCondition",
363374
"Properties": {
364375
"EndpointConfiguration": {
365376
"Types": [

tests/translator/output/intrinsic_functions.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616
"Default": "PassThrough",
1717
"Type": "String"
1818
}
19-
},
19+
},
20+
"Conditions": {
21+
"TrueCondition": {
22+
"Fn::Equals": [
23+
true,
24+
true
25+
]
26+
}
27+
},
2028
"Resources": {
2129
"MyFunction": {
2230
"Type": "AWS::Lambda::Function",
@@ -104,7 +112,8 @@
104112
}
105113
},
106114
"ApiWithExplicitS3UridevStage": {
107-
"Type": "AWS::ApiGateway::Stage",
115+
"Type": "AWS::ApiGateway::Stage",
116+
"Condition": "TrueCondition",
108117
"Properties": {
109118
"DeploymentId": {
110119
"Ref": "ApiWithExplicitS3UriDeploymenta227798f00"
@@ -185,6 +194,7 @@
185194
},
186195
"ApiWithExplicitS3UriDeploymenta227798f00": {
187196
"Type": "AWS::ApiGateway::Deployment",
197+
"Condition": "TrueCondition",
188198
"Properties": {
189199
"RestApiId": {
190200
"Ref": "ApiWithExplicitS3Uri"
@@ -351,7 +361,8 @@
351361
}
352362
},
353363
"ApiWithExplicitS3Uri": {
354-
"Type": "AWS::ApiGateway::RestApi",
364+
"Type": "AWS::ApiGateway::RestApi",
365+
"Condition": "TrueCondition",
355366
"Properties": {
356367
"BodyS3Location": {
357368
"Version": "myversion",

0 commit comments

Comments
 (0)