Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from samtranslator.model.intrinsics import is_instrinsic, fnSub
from samtranslator.model.lambda_ import LambdaPermission
from samtranslator.translator.arn_generator import ArnGenerator
from samtranslator.model.tags.resource_tagging import get_tag_list

_CORS_WILDCARD = "'*'"
CorsProperties = namedtuple("_CorsProperties", ["AllowMethods", "AllowHeaders", "AllowOrigin", "MaxAge",
Expand All @@ -30,7 +31,7 @@
class ApiGenerator(object):

def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variables, depends_on,
definition_body, definition_uri, name, stage_name, endpoint_configuration=None,
definition_body, definition_uri, name, stage_name, tags=None, endpoint_configuration=None,
method_settings=None, binary_media=None, minimum_compression_size=None, cors=None,
auth=None, gateway_responses=None, access_log_setting=None, canary_setting=None,
tracing_enabled=None, resource_attributes=None, passthrough_resource_attributes=None,
Expand All @@ -46,6 +47,7 @@ def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variab
:param definition_uri: URI to API definition
:param name: Name of the API Gateway resource
:param stage_name: Name of the Stage
:param tags: Stage Tags
:param access_log_setting: Whether to send access logs and where for Stage
:param canary_setting: Canary Setting for Stage
:param tracing_enabled: Whether active tracing with X-ray is enabled
Expand All @@ -62,6 +64,7 @@ def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variab
self.definition_uri = definition_uri
self.name = name
self.stage_name = stage_name
self.tags = tags
self.endpoint_configuration = endpoint_configuration
self.method_settings = method_settings
self.binary_media = binary_media
Expand Down Expand Up @@ -195,6 +198,9 @@ def _construct_stage(self, deployment, swagger):
if swagger is not None:
deployment.make_auto_deployable(stage, swagger)

if self.tags is not None:
stage.Tags = get_tag_list(self.tags)

return stage

def to_cloudformation(self):
Expand Down
3 changes: 2 additions & 1 deletion samtranslator/model/apigateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from samtranslator.model import PropertyType, Resource
from samtranslator.model.exceptions import InvalidResourceException
from samtranslator.model.types import is_type, one_of, is_str
from samtranslator.model.types import is_type, one_of, is_str, list_of
from samtranslator.model.intrinsics import ref, fnSub
from samtranslator.translator import logical_id_generator
from samtranslator.translator.arn_generator import ArnGenerator
Expand Down Expand Up @@ -40,6 +40,7 @@ class ApiGatewayStage(Resource):
'Description': PropertyType(False, is_str()),
'RestApiId': PropertyType(True, is_str()),
'StageName': PropertyType(True, one_of(is_str(), is_type(dict))),
'Tags': PropertyType(False, list_of(is_type(dict))),
'TracingEnabled': PropertyType(False, is_type(bool)),
'Variables': PropertyType(False, is_type(dict)),
"MethodSettings": PropertyType(False, is_type(list))
Expand Down
2 changes: 2 additions & 0 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ class SamApi(SamResourceMacro):

'Name': PropertyType(False, one_of(is_str(), is_type(dict))),
'StageName': PropertyType(True, one_of(is_str(), is_type(dict))),
'Tags': PropertyType(False, is_type(dict)),
'DefinitionBody': PropertyType(False, is_type(dict)),
'DefinitionUri': PropertyType(False, one_of(is_str(), is_type(dict))),
'CacheClusterEnabled': PropertyType(False, is_type(bool)),
Expand Down Expand Up @@ -483,6 +484,7 @@ def to_cloudformation(self, **kwargs):
self.DefinitionUri,
self.Name,
self.StageName,
tags=self.Tags,
endpoint_configuration=self.EndpointConfiguration,
method_settings=self.MethodSettings,
binary_media=self.BinaryMediaTypes,
Expand Down
34 changes: 34 additions & 0 deletions tests/model/test_sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from samtranslator.model.lambda_ import LambdaFunction, LambdaVersion
from samtranslator.model.apigateway import ApiGatewayRestApi
from samtranslator.model.apigateway import ApiGatewayDeployment
from samtranslator.model.apigateway import ApiGatewayStage
from samtranslator.model.sam_resources import SamFunction
from samtranslator.model.sam_resources import SamApi

Expand Down Expand Up @@ -121,3 +122,36 @@ def test_with_swagger_no_stage(self):

self.assertEqual(deployment.__len__(), 1)
self.assertEqual(deployment[0].StageName, "Stage")

class TestApiTags(TestCase):
kwargs = {
'intrinsics_resolver': IntrinsicsResolver({}),
'event_resources': [],
'managed_policy_map': {
"foo": "bar"
}
}

def test_with_no_tags(self):
api = SamApi("foo")
api.Tags = {}

resources = api.to_cloudformation(**self.kwargs)
deployment = [x for x in resources if isinstance(x, ApiGatewayStage)]

self.assertEqual(deployment.__len__(), 1)
self.assertEqual(deployment[0].Tags, [])

def test_with_tags(self):
api = SamApi("foo")
api.Tags = {
'MyKey': 'MyValue'
}

resources = api.to_cloudformation(**self.kwargs)
deployment = [x for x in resources if isinstance(x, ApiGatewayStage)]

self.assertEqual(deployment.__len__(), 1)
self.assertEqual(deployment[0].Tags, [
{'Key': 'MyKey', 'Value': 'MyValue'}
])
16 changes: 16 additions & 0 deletions tests/translator/input/api_with_stage_tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Parameters:
TagValueParam:
Type: String
Default: value

Resources:
MyApiWithStageTags:
Type: "AWS::Serverless::Api"
Properties:
StageName: Prod
Tags:
TagKey1: TagValue1
TagKey2: ""
TagKey3:
Ref: TagValueParam
TagKey4: "123"
67 changes: 67 additions & 0 deletions tests/translator/output/api_with_stage_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"Parameters": {
"TagValueParam": {
"Default": "value",
"Type": "String"
}
},
"Resources": {
"MyApiWithStageTagsDeployment5332c373d4": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "MyApiWithStageTags"
},
"Description": "RestApi deployment id: 5332c373d45c69e6c0f562b4a419aa8eb311adc7",
"StageName": "Stage"
}
},
"MyApiWithStageTags": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"info": {
"version": "1.0",
"title": {
"Ref": "AWS::StackName"
}
},
"paths": {},
"swagger": "2.0"
}
}
},
"MyApiWithStageTagsProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "MyApiWithStageTagsDeployment5332c373d4"
},
"RestApiId": {
"Ref": "MyApiWithStageTags"
},
"StageName": "Prod",
"Tags": [
{
"Value": "TagValue1",
"Key": "TagKey1"
},
{
"Value": "",
"Key": "TagKey2"
},
{
"Value": {
"Ref": "TagValueParam"
},
"Key": "TagKey3"
},
{
"Value": "123",
"Key": "TagKey4"
}
]
}
}
}
}
75 changes: 75 additions & 0 deletions tests/translator/output/aws-cn/api_with_stage_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"Parameters": {
"TagValueParam": {
"Default": "value",
"Type": "String"
}
},
"Resources": {
"MyApiWithStageTagsDeployment5332c373d4": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "MyApiWithStageTags"
},
"Description": "RestApi deployment id: 5332c373d45c69e6c0f562b4a419aa8eb311adc7",
"StageName": "Stage"
}
},
"MyApiWithStageTags": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"info": {
"version": "1.0",
"title": {
"Ref": "AWS::StackName"
}
},
"paths": {},
"swagger": "2.0"
},
"EndpointConfiguration": {
"Types": [
"REGIONAL"
]
},
"Parameters": {
"endpointConfigurationTypes": "REGIONAL"
}
}
},
"MyApiWithStageTagsProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "MyApiWithStageTagsDeployment5332c373d4"
},
"RestApiId": {
"Ref": "MyApiWithStageTags"
},
"StageName": "Prod",
"Tags": [
{
"Value": "TagValue1",
"Key": "TagKey1"
},
{
"Value": "",
"Key": "TagKey2"
},
{
"Value": {
"Ref": "TagValueParam"
},
"Key": "TagKey3"
},
{
"Value": "123",
"Key": "TagKey4"
}
]
}
}
}
}
75 changes: 75 additions & 0 deletions tests/translator/output/aws-us-gov/api_with_stage_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"Parameters": {
"TagValueParam": {
"Default": "value",
"Type": "String"
}
},
"Resources": {
"MyApiWithStageTagsDeployment5332c373d4": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "MyApiWithStageTags"
},
"Description": "RestApi deployment id: 5332c373d45c69e6c0f562b4a419aa8eb311adc7",
"StageName": "Stage"
}
},
"MyApiWithStageTags": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"info": {
"version": "1.0",
"title": {
"Ref": "AWS::StackName"
}
},
"paths": {},
"swagger": "2.0"
},
"EndpointConfiguration": {
"Types": [
"REGIONAL"
]
},
"Parameters": {
"endpointConfigurationTypes": "REGIONAL"
}
}
},
"MyApiWithStageTagsProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "MyApiWithStageTagsDeployment5332c373d4"
},
"RestApiId": {
"Ref": "MyApiWithStageTags"
},
"StageName": "Prod",
"Tags": [
{
"Value": "TagValue1",
"Key": "TagKey1"
},
{
"Value": "",
"Key": "TagKey2"
},
{
"Value": {
"Ref": "TagValueParam"
},
"Key": "TagKey3"
},
{
"Value": "123",
"Key": "TagKey4"
}
]
}
}
}
}
1 change: 1 addition & 0 deletions tests/translator/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class TestTranslatorEndToEnd(TestCase):
'api_with_canary_setting',
'api_with_xray_tracing',
'api_request_model',
'api_with_stage_tags',
's3',
's3_create_remove',
's3_existing_lambda_notification_configuration',
Expand Down
1 change: 1 addition & 0 deletions versions/2016-10-31.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ CacheClusterEnabled | `boolean` | Indicates whether cache clustering is enabled
CacheClusterSize | `string` | The stage's cache cluster size.
Variables | Map of `string` to `string` | A map (string to string map) that defines the stage variables, where the variable name is the key and the variable value is the value. Variable names are limited to alphanumeric characters. Values must match the following regular expression: `[A-Za-z0-9._~:/?#&=,-]+`.
MethodSettings | [CloudFormation MethodSettings property](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-methodsetting.html) | Configures all settings for API stage including Logging, Metrics, CacheTTL, Throttling. This value is passed through to CloudFormation. So any values supported by CloudFormation ``MethodSettings`` property can be used here.
Tags | Map of `string` to `string` | A map (string to string) that specifies the tags to be added to this API Stage. Keys and values are limited to alphanumeric characters.
EndpointConfiguration | `string` | Specify the type of endpoint for API endpoint. Value is either `REGIONAL`, `EDGE`, or `PRIVATE`.
BinaryMediaTypes | List of `string` | List of MIME types that your API could return. Use this to enable binary support for APIs. Use `~1` instead of `/` in the mime types (See examples in [template.yaml](../examples/2016-10-31/implicit_api_settings/template.yaml)).
MinimumCompressionSize | `int` | Allow compression of response bodies based on client's Accept-Encoding header. Compression is triggered when response body size is greater than or equal to your configured threshold. The maximum body size threshold is 10 MB (10,485,760 Bytes). The following compression types are supported: gzip, deflate, and identity.
Expand Down