Skip to content

Commit 51a89a5

Browse files
committed
terraform_plan/referenced_by: Get references from modules recursively
1 parent acfdaec commit 51a89a5

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

src/tirith/providers/terraform_plan/handler.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# input->(list ["a.b","c", "d"],value of resource)
1010
# returns->[any, any, any]
11+
from typing import Iterable, Tuple
1112
import pydash
1213

1314
from ..common import ProviderError
@@ -339,33 +340,54 @@ def direct_references_operator_referenced_by(input_data: dict, provider_inputs:
339340
continue
340341

341342
# Look to the resource_config to get the references
342-
for resource_config in get_resource_config_by_type(input_data, referenced_by):
343+
for resource_config, module_path in get_resource_config_by_type(input_data, referenced_by):
343344
for expression_val_dict in resource_config.get("expressions", {}).values():
344345
if not isinstance(expression_val_dict, dict):
345346
continue
346-
347-
for reference_address in expression_val_dict.get("references", []):
347+
for relative_reference_address in expression_val_dict.get("references", []):
348+
if module_path == "":
349+
reference_address = relative_reference_address
350+
else:
351+
reference_address = f"{module_path}.{relative_reference_address}"
348352
if reference_address in reference_target_addresses:
349353
reference_target_addresses.remove(reference_address)
350354

351355
is_all_referenced = len(reference_target_addresses) == 0
352356
outputs.append({"value": is_all_referenced, "meta": config_resources})
353357

354358

355-
def get_resource_config_by_type(input_data: dict, resource_type: str) -> iter:
359+
def get_module_resources_by_type_recursive(module: dict, resource_type: str, current_module_path: str = "") -> iter:
360+
"""
361+
Recursively retrieves all resources of a given type within a module.
362+
363+
:param module: The module to search for resources.
364+
:param resource_type: The type of resources to retrieve.
365+
:yield: dict: A resource of the specified type.
366+
367+
"""
368+
for resource in module.get("resources", []):
369+
if resource.get("type") == resource_type:
370+
yield resource, current_module_path
371+
for module_name, module_call in module.get("module_calls", {}).items():
372+
yield from get_module_resources_by_type_recursive(
373+
module_call.get("module", {}),
374+
resource_type,
375+
current_module_path=(
376+
f"{current_module_path}.module.{module_name}" if current_module_path else f"module.{module_name}"
377+
),
378+
)
379+
380+
381+
def get_resource_config_by_type(input_data: dict, resource_type: str) -> Iterable[Tuple[dict, str]]:
356382
"""
357383
Get all of the resource config by type
358384
359385
:param input_data: The input data
360386
:param resource_type: The resource type
361387
:return: The resource config (iterable)
362388
"""
363-
config_resources = input_data.get("configuration", {}).get("root_module", {}).get("resources", [])
364-
365-
for config_resource in config_resources:
366-
if config_resource.get("type") != resource_type:
367-
continue
368-
yield config_resource
389+
root_module = input_data.get("configuration", {}).get("root_module", {})
390+
yield from get_module_resources_by_type_recursive(root_module, resource_type)
369391

370392

371393
def direct_references_operator_references_to(input_data: dict, provider_inputs: dict, outputs: list):
@@ -389,7 +411,8 @@ def direct_references_operator_references_to(input_data: dict, provider_inputs:
389411
resource_change_address = resource_change.get("address")
390412

391413
# Look to the resource_config to get the references
392-
for resource_config in get_resource_config_by_type(input_data, resource_type):
414+
# TODO: Use the module_path
415+
for resource_config, module_path in get_resource_config_by_type(input_data, resource_type):
393416
if resource_config.get("address") != resource_change_address:
394417
continue
395418
for expression_val_dict in resource_config.get("expressions", {}).values():

0 commit comments

Comments
 (0)