Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added special handling for Leaf Constructs #47

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading