Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MinimumCompressionSize #786

Merged
merged 4 commits into from
Feb 1, 2019
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
1 change: 1 addition & 0 deletions docs/cloudformation_compatibility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Variables All
EndpointConfiguration All
MethodSettings All
BinaryMediaTypes All
MinimumCompressionSize All
Cors All
TracingEnabled All
================================== ======================== ========================
Expand Down
1 change: 1 addition & 0 deletions docs/globals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Currently, the following resources and properties are being supported:
EndpointConfiguration:
MethodSettings:
BinaryMediaTypes:
MinimumCompressionSize:
Cors:
AccessLogSetting:
CanarySetting:
Expand Down
3 changes: 3 additions & 0 deletions examples/2016-10-31/implicit_api_settings/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Globals:
# These are equivalent to image/gif and image/png when deployed
- image~1gif
- image~1png

# Compression is triggered when response body size is greater than or equal to your configured threshold
MinimumCompressionSize: 1024

# Logging, Metrics, Throttling, and all other Stage settings
MethodSettings: [{
Expand Down
6 changes: 4 additions & 2 deletions samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ 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,
method_settings=None, binary_media=None, cors=None, auth=None, access_log_setting=None,
canary_setting=None, tracing_enabled=None):
method_settings=None, binary_media=None, minimum_compression_size=None, cors=None,
auth=None, access_log_setting=None, canary_setting=None, tracing_enabled=None):
"""Constructs an API Generator class that generates API Gateway resources

:param logical_id: Logical id of the SAM API Resource
Expand Down Expand Up @@ -55,6 +55,7 @@ def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variab
self.endpoint_configuration = endpoint_configuration
self.method_settings = method_settings
self.binary_media = binary_media
self.minimum_compression_size = minimum_compression_size
self.cors = cors
self.auth = auth
self.access_log_setting = access_log_setting
Expand All @@ -69,6 +70,7 @@ def _construct_rest_api(self):
"""
rest_api = ApiGatewayRestApi(self.logical_id, depends_on=self.depends_on)
rest_api.BinaryMediaTypes = self.binary_media
rest_api.MinimumCompressionSize = self.minimum_compression_size

if self.endpoint_configuration:
self._set_endpoint_configuration(rest_api, self.endpoint_configuration)
Expand Down
3 changes: 2 additions & 1 deletion samtranslator/model/apigateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class ApiGatewayRestApi(Resource):
'Name': PropertyType(False, is_str()),
'Parameters': PropertyType(False, is_type(dict)),
'EndpointConfiguration': PropertyType(False, is_type(dict)),
"BinaryMediaTypes": PropertyType(False, is_type(list))
"BinaryMediaTypes": PropertyType(False, is_type(list)),
"MinimumCompressionSize": PropertyType(False, is_type(int))
}

runtime_attrs = {
Expand Down
2 changes: 1 addition & 1 deletion samtranslator/model/s3_utils/uri_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def to_s3_uri(code_dict):
raise TypeError("Code location should be a dictionary")

if version:
uri += "?versionId=" + version
uri += "?versionId=" + version

return uri

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 @@ -429,6 +429,7 @@ class SamApi(SamResourceMacro):
'EndpointConfiguration': PropertyType(False, is_str()),
'MethodSettings': PropertyType(False, is_type(list)),
'BinaryMediaTypes': PropertyType(False, is_type(list)),
'MinimumCompressionSize': PropertyType(False, is_type(int)),
'Cors': PropertyType(False, one_of(is_str(), is_type(dict))),
'Auth': PropertyType(False, is_type(dict)),
'AccessLogSetting': PropertyType(False, is_type(dict)),
Expand Down Expand Up @@ -463,6 +464,7 @@ def to_cloudformation(self, **kwargs):
endpoint_configuration=self.EndpointConfiguration,
method_settings=self.MethodSettings,
binary_media=self.BinaryMediaTypes,
minimum_compression_size=self.MinimumCompressionSize,
cors=self.Cors,
auth=self.Auth,
access_log_setting=self.AccessLogSetting,
Expand Down
1 change: 1 addition & 0 deletions samtranslator/plugins/globals/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Globals(object):
"EndpointConfiguration",
"MethodSettings",
"BinaryMediaTypes",
"MinimumCompressionSize",
"Cors",
"AccessLogSetting",
"CanarySetting",
Expand Down
2 changes: 1 addition & 1 deletion samtranslator/translator/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def translate(self, sam_template, parameter_values):
if 'Transform' in template:
del template['Transform']

if len(document_errors) is 0:
if len(document_errors) == 0:
template = intrinsics_resolver.resolve_sam_resource_id_refs(template, changed_logical_ids)
template = intrinsics_resolver.resolve_sam_resource_refs(template, supported_resource_refs)
return template
Expand Down
24 changes: 24 additions & 0 deletions tests/translator/input/api_with_minimum_compression_size.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Globals:
Api:
MinimumCompressionSize: 1024

Resources:
ImplicitApiFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/member_portal.zip
Handler: index.gethtml
Runtime: nodejs4.3
Events:
GetHtml:
Type: Api
Properties:
Path: /
Method: get

ExplicitApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json
MinimumCompressionSize: 256
176 changes: 176 additions & 0 deletions tests/translator/output/api_with_minimum_compression_size.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"Resources": {
"ImplicitApiFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "member_portal.zip"
},
"Handler": "index.gethtml",
"Role": {
"Fn::GetAtt": [
"ImplicitApiFunctionRole",
"Arn"
]
},
"Runtime": "nodejs4.3",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"ImplicitApiFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
}
}
},
"ExplicitApiDeploymentf117c932f7": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ExplicitApi"
},
"Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289",
"StageName": "Stage"
}
},
"ImplicitApiFunctionGetHtmlPermissionTest": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:invokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "ImplicitApiFunction"
},
"SourceArn": {
"Fn::Sub": [
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
{
"__Stage__": "*",
"__ApiId__": {
"Ref": "ServerlessRestApi"
}
}
]
}
}
},
"ImplicitApiFunctionGetHtmlPermissionProd": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:invokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "ImplicitApiFunction"
},
"SourceArn": {
"Fn::Sub": [
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
{
"__Stage__": "Prod",
"__ApiId__": {
"Ref": "ServerlessRestApi"
}
}
]
}
}
},
"ExplicitApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"MinimumCompressionSize": 256,
"BodyS3Location": {
"Bucket": "sam-demo-bucket",
"Key": "webpage_swagger.json"
}
}
},
"ExplicitApiProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ExplicitApiDeploymentf117c932f7"
},
"RestApiId": {
"Ref": "ExplicitApi"
},
"StageName": "Prod"
}
},
"ServerlessRestApiProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ServerlessRestApiDeployment62b96c1a61"
},
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"StageName": "Prod"
}
},
"ServerlessRestApiDeployment62b96c1a61": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"Description": "RestApi deployment id: 62b96c1a611878eefb13e8ef66dbc71b9ba3dd19",
"StageName": "Stage"
}
},
"ServerlessRestApi": {
"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/${ImplicitApiFunction.Arn}/invocations"
}
},
"responses": {}
}
}
},
"swagger": "2.0"
},
"MinimumCompressionSize": 1024
}
}
}
}
Loading