1717from infrahub .git .repository import get_initialized_repo
1818from infrahub .services import services
1919from infrahub .support .macro import MacroDefinition
20- from infrahub .workflows .catalogue import PROCESS_COMPUTED_MACRO
20+ from infrahub .workflows .catalogue import PROCESS_COMPUTED_MACRO , UPDATE_COMPUTED_ATTRIBUTE_TRANSFORM
2121from infrahub .workflows .utils import add_branch_tag
2222
23+ from .constants import AUTOMATION_NAME , AUTOMATION_NAME_PREFIX
24+ from .models import ComputedAttributeAutomations
25+
2326if TYPE_CHECKING :
2427 from infrahub .core .schema .computed_attribute import ComputedAttribute
2528 from infrahub .core .schema .schema_branch import ComputedAttributeTarget
@@ -175,24 +178,33 @@ async def process_jinja2(
175178 print ()
176179
177180
178- @flow (name = "computed-attribute-setup" )
181+ @flow (name = "computed-attribute-setup" , flow_run_name = "Setup computed attributes in task-manager" )
179182async def computed_attribute_setup () -> None :
180183 # service = services.service
181184 schema_branch = registry .schema .get_schema_branch (name = registry .default_branch )
182185 log = get_run_logger ()
186+
183187 async with get_client (sync_client = False ) as client :
184188 deployments = {
185189 item .name : item
186190 for item in await client .read_deployments (
187- deployment_filter = DeploymentFilter (name = DeploymentFilterName (any_ = [PROCESS_COMPUTED_MACRO .name ]))
191+ deployment_filter = DeploymentFilter (
192+ name = DeploymentFilterName (
193+ any_ = [PROCESS_COMPUTED_MACRO .name , UPDATE_COMPUTED_ATTRIBUTE_TRANSFORM .name ]
194+ )
195+ )
188196 )
189197 }
190198 if PROCESS_COMPUTED_MACRO .name not in deployments :
191199 raise ValueError ("Unable to find the deployment for PROCESS_COMPUTED_MACRO" )
192- deployment_id = deployments [PROCESS_COMPUTED_MACRO .name ].id
200+ if UPDATE_COMPUTED_ATTRIBUTE_TRANSFORM .name not in deployments :
201+ raise ValueError ("Unable to find the deployment for UPDATE_COMPUTED_ATTRIBUTE_TRANSFORM" )
193202
194- # TODO need to pull the existing automation to see if we need to create or update each object
195- # automations = await client.read_automations()
203+ deployment_id_jinja = deployments [PROCESS_COMPUTED_MACRO .name ].id
204+ # deployment_id_python = deployments[UPDATE_COMPUTED_ATTRIBUTE_TRANSFORM.name].id
205+
206+ automations = await client .read_automations ()
207+ existing_computed_attr_automations = ComputedAttributeAutomations .from_prefect (automations = automations )
196208
197209 computed_attributes : dict [str , ComputedAttributeTarget ] = {}
198210 for item in schema_branch ._computed_jinja2_attribute_map .values ():
@@ -205,9 +217,11 @@ async def computed_attribute_setup() -> None:
205217
206218 for identifier , computed_attribute in computed_attributes .items ():
207219 log .info (f"processing { computed_attribute .key_name } " )
220+ scope = "default"
221+
208222 automation = AutomationCore (
209- name = f"computed-attribute-process- { identifier } " ,
210- description = f"Process value of the computed attribute for { identifier } " ,
223+ name = AUTOMATION_NAME . format ( prefix = AUTOMATION_NAME_PREFIX , identifier = identifier , scope = scope ) ,
224+ description = f"Process value of the computed attribute for { identifier } [ { scope } ] " ,
211225 enabled = True ,
212226 trigger = EventTrigger (
213227 posture = Posture .Reactive ,
@@ -219,7 +233,7 @@ async def computed_attribute_setup() -> None:
219233 actions = [
220234 RunDeployment (
221235 source = "selected" ,
222- deployment_id = deployment_id ,
236+ deployment_id = deployment_id_jinja ,
223237 parameters = {
224238 "branch_name" : "{{ event.resource['infrahub.branch.name'] }}" ,
225239 "node_kind" : "{{ event.resource['infrahub.node.kind'] }}" ,
@@ -230,5 +244,10 @@ async def computed_attribute_setup() -> None:
230244 ],
231245 )
232246
233- response = await client .create_automation (automation = automation )
234- log .info (f"Processed: { computed_attribute .key_name } : { response } " )
247+ if existing_computed_attr_automations .has (identifier = identifier , scope = scope ):
248+ existing = existing_computed_attr_automations .get (identifier = identifier , scope = scope )
249+ await client .update_automation (automation_id = existing .id , automation = automation )
250+ log .info (f"{ computed_attribute .key_name } Updated" )
251+ else :
252+ await client .create_automation (automation = automation )
253+ log .info (f"{ computed_attribute .key_name } Created" )
0 commit comments