Skip to content

Commit b5a499f

Browse files
committed
Change the match logic for computed jinja attributes
1 parent 380ec49 commit b5a499f

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

backend/infrahub/computed_attribute/tasks.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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:

backend/infrahub/core/schema/schema_branch.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ def node_filters(self) -> list[str]:
7878

7979
return ["ids"]
8080

81+
def __hash__(self):
82+
return hash((self.kind, self.attribute, tuple(self.filter_keys)))
83+
8184

8285
class RegisteredNodeComputedAttribute(BaseModel):
8386
local_fields: dict[str, list[ComputedAttributeTarget]] = Field(

0 commit comments

Comments
 (0)