1- from typing import Any , Dict , List
1+ import copy
2+ from typing import Any , Dict , List , Optional , Union
23
3- from samtranslator .model .exceptions import ExceptionWithMessage
4+ from samtranslator .model .exceptions import ExceptionWithMessage , InvalidResourceAttributeTypeException
45from samtranslator .public .intrinsics import is_intrinsics
56from samtranslator .public .sdk .resource import SamResourceType
67from samtranslator .swagger .swagger import SwaggerEditor
@@ -99,7 +100,7 @@ class Globals:
99100 SamResourceType .Function .value : ["RuntimeManagementConfig" ],
100101 }
101102
102- def __init__ (self , template ) -> None : # type: ignore[no-untyped-def]
103+ def __init__ (self , template : Dict [ str , Any ] ) -> None :
103104 """
104105 Constructs an instance of this object
105106
@@ -111,12 +112,57 @@ def __init__(self, template) -> None: # type: ignore[no-untyped-def]
111112 # Sort the names for stability in list ordering
112113 self .supported_resource_section_names .sort ()
113114
114- self .template_globals = {}
115+ self .template_globals : Dict [ str , GlobalProperties ] = {}
115116
116117 if self ._KEYWORD in template :
117118 self .template_globals = self ._parse (template [self ._KEYWORD ]) # type: ignore[no-untyped-call]
118119
119- def merge (self , resource_type , resource_properties ): # type: ignore[no-untyped-def]
120+ def get_template_globals (
121+ self , logical_id : str , resource_type : str , ignore_globals : Optional [Union [str , List [str ]]]
122+ ) -> "GlobalProperties" :
123+ """
124+ Get template globals but remove globals based on IgnoreGlobals attribute.
125+
126+ :param string logical_id: LogicalId of the resource
127+ :param string resource_type: Type of the resource (Ex: AWS::Serverless::Function)
128+ :param dict ignore_globals: IgnoreGlobals resource attribute. It can be either 1) "*" string value
129+ or list of string value, each value should be a valid property in Globals section
130+ :return dict: processed template globals
131+ """
132+ if not ignore_globals :
133+ return self .template_globals [resource_type ]
134+
135+ if isinstance (ignore_globals , str ) and ignore_globals == "*" :
136+ return GlobalProperties ({})
137+
138+ if isinstance (ignore_globals , list ):
139+ global_props : GlobalProperties = copy .deepcopy (self .template_globals [resource_type ])
140+ for key in ignore_globals :
141+ if key not in global_props .global_properties :
142+ raise InvalidResourceAttributeTypeException (
143+ logical_id ,
144+ "IgnoreGlobals" ,
145+ None ,
146+ f"Resource { logical_id } has invalid resource attribute 'IgnoreGlobals' on item '{ key } '." ,
147+ )
148+ del global_props .global_properties [key ]
149+ return global_props
150+
151+ # We raise exception for any non "*" or non-list input
152+ raise InvalidResourceAttributeTypeException (
153+ logical_id ,
154+ "IgnoreGlobals" ,
155+ None ,
156+ f"Resource { logical_id } has invalid resource attribute 'IgnoreGlobals'." ,
157+ )
158+
159+ def merge (
160+ self ,
161+ resource_type : str ,
162+ resource_properties : Dict [str , Any ],
163+ logical_id : str = "" ,
164+ ignore_globals : Optional [Union [str , List [str ]]] = None ,
165+ ) -> Any :
120166 """
121167 Adds global properties to the resource, if necessary. This method is a no-op if there are no global properties
122168 for this resource type
@@ -130,12 +176,12 @@ def merge(self, resource_type, resource_properties): # type: ignore[no-untyped-
130176 # Nothing to do. Return the template unmodified
131177 return resource_properties
132178
133- global_props = self .template_globals [ resource_type ]
179+ global_props = self .get_template_globals ( logical_id , str ( resource_type ), ignore_globals )
134180
135- return global_props .merge (resource_properties )
181+ return global_props .merge (resource_properties ) # type: ignore[no-untyped-call]
136182
137183 @classmethod
138- def del_section (cls , template ): # type: ignore[no-untyped-def]
184+ def del_section (cls , template : Dict [ str , Any ]) -> None :
139185 """
140186 Helper method to delete the Globals section altogether from the template
141187
0 commit comments