@@ -218,28 +218,30 @@ async def computed_attribute_setup() -> None:
218218 automations = await client .read_automations ()
219219 existing_computed_attr_automations = ComputedAttributeAutomations .from_prefect (automations = automations )
220220
221- computed_attributes : dict [str , ComputedAttributeTarget ] = {}
222- for item in schema_branch . _computed_jinja2_attribute_map . values ():
223- for attrs in list ( item . local_fields . values ()) + list ( item . relationships . values () ):
224- for attr in attrs :
225- if attr . key_name in computed_attributes :
226- continue
227- log . info ( f"found { attr . key_name } " )
228- computed_attributes [ attr . key_name ] = attr
229-
230- for identifier , computed_attribute in computed_attributes .items ():
221+ mapping : dict [ComputedAttributeTarget , set [ str ] ] = {}
222+
223+ for node , registered_computed_attribute in schema_branch . _computed_jinja2_attribute_map . items ( ):
224+ for local_fields in registered_computed_attribute . local_fields . values () :
225+ for local_field in local_fields :
226+ if local_field not in mapping :
227+ mapping [ local_field ] = set ( )
228+ mapping [ local_field ]. add ( node )
229+
230+ for computed_attribute , source_node_types in mapping .items ():
231231 log .info (f"processing { computed_attribute .key_name } " )
232232 scope = "default"
233233
234234 automation = AutomationCore (
235- name = AUTOMATION_NAME .format (prefix = AUTOMATION_NAME_PREFIX , identifier = identifier , scope = scope ),
236- description = f"Process value of the computed attribute for { identifier } [{ scope } ]" ,
235+ name = AUTOMATION_NAME .format (
236+ prefix = AUTOMATION_NAME_PREFIX , identifier = computed_attribute .key_name , scope = scope
237+ ),
238+ description = f"Process value of the computed attribute for { computed_attribute .key_name } [{ scope } ]" ,
237239 enabled = True ,
238240 trigger = EventTrigger (
239241 posture = Posture .Reactive ,
240242 expect = {"infrahub.node.*" },
241243 within = timedelta (0 ),
242- match = ResourceSpecification ({"infrahub.node.kind" : [ computed_attribute . kind ] }),
244+ match = ResourceSpecification ({"infrahub.node.kind" : list ( source_node_types ) }),
243245 threshold = 1 ,
244246 ),
245247 actions = [
@@ -258,8 +260,8 @@ async def computed_attribute_setup() -> None:
258260 ],
259261 )
260262
261- if existing_computed_attr_automations .has (identifier = identifier , scope = scope ):
262- existing = existing_computed_attr_automations .get (identifier = identifier , scope = scope )
263+ if existing_computed_attr_automations .has (identifier = computed_attribute . key_name , scope = scope ):
264+ existing = existing_computed_attr_automations .get (identifier = computed_attribute . key_name , scope = scope )
263265 await client .update_automation (automation_id = existing .id , automation = automation )
264266 log .info (f"{ computed_attribute .key_name } Updated" )
265267 else :
0 commit comments