Skip to content

Commit 927ff90

Browse files
authored
Merge pull request #5758 from opsmill/pog-add-context-to-triggers-IFC-1244
Add context to triggers/automations
2 parents 8b522b5 + dbf932b commit 927ff90

File tree

19 files changed

+333
-42
lines changed

19 files changed

+333
-42
lines changed

backend/infrahub/api/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ async def load_schema(
384384
request_id=request_id,
385385
account_id=account_session.account_id,
386386
branch=branch,
387+
context=context,
387388
),
388389
)
389390
await service.event.send(event=event)

backend/infrahub/computed_attribute/tasks.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ async def trigger_update_python_computed_attributes(
170170
"object_id": node.id,
171171
"computed_attribute_name": computed_attribute_name,
172172
"computed_attribute_kind": computed_attribute_kind,
173+
"context": context,
173174
},
174175
)
175176

@@ -315,6 +316,7 @@ async def trigger_update_jinja2_computed_attributes(
315316
"computed_attribute_kind": computed_attribute_kind,
316317
"node_kind": computed_attribute_kind,
317318
"object_id": node.id,
319+
"context": context,
318320
},
319321
)
320322

@@ -383,6 +385,13 @@ async def computed_attribute_setup(
383385
"computed_attribute_name": computed_attribute.attribute.name,
384386
"computed_attribute_kind": computed_attribute.kind,
385387
"updated_fields": "{{ event.payload['fields'] | tojson }}",
388+
"context": {
389+
"__prefect_kind": "json",
390+
"value": {
391+
"__prefect_kind": "jinja",
392+
"template": "{{ event.payload['context'] | tojson }}",
393+
},
394+
},
386395
},
387396
job_variables={},
388397
)
@@ -407,6 +416,7 @@ async def computed_attribute_setup(
407416
"branch_name": registry.default_branch,
408417
"computed_attribute_name": computed_attribute.attribute.name,
409418
"computed_attribute_kind": computed_attribute.kind,
419+
"context": context,
410420
},
411421
)
412422

@@ -448,6 +458,13 @@ async def computed_attribute_setup(
448458
"computed_attribute_name": computed_attribute.attribute.name,
449459
"computed_attribute_kind": computed_attribute.kind,
450460
"updated_fields": "{{ event.payload['fields'] | tojson }}",
461+
"context": {
462+
"__prefect_kind": "json",
463+
"value": {
464+
"__prefect_kind": "jinja",
465+
"template": "{{ event.payload['context'] | tojson }}",
466+
},
467+
},
451468
},
452469
job_variables={},
453470
)
@@ -474,6 +491,7 @@ async def computed_attribute_setup(
474491
"branch_name": branch_name,
475492
"computed_attribute_name": computed_attribute.attribute.name,
476493
"computed_attribute_kind": computed_attribute.kind,
494+
"context": context,
477495
},
478496
)
479497

@@ -565,6 +583,13 @@ async def computed_attribute_setup_python(
565583
"object_id": "{{ event.resource['infrahub.node.id'] }}",
566584
"computed_attribute_name": computed_attribute.computed_attribute.attribute.name,
567585
"computed_attribute_kind": computed_attribute.computed_attribute.kind,
586+
"context": {
587+
"__prefect_kind": "json",
588+
"value": {
589+
"__prefect_kind": "jinja",
590+
"template": "{{ event.payload['context'] | tojson }}",
591+
},
592+
},
568593
},
569594
job_variables={},
570595
)
@@ -613,6 +638,13 @@ async def computed_attribute_setup_python(
613638
"branch_name": "{{ event.resource['infrahub.branch.name'] }}",
614639
"node_kind": "{{ event.resource['infrahub.node.kind'] }}",
615640
"object_id": "{{ event.resource['infrahub.node.id'] }}",
641+
"context": {
642+
"__prefect_kind": "json",
643+
"value": {
644+
"__prefect_kind": "jinja",
645+
"template": "{{ event.payload['context'] | tojson }}",
646+
},
647+
},
616648
},
617649
job_variables={},
618650
)
@@ -641,6 +673,7 @@ async def computed_attribute_setup_python(
641673
"branch_name": branch_name,
642674
"computed_attribute_name": computed_attribute.computed_attribute.attribute.name,
643675
"computed_attribute_kind": computed_attribute.computed_attribute.kind,
676+
"context": context,
644677
},
645678
)
646679

backend/infrahub/computed_attribute/triggers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
parameters={
1717
"branch_name": "{{ event.resource['infrahub.branch.name'] }}",
1818
"trigger_updates": False,
19+
"context": {
20+
"__prefect_kind": "json",
21+
"value": {"__prefect_kind": "jinja", "template": "{{ event.payload['context'] | tojson }}"},
22+
},
1923
},
2024
)
2125
],
@@ -31,6 +35,10 @@
3135
parameters={
3236
"branch_name": "{{ event.resource['infrahub.branch.name'] }}",
3337
"commit": "{{ event.payload['commit'] }}",
38+
"context": {
39+
"__prefect_kind": "json",
40+
"value": {"__prefect_kind": "jinja", "template": "{{ event.payload['context'] | tojson }}"},
41+
},
3442
},
3543
)
3644
],
@@ -45,6 +53,10 @@
4553
name=COMPUTED_ATTRIBUTE_REMOVE_PYTHON.name,
4654
parameters={
4755
"branch_name": "{{ event.resource['infrahub.branch.name'] }}",
56+
"context": {
57+
"__prefect_kind": "json",
58+
"value": {"__prefect_kind": "jinja", "template": "{{ event.payload['context'] | tojson }}"},
59+
},
4860
},
4961
)
5062
],
@@ -58,12 +70,20 @@
5870
name=COMPUTED_ATTRIBUTE_SETUP.name,
5971
parameters={
6072
"branch_name": "{{ event.resource['infrahub.branch.name'] }}",
73+
"context": {
74+
"__prefect_kind": "json",
75+
"value": {"__prefect_kind": "jinja", "template": "{{ event.payload['context'] | tojson }}"},
76+
},
6177
},
6278
),
6379
ExecuteWorkflow(
6480
name=COMPUTED_ATTRIBUTE_SETUP_PYTHON.name,
6581
parameters={
6682
"branch_name": "{{ event.resource['infrahub.branch.name'] }}",
83+
"context": {
84+
"__prefect_kind": "json",
85+
"value": {"__prefect_kind": "jinja", "template": "{{ event.payload['context'] | tojson }}"},
86+
},
6787
},
6888
),
6989
],

backend/infrahub/core/branch/tasks.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ async def rebase_branch(branch: str, context: InfrahubContext, service: Infrahub
155155
# -------------------------------------------------------------
156156
# TODO Add account information
157157
await service.event.send(
158-
event=BranchRebasedEvent(branch_name=obj.name, branch_id=str(obj.uuid), meta=EventMeta(branch=obj))
158+
event=BranchRebasedEvent(
159+
branch_name=obj.name, branch_id=str(obj.uuid), meta=EventMeta(branch=obj, context=context)
160+
)
159161
)
160162

161163

@@ -250,7 +252,10 @@ async def delete_branch(branch: str, context: InfrahubContext, service: Infrahub
250252
await obj.delete(db=db)
251253

252254
event = BranchDeletedEvent(
253-
branch_name=branch, branch_id=str(obj.uuid), sync_with_git=obj.sync_with_git, meta=EventMeta(branch=obj)
255+
branch_name=branch,
256+
branch_id=str(obj.uuid),
257+
sync_with_git=obj.sync_with_git,
258+
meta=EventMeta(branch=obj, context=context),
254259
)
255260

256261
await service.workflow.submit_workflow(
@@ -318,7 +323,9 @@ async def create_branch(model: BranchCreateModel, context: InfrahubContext, serv
318323
branch_name=obj.name,
319324
branch_id=str(obj.uuid),
320325
sync_with_git=obj.sync_with_git,
321-
meta=EventMeta(branch=obj, account_id=context.account.account_id, initiator_id=WORKER_IDENTITY),
326+
meta=EventMeta(
327+
branch=obj, account_id=context.account.account_id, initiator_id=WORKER_IDENTITY, context=context
328+
),
322329
)
323330
await service.event.send(event=event)
324331

backend/infrahub/events/models.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from __future__ import annotations
22

3-
from typing import Any, cast
3+
from typing import Any, cast, final
44
from uuid import UUID, uuid4
55

66
from pydantic import BaseModel, Field, computed_field
77

88
from infrahub import __version__
9+
from infrahub.auth import AccountSession, AuthType
10+
from infrahub.context import InfrahubContext # noqa: TC001
911
from infrahub.core.branch import Branch # noqa: TC001
1012
from infrahub.message_bus import InfrahubMessage, Meta
1113
from infrahub.worker import WORKER_IDENTITY
@@ -25,7 +27,7 @@ class EventMeta(BaseModel):
2527
initiator_id: str = Field(
2628
default=WORKER_IDENTITY, description="The worker identity of the initial sender of this message"
2729
)
28-
context: list[dict] = Field(default_factory=list)
30+
context: InfrahubContext = Field(..., description="The context used when originating this event")
2931
level: int = Field(default=0)
3032
has_children: bool = Field(
3133
default=False, description="Indicates if this event might potentially have child events under it."
@@ -81,8 +83,13 @@ def get_related(self) -> list[dict[str, str]]:
8183
return related
8284

8385
@classmethod
84-
def default(cls) -> EventMeta:
85-
return cls()
86+
def with_dummy_context(cls, branch: Branch) -> EventMeta:
87+
return cls(
88+
branch=branch,
89+
context=InfrahubContext.init(
90+
branch=branch, account=AccountSession(auth_type=AuthType.NONE, authenticated=False, account_id="")
91+
),
92+
)
8693

8794
@classmethod
8895
def from_parent(cls, parent: InfrahubEvent) -> EventMeta:
@@ -98,11 +105,12 @@ def from_parent(cls, parent: InfrahubEvent) -> EventMeta:
98105
initiator_id=parent.meta.initiator_id,
99106
account_id=parent.meta.account_id,
100107
level=parent.meta.level + 1,
108+
context=parent.meta.context,
101109
)
102110

103111

104112
class InfrahubEvent(BaseModel):
105-
meta: EventMeta = Field(default_factory=EventMeta.default)
113+
meta: EventMeta = Field(..., description="Metadata for the event")
106114

107115
def get_id(self) -> str:
108116
return self.meta.get_id()
@@ -126,8 +134,21 @@ def get_related(self) -> list[dict[str, str]]:
126134
return self.meta.get_related()
127135

128136
def get_payload(self) -> dict[str, Any]:
137+
"""The purpose if this method is to allow subclasses to define their own payload.
138+
139+
It should not be used to get the complete payload instead .get_event_payload() should
140+
be used for that as it will always contain the 'context' key regardless of changes
141+
in child classes
142+
"""
129143
return {}
130144

145+
@final
146+
def get_event_payload(self) -> dict[str, Any]:
147+
"""This method should be used when emitting the event to the event broker"""
148+
event_payload = self.get_payload()
149+
event_payload["context"] = self.meta.context.model_dump(mode="json")
150+
return event_payload
151+
131152
def get_message_meta(self) -> Meta:
132153
meta = Meta()
133154

backend/infrahub/git/integrator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ async def import_objects_from_files(
207207
commit=commit,
208208
repository_name=self.name,
209209
repository_id=str(self.id),
210-
meta=EventMeta(branch=infrahub_branch),
210+
meta=EventMeta.with_dummy_context(branch=infrahub_branch),
211211
)
212212
)
213213

backend/infrahub/graphql/mutations/computed_attribute.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ async def mutate(
9797
fields=[str(data.attribute)],
9898
action=MutationAction.UPDATED,
9999
meta=EventMeta(
100+
context=graphql_context.get_context(),
100101
initiator_id=WORKER_IDENTITY,
101102
request_id=request_id,
102103
account_id=graphql_context.active_account_session.account_id,

backend/infrahub/graphql/mutations/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ async def mutate(cls, root: dict, info: GraphQLResolveInfo, data: InputObjectTyp
106106
initiator_id=WORKER_IDENTITY,
107107
request_id=request_id,
108108
branch=graphql_context.branch,
109+
context=graphql_context.get_context(),
109110
)
110111
main_event = NodeMutatedEvent(
111112
kind=obj._schema.kind,

backend/infrahub/graphql/mutations/relationship.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,24 @@ async def mutate( # noqa: PLR0915
171171
data=node_changelog,
172172
action=MutationAction.UPDATED,
173173
fields=[relationship_name],
174-
meta=EventMeta(
175-
branch=graphql_context.branch,
176-
),
174+
meta=EventMeta(branch=graphql_context.branch, context=graphql_context.get_context()),
177175
)
178176
graphql_context.background.add_task(graphql_context.active_service.event.send, event)
179177
if group_event_type == GroupUpdateType.MEMBERS:
180178
if cls.__name__ == "RelationshipAdd":
181179
group_add_event = GroupMemberAddedEvent(
182-
node_id=source.id, kind=source.get_schema().kind, members=peers
180+
node_id=source.id,
181+
kind=source.get_schema().kind,
182+
members=peers,
183+
meta=EventMeta(branch=graphql_context.branch, context=graphql_context.get_context()),
183184
)
184185
graphql_context.background.add_task(graphql_context.active_service.event.send, group_add_event)
185186
elif cls.__name__ == "RelationshipRemove":
186187
group_remove_event = GroupMemberRemovedEvent(
187-
node_id=source.id, kind=source.get_schema().kind, members=peers
188+
node_id=source.id,
189+
kind=source.get_schema().kind,
190+
members=peers,
191+
meta=EventMeta(branch=graphql_context.branch, context=graphql_context.get_context()),
188192
)
189193
graphql_context.background.add_task(graphql_context.active_service.event.send, group_remove_event)
190194
elif group_event_type == GroupUpdateType.MEMBER_OF_GROUPS:
@@ -200,6 +204,7 @@ async def mutate( # noqa: PLR0915
200204
node_id=node_id,
201205
kind=node_kind,
202206
members=[EventNode(id=source.get_id(), kind=source.get_kind())],
207+
meta=EventMeta(branch=graphql_context.branch, context=graphql_context.get_context()),
203208
)
204209
graphql_context.background.add_task(
205210
graphql_context.active_service.event.send, group_add_event
@@ -209,6 +214,7 @@ async def mutate( # noqa: PLR0915
209214
node_id=node_id,
210215
kind=node_kind,
211216
members=[EventNode(id=source.get_id(), kind=source.get_kind())],
217+
meta=EventMeta(branch=graphql_context.branch, context=graphql_context.get_context()),
212218
)
213219
graphql_context.background.add_task(
214220
graphql_context.active_service.event.send, group_remove_event

0 commit comments

Comments
 (0)