8
8
9
9
# input->(list ["a.b","c", "d"],value of resource)
10
10
# returns->[any, any, any]
11
+ from typing import Iterable , Tuple
11
12
import pydash
12
13
13
14
from ..common import ProviderError
@@ -339,33 +340,54 @@ def direct_references_operator_referenced_by(input_data: dict, provider_inputs:
339
340
continue
340
341
341
342
# 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 ):
343
344
for expression_val_dict in resource_config .get ("expressions" , {}).values ():
344
345
if not isinstance (expression_val_dict , dict ):
345
346
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 } "
348
352
if reference_address in reference_target_addresses :
349
353
reference_target_addresses .remove (reference_address )
350
354
351
355
is_all_referenced = len (reference_target_addresses ) == 0
352
356
outputs .append ({"value" : is_all_referenced , "meta" : config_resources })
353
357
354
358
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 ]]:
356
382
"""
357
383
Get all of the resource config by type
358
384
359
385
:param input_data: The input data
360
386
:param resource_type: The resource type
361
387
:return: The resource config (iterable)
362
388
"""
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 )
369
391
370
392
371
393
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:
389
411
resource_change_address = resource_change .get ("address" )
390
412
391
413
# 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 ):
393
416
if resource_config .get ("address" ) != resource_change_address :
394
417
continue
395
418
for expression_val_dict in resource_config .get ("expressions" , {}).values ():
0 commit comments