-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
resource_metadata_normalizer.py
65 lines (51 loc) · 1.99 KB
/
resource_metadata_normalizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Class that Normalizes a Template based on Resource Metadata
"""
import logging
RESOURCES_KEY = "Resources"
PROPERTIES_KEY = "Properties"
METADATA_KEY = "Metadata"
ASSET_PATH_METADATA_KEY = "aws:asset:path"
ASSET_PROPERTY_METADATA_KEY = "aws:asset:property"
LOG = logging.getLogger(__name__)
class ResourceMetadataNormalizer:
@staticmethod
def normalize(template_dict):
"""
Normalize all Resources in the template with the Metadata Key on the resource.
This method will mutate the template
Parameters
----------
template_dict dict
Dictionary representing the template
"""
resources = template_dict.get(RESOURCES_KEY, {})
for logical_id, resource in resources.items():
resource_metadata = resource.get(METADATA_KEY, {})
asset_path = resource_metadata.get(ASSET_PATH_METADATA_KEY)
asset_property = resource_metadata.get(ASSET_PROPERTY_METADATA_KEY)
ResourceMetadataNormalizer._replace_property(asset_property, asset_path, resource, logical_id)
@staticmethod
def _replace_property(property_key, property_value, resource, logical_id):
"""
Replace a property with an asset on a given resource
This method will mutate the template
Parameters
----------
property str
The property to replace on the resource
property_value str
The new value of the property
resource dict
Dictionary representing the Resource to change
logical_id str
LogicalId of the Resource
"""
if property_key and property_value:
resource.get(PROPERTIES_KEY, {})[property_key] = property_value
elif property_key or property_value:
LOG.info(
"WARNING: Ignoring Metadata for Resource %s. Metadata contains only aws:asset:path or "
"aws:assert:property but not both",
logical_id,
)