Skip to content

Commit

Permalink
Support Ignore Globals resource attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinZZ committed Oct 19, 2023
1 parent 4fac468 commit ead1a95
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ class Properties(BaseModel):
SnapStart: Optional[SnapStart] = prop("SnapStart")
RuntimeManagementConfig: Optional[RuntimeManagementConfig] = prop("RuntimeManagementConfig")
Tags: Optional[Tags] = prop("Tags")
PropagateTags: Optional[bool] # TODO: add docs
PropagateTags: Optional[bool] = prop("PropagateTags")
Timeout: Optional[Timeout] = prop("Timeout")
Tracing: Optional[Tracing] = prop("Tracing")
VersionDescription: Optional[PassThroughProp] = prop("VersionDescription")
Expand Down Expand Up @@ -656,7 +656,7 @@ class Globals(BaseModel):
["AWS::Lambda::Function", "Properties", "Environment"],
)
Tags: Optional[Tags] = prop("Tags")
PropagateTags: Optional[bool] # TODO: add docs
PropagateTags: Optional[bool] = prop("PropagateTags")
Tracing: Optional[Tracing] = prop("Tracing")
KmsKeyArn: Optional[KmsKeyArn] = prop("KmsKeyArn")
Layers: Optional[Layers] = prop("Layers")
Expand Down
1 change: 1 addition & 0 deletions samtranslator/internal/schema_source/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ class ResourceAttributes(BaseModel):
Metadata: Optional[PassThroughProp]
UpdateReplacePolicy: Optional[PassThroughProp]
Condition: Optional[PassThroughProp]
IgnoreGlobals: Optional[bool]
1 change: 1 addition & 0 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class SamFunction(SamResourceMacro):
Environment: Optional[Dict[str, Any]]
Events: Optional[Dict[str, Any]]
Tags: Optional[Dict[str, Any]]
IgnoreGlobals: Optional[bool]
PropagateTags: Optional[bool]
Tracing: Optional[Intrinsicable[str]]
KmsKeyArn: Optional[Intrinsicable[str]]
Expand Down
4 changes: 2 additions & 2 deletions samtranslator/plugins/globals/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Globals:
SamResourceType.Function.value: ["RuntimeManagementConfig"],
}

def __init__(self, template) -> None: # type: ignore[no-untyped-def]
def __init__(self, template: Dict[str, Any]) -> None:
"""
Constructs an instance of this object
Expand Down Expand Up @@ -135,7 +135,7 @@ def merge(self, resource_type, resource_properties): # type: ignore[no-untyped-
return global_props.merge(resource_properties)

@classmethod
def del_section(cls, template): # type: ignore[no-untyped-def]
def del_section(cls, template: Dict[str, Any]) -> None:
"""
Helper method to delete the Globals section altogether from the template
Expand Down
12 changes: 9 additions & 3 deletions samtranslator/plugins/globals/globals_plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict

from samtranslator.metrics.method_decorator import cw_timer
from samtranslator.plugins.globals.globals import Globals, InvalidGlobalsSectionException
from samtranslator.public.exceptions import InvalidDocumentException
Expand All @@ -13,7 +15,7 @@ class GlobalsPlugin(BasePlugin):
"""

@cw_timer(prefix="Plugin-Globals")
def on_before_transform_template(self, template_dict): # type: ignore[no-untyped-def]
def on_before_transform_template(self, template_dict: Dict[str, Any]) -> None:
"""
Hook method that runs before a template gets transformed. In this method, we parse and process Globals section
from the template (if present).
Expand All @@ -28,11 +30,15 @@ def on_before_transform_template(self, template_dict): # type: ignore[no-untype
# For each resource in template, try and merge with Globals if necessary
template = SamTemplate(template_dict)
for logicalId, resource in template.iterate():
resource.properties = global_section.merge(resource.type, resource.properties) # type: ignore[no-untyped-call]
# If IgnoreGlobals resource attribute is set to True, we would ignore
# globals for this resource type and use its own properties definition
if resource.ignore_globals:
continue
resource.properties = global_section.merge(str(resource.type), resource.properties)
template.set(logicalId, resource)

# Remove the Globals section from template if necessary
Globals.del_section(template_dict) # type: ignore[no-untyped-call]
Globals.del_section(template_dict)

# If there was a global openApiVersion flag, check and convert swagger
# to the right version
Expand Down
42 changes: 40 additions & 2 deletions samtranslator/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -249780,6 +249780,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -252731,6 +252735,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -253032,6 +253040,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -253116,6 +253128,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -253613,7 +253629,8 @@
"type": "string"
},
"PropagateTags": {
"title": "Propagatetags",
"markdownDescription": "Indicate whether or not to pass tags from the `Tags` property to your [AWS::Serverless::Function](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-generated-resources-function.html) generated resources\\. Specify `True` to propagate tags in your generated resources\\. \n*Type*: Boolean \n*Required*: No \n*Default*: `False` \n*AWS CloudFormation compatibility*: This property is unique to AWS SAM and doesn't have an AWS CloudFormation equivalent\\.",
"title": "PropagateTags",
"type": "boolean"
},
"ProvisionedConcurrencyConfig": {
Expand Down Expand Up @@ -253992,7 +254009,8 @@
"title": "Policies"
},
"PropagateTags": {
"title": "Propagatetags",
"markdownDescription": "Indicate whether or not to pass tags from the `Tags` property to your [AWS::Serverless::Function](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-generated-resources-function.html) generated resources\\. Specify `True` to propagate tags in your generated resources\\. \n*Type*: Boolean \n*Required*: No \n*Default*: `False` \n*AWS CloudFormation compatibility*: This property is unique to AWS SAM and doesn't have an AWS CloudFormation equivalent\\.",
"title": "PropagateTags",
"type": "boolean"
},
"ProvisionedConcurrencyConfig": {
Expand Down Expand Up @@ -254121,6 +254139,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -255040,6 +255062,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -255198,6 +255224,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -255292,6 +255322,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -255839,6 +255873,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down
1 change: 1 addition & 0 deletions samtranslator/sdk/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, resource_dict: Dict[str, Any]) -> None:
self.condition = resource_dict.get("Condition", None)
self.deletion_policy = resource_dict.get("DeletionPolicy", None)
self.update_replace_policy = resource_dict.get("UpdateReplacePolicy", None)
self.ignore_globals = resource_dict.get("IgnoreGlobals", False)

# Properties is *not* required. Ex: SimpleTable resource has no required properties
self.properties = resource_dict.get("Properties", {})
Expand Down
42 changes: 40 additions & 2 deletions schema_source/sam.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -4334,6 +4338,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -4716,6 +4724,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -4800,6 +4812,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -5364,7 +5380,8 @@
"title": "PermissionsBoundary"
},
"PropagateTags": {
"title": "Propagatetags",
"markdownDescription": "Indicate whether or not to pass tags from the `Tags` property to your [AWS::Serverless::Function](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-generated-resources-function.html) generated resources\\. Specify `True` to propagate tags in your generated resources\\. \n*Type*: Boolean \n*Required*: No \n*Default*: `False` \n*AWS CloudFormation compatibility*: This property is unique to AWS SAM and doesn't have an AWS CloudFormation equivalent\\.",
"title": "PropagateTags",
"type": "boolean"
},
"ProvisionedConcurrencyConfig": {
Expand Down Expand Up @@ -5920,7 +5937,8 @@
"title": "Policies"
},
"PropagateTags": {
"title": "Propagatetags",
"markdownDescription": "Indicate whether or not to pass tags from the `Tags` property to your [AWS::Serverless::Function](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-generated-resources-function.html) generated resources\\. Specify `True` to propagate tags in your generated resources\\. \n*Type*: Boolean \n*Required*: No \n*Default*: `False` \n*AWS CloudFormation compatibility*: This property is unique to AWS SAM and doesn't have an AWS CloudFormation equivalent\\.",
"title": "PropagateTags",
"type": "boolean"
},
"ProvisionedConcurrencyConfig": {
Expand Down Expand Up @@ -6091,6 +6109,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -7010,6 +7032,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -7218,6 +7244,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -7368,6 +7398,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down Expand Up @@ -7915,6 +7949,10 @@
"DependsOn": {
"$ref": "#/definitions/PassThroughProp"
},
"IgnoreGlobals": {
"title": "Ignoreglobals",
"type": "boolean"
},
"Metadata": {
"$ref": "#/definitions/PassThroughProp"
},
Expand Down
21 changes: 21 additions & 0 deletions tests/translator/input/function_with_ignore_globals.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Globals:
Function:
Runtime: python3.8
Handler: index.lambda_handler
MemorySize: 128

Resources:
ZipFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://bucket/key

ImageFunction:
Type: AWS::Serverless::Function
IgnoreGlobals: true
Properties:
PackageType: Image
ImageUri: s3://bucket/key
Metadata:
Dockerfile: Dockerfile
DockerContext: ./src/image

0 comments on commit ead1a95

Please sign in to comment.