Skip to content

Commit d51c921

Browse files
authored
Merge pull request #4925 from opsmill/pog-change-match-logic-computed-jinja-attributes
Change the match logic for computed jinja attributes
2 parents 1a887dc + cdae5a8 commit d51c921

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

backend/infrahub/computed_attribute/tasks.py

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

backend/infrahub/core/schema/schema_branch.py

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

8080
return ["ids"]
8181

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

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

0 commit comments

Comments
 (0)