in samtranslator/model/sam_resources.py [0:0]
def _construct_code_dict(self) -> Dict[str, Any]:
"""Constructs Lambda Code Dictionary based on the accepted SAM artifact properties such
as `InlineCode`, `CodeUri` and `ImageUri` and also raises errors if more than one of them is
defined. `PackageType` determines which artifacts are considered.
:raises InvalidResourceException when conditions on the SAM artifact properties are not met.
"""
# list of accepted artifacts
packagetype = self.PackageType or ZIP
artifacts = {}
if packagetype == ZIP:
artifacts = {"InlineCode": self.InlineCode, "CodeUri": self.CodeUri}
elif packagetype == IMAGE:
artifacts = {"ImageUri": self.ImageUri}
if packagetype not in [ZIP, IMAGE]:
raise InvalidResourceException(self.logical_id, f"invalid 'PackageType' : {packagetype}")
# Inline function for transformation of inline code.
# It accepts arbitrary argumemnts, because the arguments do not matter for the result.
def _construct_inline_code(*args: Any, **kwargs: Dict[str, Any]) -> Dict[str, Any]:
return {"ZipFile": self.InlineCode}
# dispatch mechanism per artifact on how it needs to be transformed.
artifact_dispatch: Dict[str, Callable[..., Dict[str, Any]]] = {
"InlineCode": _construct_inline_code,
"CodeUri": construct_s3_location_object,
"ImageUri": construct_image_code_object,
}
filtered_artifacts = dict(filter(lambda x: x[1] is not None, artifacts.items()))
# There are more than one allowed artifact types present, raise an Error.
# There are no valid artifact types present, also raise an Error.
if len(filtered_artifacts) > 1 or len(filtered_artifacts) == 0:
if packagetype == ZIP and len(filtered_artifacts) == 0:
raise InvalidResourceException(self.logical_id, "Only one of 'InlineCode' or 'CodeUri' can be set.")
if packagetype == IMAGE:
raise InvalidResourceException(self.logical_id, "'ImageUri' must be set.")
filtered_keys = list(filtered_artifacts.keys())
# NOTE(sriram-mv): This precedence order is important. It is protect against python2 vs python3
# dictionary ordering when getting the key values with .keys() on a dictionary.
# Do not change this precedence order.
if "InlineCode" in filtered_keys:
filtered_key = "InlineCode"
elif "CodeUri" in filtered_keys:
filtered_key = "CodeUri"
elif "ImageUri" in filtered_keys:
filtered_key = "ImageUri"
else:
raise InvalidResourceException(self.logical_id, "Either 'InlineCode' or 'CodeUri' must be set.")
dispatch_function: Callable[..., Dict[str, Any]] = artifact_dispatch[filtered_key]
code_dict = dispatch_function(artifacts[filtered_key], self.logical_id, filtered_key)
if self.SourceKMSKeyArn and packagetype == ZIP:
code_dict["SourceKMSKeyArn"] = self.SourceKMSKeyArn
return code_dict