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
12 changes: 12 additions & 0 deletions samtranslator/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
""" CloudFormation Resource serialization, deserialization, and validation """
from six import string_types

import re
import inspect
from samtranslator.model.exceptions import InvalidResourceException
Expand Down Expand Up @@ -424,6 +426,16 @@ def _check_tag(self, reserved_tag_name, tags):
"cannot be set on your resource. "
"Please change the tag key in the input.")

def _resolve_string_parameter(self, intrinsics_resolver, parameter_value, parameter_name):
if not parameter_value:
return parameter_value
value = intrinsics_resolver.resolve_parameter_refs(parameter_value)
if not isinstance(value, string_types):
raise InvalidResourceException(self.logical_id,
"Could not resolve parameter for '{}' or parameter is not a String."
.format(parameter_name))
return value


class ResourceTypeResolver(object):
"""ResourceTypeResolver maps Resource Types to Resource classes, e.g. AWS::Serverless::Function to
Expand Down
19 changes: 10 additions & 9 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,13 @@ def _construct_lambda_layer(self, intrinsics_resolver):
:returns: a list containing the Lambda function and execution role resources
:rtype: list
"""
retention_policy_value = self._get_retention_policy_value(intrinsics_resolver)
# Resolve intrinsics if applicable:
self.LayerName = self._resolve_string_parameter(intrinsics_resolver, self.LayerName, 'LayerName')
self.LicenseInfo = self._resolve_string_parameter(intrinsics_resolver, self.LicenseInfo, 'LicenseInfo')
self.Description = self._resolve_string_parameter(intrinsics_resolver, self.Description, 'Description')
self.RetentionPolicy = self._resolve_string_parameter(intrinsics_resolver, self.RetentionPolicy, 'RetentionPolicy')

retention_policy_value = self._get_retention_policy_value()

attributes = self.get_passthrough_resource_attributes()
if attributes is None:
Expand Down Expand Up @@ -662,19 +668,12 @@ def _construct_lambda_layer(self, intrinsics_resolver):

return lambda_layer

def _get_retention_policy_value(self, intrinsics_resolver):
def _get_retention_policy_value(self):
"""
Sets the deletion policy on this resource. The default is 'Retain'.

:return: value for the DeletionPolicy attribute.
"""
if isinstance(self.RetentionPolicy, dict):
self.RetentionPolicy = intrinsics_resolver.resolve_parameter_refs(self.RetentionPolicy)
# If it's still not a string, throw an exception
if not isinstance(self.RetentionPolicy, string_types):
raise InvalidResourceException(self.logical_id,
"Could not resolve parameter for '{}' or parameter is not a String."
.format('RetentionPolicy'))

if self.RetentionPolicy is None or self.RetentionPolicy.lower() == self.RETAIN.lower():
return self.RETAIN
Expand All @@ -684,3 +683,5 @@ def _get_retention_policy_value(self, intrinsics_resolver):
raise InvalidResourceException(self.logical_id,
"'{}' must be one of the following options: {}."
.format('RetentionPolicy', [self.RETAIN, self.DELETE]))


12 changes: 11 additions & 1 deletion tests/translator/input/layers_with_intrinsics.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Parameters:
LayerNameParam:
Type: String
Default: SomeLayerName
LayerLicenseInfo:
Type: String
Default: MIT-0 License
Expand All @@ -17,4 +20,11 @@ Resources:
Properties:
ContentUri: s3://sam-demo-bucket/layer.zip
CompatibleRuntimes:
Ref: LayerRuntimeList
Ref: LayerRuntimeList

LayerWithNameIntrinsic:
Type: AWS::Serverless::LayerVersion
Properties:
ContentUri: s3://sam-demo-bucket/layer.zip
LayerName: !Ref LayerNameParam

34 changes: 24 additions & 10 deletions tests/translator/output/aws-cn/layers_with_intrinsics.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
{
"Parameters": {
"LayerLicenseInfo": {
"Type": "String",
"Default": "MIT-0 License"
"Default": "MIT-0 License",
"Type": "String"
},
"LayerRuntimeList": {
"Type": "CommaDelimitedList"
},
"LayerNameParam": {
"Default": "SomeLayerName",
"Type": "String"
}
},
"Resources": {
"LayerWithLicenseIntrinsic16287c50c8": {
"Type": "AWS::Lambda::LayerVersion",
"LayerWithNameIntrinsiccf8baed8b9": {
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithLicenseIntrinsic",
"LicenseInfo": {
"Ref": "LayerLicenseInfo"
}
"LayerName": "SomeLayerName"
}
},
"LayerWithRuntimesIntrinsic1a006faa85": {
"Type": "AWS::Lambda::LayerVersion",
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
Expand All @@ -36,6 +37,19 @@
"Ref": "LayerRuntimeList"
}
}
},
"LayerWithLicenseIntrinsic965c8d0c9b": {
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithLicenseIntrinsic",
"LicenseInfo": "MIT-0 License"
}
}
}
}
}

34 changes: 24 additions & 10 deletions tests/translator/output/aws-us-gov/layers_with_intrinsics.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
{
"Parameters": {
"LayerLicenseInfo": {
"Type": "String",
"Default": "MIT-0 License"
"Default": "MIT-0 License",
"Type": "String"
},
"LayerRuntimeList": {
"Type": "CommaDelimitedList"
},
"LayerNameParam": {
"Default": "SomeLayerName",
"Type": "String"
}
},
"Resources": {
"LayerWithLicenseIntrinsic16287c50c8": {
"Type": "AWS::Lambda::LayerVersion",
"LayerWithNameIntrinsiccf8baed8b9": {
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithLicenseIntrinsic",
"LicenseInfo": {
"Ref": "LayerLicenseInfo"
}
"LayerName": "SomeLayerName"
}
},
"LayerWithRuntimesIntrinsic1a006faa85": {
"Type": "AWS::Lambda::LayerVersion",
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
Expand All @@ -36,6 +37,19 @@
"Ref": "LayerRuntimeList"
}
}
},
"LayerWithLicenseIntrinsic965c8d0c9b": {
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithLicenseIntrinsic",
"LicenseInfo": "MIT-0 License"
}
}
}
}
}

34 changes: 24 additions & 10 deletions tests/translator/output/layers_with_intrinsics.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
{
"Parameters": {
"LayerLicenseInfo": {
"Type": "String",
"Default": "MIT-0 License"
"Default": "MIT-0 License",
"Type": "String"
},
"LayerRuntimeList": {
"Type": "CommaDelimitedList"
},
"LayerNameParam": {
"Default": "SomeLayerName",
"Type": "String"
}
},
"Resources": {
"LayerWithLicenseIntrinsic16287c50c8": {
"Type": "AWS::Lambda::LayerVersion",
"LayerWithNameIntrinsiccf8baed8b9": {
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithLicenseIntrinsic",
"LicenseInfo": {
"Ref": "LayerLicenseInfo"
}
"LayerName": "SomeLayerName"
}
},
"LayerWithRuntimesIntrinsic1a006faa85": {
"Type": "AWS::Lambda::LayerVersion",
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
Expand All @@ -36,6 +37,19 @@
"Ref": "LayerRuntimeList"
}
}
},
"LayerWithLicenseIntrinsic965c8d0c9b": {
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithLicenseIntrinsic",
"LicenseInfo": "MIT-0 License"
}
}
}
}
}