Skip to content

Commit

Permalink
moved _get_module_with_only_relevant_foreach_idx to module_handler as…
Browse files Browse the repository at this point in the history
… this is the only place where it's used
  • Loading branch information
bo156 committed Sep 20, 2023
1 parent 8b2b980 commit fa0ad45
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
17 changes: 0 additions & 17 deletions checkov/terraform/graph_builder/foreach/abstract_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,6 @@ def _build_sub_graph(self, blocks_to_render: list[int]) -> TerraformLocalGraph:
sub_graph.out_edges = self.local_graph.out_edges
return sub_graph

@staticmethod
def _get_module_with_only_relevant_foreach_idx(original_foreach_or_count_key: int | str,
original_module_key: TFModule,
tf_moudle: TFModule | None) -> TFModule | None:
if tf_moudle is None:
return None
if tf_moudle == original_module_key:
return TFModule(name=tf_moudle.name, path=tf_moudle.path,
nested_tf_module=tf_moudle.nested_tf_module,
foreach_idx=original_foreach_or_count_key)
nested_module = tf_moudle.nested_tf_module
updated_module = ForeachAbstractHandler._get_module_with_only_relevant_foreach_idx(
original_foreach_or_count_key, original_module_key, nested_module)
return TFModule(name=tf_moudle.name, path=tf_moudle.path,
nested_tf_module=updated_module,
foreach_idx=tf_moudle.foreach_idx)

@staticmethod
def _pop_foreach_attrs(attrs: dict[str, Any]) -> None:
attrs.pop(COUNT_STRING, None)
Expand Down
19 changes: 18 additions & 1 deletion checkov/terraform/graph_builder/foreach/module_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,27 @@ def _update_resolved_entry_for_tf_definition(child: TerraformBlock, original_for
resolved_module_name = config.get(RESOLVED_MODULE_ENTRY_NAME)
if resolved_module_name is not None and len(resolved_module_name) > 0:
original_definition_key = config[RESOLVED_MODULE_ENTRY_NAME][0]
tf_source_modules = ForeachAbstractHandler._get_module_with_only_relevant_foreach_idx(
tf_source_modules = ForeachModuleHandler._get_module_with_only_relevant_foreach_idx(
original_foreach_or_count_key,
original_module_key,
resolved_module_name[0].tf_source_modules,
)
config[RESOLVED_MODULE_ENTRY_NAME][0] = TFDefinitionKey(file_path=original_definition_key.file_path,
tf_source_modules=tf_source_modules)

@staticmethod
def _get_module_with_only_relevant_foreach_idx(original_foreach_or_count_key: int | str,
original_module_key: TFModule,
tf_moudle: TFModule | None) -> TFModule | None:
if tf_moudle is None:
return None
if tf_moudle == original_module_key:
return TFModule(name=tf_moudle.name, path=tf_moudle.path,
nested_tf_module=tf_moudle.nested_tf_module,
foreach_idx=original_foreach_or_count_key)
nested_module = tf_moudle.nested_tf_module
updated_module = ForeachModuleHandler._get_module_with_only_relevant_foreach_idx(
original_foreach_or_count_key, original_module_key, nested_module)
return TFModule(name=tf_moudle.name, path=tf_moudle.path,
nested_tf_module=updated_module,
foreach_idx=tf_moudle.foreach_idx)
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def test__get_module_with_only_relevant_foreach_idx():
)
original_key = TFModule(name='2', path='2', foreach_idx='2',
nested_tf_module=TFModule(name='3', path='3', foreach_idx='3', nested_tf_module=None))
result = ForeachAbstractHandler._get_module_with_only_relevant_foreach_idx('test', original_key, module)
result = ForeachModuleHandler._get_module_with_only_relevant_foreach_idx('test', original_key, module)
assert result == TFModule(name='1', path='1', foreach_idx='1',
nested_tf_module=TFModule(name='2', path='2', foreach_idx='test',
nested_tf_module=TFModule(name='3', path='3', foreach_idx='3',
Expand Down

0 comments on commit fa0ad45

Please sign in to comment.