Skip to content

Commit

Permalink
Added special handling for Leaf Constructs (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
ammokhov committed Mar 11, 2024
1 parent 36b5d75 commit 61b46bf
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 7 deletions.
45 changes: 45 additions & 0 deletions src/rpdk/guard_rail/core/stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class DIFFKEYS:
"oneOf",
}

cfn_leaf_level_constructs = {
"relationshipRef",
"insertionOrder",
"arrayType",
}

native_constructs = {
"type",
"description",
Expand All @@ -78,6 +84,7 @@ class DIFFKEYS:
"contains",
"items",
"additionalProperties",
"uniqueItems",
}


Expand Down Expand Up @@ -112,6 +119,7 @@ def _is_resource_property(path_list):
len(path_list) > 0
and path_list[0] == PROPERTIES
and path_list[-1] not in native_constructs
and path_list[-1] not in cfn_leaf_level_constructs
)


Expand All @@ -125,6 +133,11 @@ def _is_json_construct(path_list):
return len(path_list) > 0 and path_list[-1] in native_constructs


def _is_cfn_leaf_construct(path_list):
"""This method defines json constructs"""
return len(path_list) > 0 and path_list[-1] in cfn_leaf_level_constructs


def _get_path(path_list):
"""This method converts array into schema path notation"""
return "/".join([""] + path_list)
Expand Down Expand Up @@ -166,6 +179,13 @@ def _traverse_nested_properties(
diff_value (Any): arbitrary value
"""

# cfn might have more custom leaf level props
# if not specifically added to cfn_leaf_level_constructs
# it could be traversed further and interpreted as a property
# this hedges out from lookup/attribute/unbounded exceptions
if not diff_value or not isinstance(diff_value, dict):
return

# if type is absent, then we are dealing with properties
if "type" not in diff_value:
for property_name, property_definition in diff_value.items():
Expand Down Expand Up @@ -236,6 +256,13 @@ def __translate_iter_added_diff(diffkey, schema_meta_diff, diff_value):
diffkey,
value,
)
if _is_cfn_leaf_construct(path_list):
_add_item(
schema_meta_diff,
path_list[-1],
diffkey,
value,
)

# using partial to avoid code repetition
append_added = partial(__translate_iter_added_diff, DIFFKEYS.ADDED)
Expand Down Expand Up @@ -285,6 +312,13 @@ def __translate_dict_diff(diffkey, schema_meta_diff, diff_value):
diffkey,
_get_path(path_list[:-1]),
)
if _is_cfn_leaf_construct(path_list):
_add_item(
schema_meta_diff,
path_list[-1],
diffkey,
_get_path(path_list[:-1]),
)

# using partial to avoid code repetition
append_added = partial(__translate_dict_diff, DIFFKEYS.ADDED)
Expand Down Expand Up @@ -336,6 +370,17 @@ def _translate_values_changed_diff_(schema_meta_diff, diff_value):
DIFFKEYS.NEW_VALUE: value[DIFFKEYS.NEW_VALUE],
},
)
if _is_cfn_leaf_construct(path_list):
_add_item(
schema_meta_diff,
path_list[-1],
DIFFKEYS.CHANGED,
{
DIFFKEYS.PROPERTY: _get_path(path_list[:-1]),
DIFFKEYS.OLD_VALUE: value[DIFFKEYS.OLD_VALUE],
DIFFKEYS.NEW_VALUE: value[DIFFKEYS.NEW_VALUE],
},
)


def _translate_meta_diff(iterable: Iterable):
Expand Down
Loading

0 comments on commit 61b46bf

Please sign in to comment.