@@ -215,28 +215,30 @@ async def computed_attribute_setup() -> None:
215215 automations = await client .read_automations ()
216216 existing_computed_attr_automations = ComputedAttributeAutomations .from_prefect (automations = automations )
217217
218- computed_attributes : dict [str , ComputedAttributeTarget ] = {}
219- for item in schema_branch ._computed_jinja2_attribute_map .values ():
220- for attrs in list (item .local_fields .values ()) + list (item .relationships .values ()):
221- for attr in attrs :
222- if attr .key_name in computed_attributes :
223- continue
224- log .info (f"found { attr .key_name } " )
225- computed_attributes [attr .key_name ] = attr
218+ mapping : dict [ComputedAttributeTarget , set [str ]] = {}
226219
227- for identifier , computed_attribute in computed_attributes .items ():
220+ for node , registered_computed_attribute in schema_branch ._computed_jinja2_attribute_map .items ():
221+ for local_fields in registered_computed_attribute .local_fields .values ():
222+ for local_field in local_fields :
223+ if local_field not in mapping :
224+ mapping [local_field ] = set ()
225+ mapping [local_field ].add (node )
226+
227+ for computed_attribute , source_node_types in mapping .items ():
228228 log .info (f"processing { computed_attribute .key_name } " )
229229 scope = "default"
230230
231231 automation = AutomationCore (
232- name = AUTOMATION_NAME .format (prefix = AUTOMATION_NAME_PREFIX , identifier = identifier , scope = scope ),
233- description = f"Process value of the computed attribute for { identifier } [{ scope } ]" ,
232+ name = AUTOMATION_NAME .format (
233+ prefix = AUTOMATION_NAME_PREFIX , identifier = computed_attribute .key_name , scope = scope
234+ ),
235+ description = f"Process value of the computed attribute for { computed_attribute .key_name } [{ scope } ]" ,
234236 enabled = True ,
235237 trigger = EventTrigger (
236238 posture = Posture .Reactive ,
237239 expect = {"infrahub.node.*" },
238240 within = timedelta (0 ),
239- match = ResourceSpecification ({"infrahub.node.kind" : [ computed_attribute . kind ] }),
241+ match = ResourceSpecification ({"infrahub.node.kind" : list ( source_node_types ) }),
240242 threshold = 1 ,
241243 ),
242244 actions = [
@@ -255,8 +257,8 @@ async def computed_attribute_setup() -> None:
255257 ],
256258 )
257259
258- if existing_computed_attr_automations .has (identifier = identifier , scope = scope ):
259- existing = existing_computed_attr_automations .get (identifier = identifier , scope = scope )
260+ if existing_computed_attr_automations .has (identifier = computed_attribute . key_name , scope = scope ):
261+ existing = existing_computed_attr_automations .get (identifier = computed_attribute . key_name , scope = scope )
260262 await client .update_automation (automation_id = existing .id , automation = automation )
261263 log .info (f"{ computed_attribute .key_name } Updated" )
262264 else :
0 commit comments