Skip to content
Closed
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
19 changes: 8 additions & 11 deletions examples/apps/cloudfront-http-redirect/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
'use strict';

exports.handler = (event, context, callback) => {
/*
* Generate HTTP redirect response with 302 status code and Location header.
*/
const response = {
status: '302',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: 'http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html',
}],

var response = {
"statusCode": 302,
"headers": {
"Location": "http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html"
},
"body": "{}",
"isBase64Encoded": false
};
callback(null, response);
};

2 changes: 1 addition & 1 deletion samtranslator/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.13.0'
__version__ = '1.13.2'
2 changes: 1 addition & 1 deletion samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def _construct_stage(self, deployment, swagger):
stage.TracingEnabled = self.tracing_enabled

if swagger is not None:
deployment.make_auto_deployable(stage, swagger)
deployment.make_auto_deployable(stage, self.open_api_version, swagger)

return stage

Expand Down
18 changes: 13 additions & 5 deletions samtranslator/model/apigateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class ApiGatewayAccount(Resource):


class ApiGatewayDeployment(Resource):
_X_HASH_DELIMITER = "||"

resource_type = 'AWS::ApiGateway::Deployment'
property_types = {
'Description': PropertyType(False, is_str()),
Expand All @@ -73,9 +75,10 @@ class ApiGatewayDeployment(Resource):
"deployment_id": lambda self: ref(self.logical_id),
}

def make_auto_deployable(self, stage, swagger=None):
def make_auto_deployable(self, stage, openapi_version=None, swagger=None):
"""
Sets up the resource such that it will triggers a re-deployment when Swagger changes
Sets up the resource such that it will trigger a re-deployment when Swagger changes
or the openapi version changes.

:param swagger: Dictionary containing the Swagger definition of the API
"""
Expand All @@ -88,10 +91,15 @@ def make_auto_deployable(self, stage, swagger=None):
# to prevent redeployment when API has not changed

# NOTE: `str(swagger)` is for backwards compatibility. Changing it to a JSON or something will break compat
generator = logical_id_generator.LogicalIdGenerator(self.logical_id, str(swagger))
hash_input = [str(swagger)]
if openapi_version:
hash_input.append(str(openapi_version))

data = self._X_HASH_DELIMITER.join(hash_input)
generator = logical_id_generator.LogicalIdGenerator(self.logical_id, data)
self.logical_id = generator.gen()
hash = generator.get_hash(length=40) # Get the full hash
self.Description = "RestApi deployment id: {}".format(hash)
digest = generator.get_hash(length=40) # Get the full hash
self.Description = "RestApi deployment id: {}".format(digest)
stage.update_deployment_ref(self.logical_id)


Expand Down
3 changes: 3 additions & 0 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ def to_cloudformation(self, **kwargs):
"""
resources = []

intrinsics_resolver = kwargs["intrinsics_resolver"]
self.BinaryMediaTypes = intrinsics_resolver.resolve_parameter_refs(self.BinaryMediaTypes)

api_generator = ApiGenerator(self.logical_id,
self.CacheClusterEnabled,
self.CacheClusterSize,
Expand Down
4 changes: 3 additions & 1 deletion samtranslator/swagger/swagger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import json
import re
from six import string_types

Expand Down Expand Up @@ -257,7 +258,8 @@ def add_cors(self, path, allowed_origins, allowed_headers=None, allowed_methods=
allow_credentials)

def add_binary_media_types(self, binary_media_types):
self._doc[self._X_APIGW_BINARY_MEDIA_TYPES] = binary_media_types
bmt = json.loads(json.dumps(binary_media_types).replace('~1', '/'))
self._doc[self._X_APIGW_BINARY_MEDIA_TYPES] = bmt

def _options_method_response_for_cors(self, allowed_origins, allowed_headers=None, allowed_methods=None,
max_age=None, allow_credentials=None):
Expand Down
11 changes: 8 additions & 3 deletions tests/translator/input/api_with_binary_media_types.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Parameters:
BMT:
Type: String
Default: 'image~1jpg'
Globals:
Api:
BinaryMediaTypes:
- image/jpg
- {"Fn::Join": ["/", ["image", "png"]]}
- image~1gif
- {"Fn::Join": ["~1", ["image", "png"]]}

Resources:
ImplicitApiFunction:
Expand All @@ -24,4 +28,5 @@ Resources:
StageName: Prod
DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json
BinaryMediaTypes:
- image/gif
- application~1octet-stream
- !Ref BMT
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
Parameters:
BMT:
Type: String
Default: image~1jpeg
Globals:
Api:
BinaryMediaTypes:
- image/jpg
- {"Fn::Join": ["/", ["image", "png"]]}
- !Ref BMT
- image~1jpg
- {"Fn::Join": ["~1", ["image", "png"]]}

Resources:
ExplicitApiManagedSwagger:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
BinaryMediaTypes:
- image/gif
- image~1gif

ExplicitApiDefinitionBody:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
BinaryMediaTypes:
- application/json
- application~1json
DefinitionBody: {
"paths": {},
"swagger": "2.0",
Expand Down
20 changes: 10 additions & 10 deletions tests/translator/output/api_request_model_openapi_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
}
}
},
"HtmlApiDeploymentefb667b26e": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "HtmlApi"
},
"Description": "RestApi deployment id: efb667b26e8a0b0f733f5dfc27d039c1a2867db0"
}
},
"HtmlFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -49,7 +58,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "HtmlApiDeployment47f78fe7d9"
"Ref": "HtmlApiDeploymentefb667b26e"
},
"RestApiId": {
"Ref": "HtmlApi"
Expand Down Expand Up @@ -164,15 +173,6 @@
}
}
},
"HtmlApiDeployment47f78fe7d9": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "HtmlApi"
},
"Description": "RestApi deployment id: 47f78fe7d91eb5d2070674ad77943842c49dab89"
}
},
"HtmlFunctionGetHtmlPermissionTest": {
"Type": "AWS::Lambda::Permission",
"Properties": {
Expand Down
20 changes: 10 additions & 10 deletions tests/translator/output/api_with_auth_all_maximum_openapi_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@
}
}
},
"MyApiDeployment7d5eb3f14f": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "MyApi"
},
"Description": "RestApi deployment id: 7d5eb3f14f891b4276d48b764348b944dcaf2918"
}
},
"MyFunctionWithCognitoMultipleUserPoolsAuthorizerPermissionProd": {
"Type": "AWS::Lambda::Permission",
"Properties": {
Expand Down Expand Up @@ -453,20 +462,11 @@
}
}
},
"MyApiDeployment18e0cddfbf": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "MyApi"
},
"Description": "RestApi deployment id: 18e0cddfbf74ca986461d5a484b9eedd934a6423"
}
},
"MyApiProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "MyApiDeployment18e0cddfbf"
"Ref": "MyApiDeployment7d5eb3f14f"
},
"RestApiId": {
"Ref": "MyApi"
Expand Down
Loading