Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions backend/infrahub/computed_attribute/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,28 +215,30 @@ async def computed_attribute_setup() -> None:
automations = await client.read_automations()
existing_computed_attr_automations = ComputedAttributeAutomations.from_prefect(automations=automations)

computed_attributes: dict[str, ComputedAttributeTarget] = {}
for item in schema_branch._computed_jinja2_attribute_map.values():
for attrs in list(item.local_fields.values()) + list(item.relationships.values()):
for attr in attrs:
if attr.key_name in computed_attributes:
continue
log.info(f"found {attr.key_name}")
computed_attributes[attr.key_name] = attr
mapping: dict[ComputedAttributeTarget, set[str]] = {}

for identifier, computed_attribute in computed_attributes.items():
for node, registered_computed_attribute in schema_branch._computed_jinja2_attribute_map.items():
for local_fields in registered_computed_attribute.local_fields.values():
for local_field in local_fields:
if local_field not in mapping:
mapping[local_field] = set()
mapping[local_field].add(node)

for computed_attribute, source_node_types in mapping.items():
log.info(f"processing {computed_attribute.key_name}")
scope = "default"

automation = AutomationCore(
name=AUTOMATION_NAME.format(prefix=AUTOMATION_NAME_PREFIX, identifier=identifier, scope=scope),
description=f"Process value of the computed attribute for {identifier} [{scope}]",
name=AUTOMATION_NAME.format(
prefix=AUTOMATION_NAME_PREFIX, identifier=computed_attribute.key_name, scope=scope
),
description=f"Process value of the computed attribute for {computed_attribute.key_name} [{scope}]",
enabled=True,
trigger=EventTrigger(
posture=Posture.Reactive,
expect={"infrahub.node.*"},
within=timedelta(0),
match=ResourceSpecification({"infrahub.node.kind": [computed_attribute.kind]}),
match=ResourceSpecification({"infrahub.node.kind": list(source_node_types)}),
threshold=1,
),
actions=[
Expand All @@ -255,8 +257,8 @@ async def computed_attribute_setup() -> None:
],
)

if existing_computed_attr_automations.has(identifier=identifier, scope=scope):
existing = existing_computed_attr_automations.get(identifier=identifier, scope=scope)
if existing_computed_attr_automations.has(identifier=computed_attribute.key_name, scope=scope):
existing = existing_computed_attr_automations.get(identifier=computed_attribute.key_name, scope=scope)
await client.update_automation(automation_id=existing.id, automation=automation)
log.info(f"{computed_attribute.key_name} Updated")
else:
Expand Down
3 changes: 3 additions & 0 deletions backend/infrahub/core/schema/schema_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def node_filters(self) -> list[str]:

return ["ids"]

def __hash__(self):
return hash((self.kind, self.attribute, tuple(self.filter_keys)))


class RegisteredNodeComputedAttribute(BaseModel):
local_fields: dict[str, list[ComputedAttributeTarget]] = Field(
Expand Down
Loading