22
33from typing import TYPE_CHECKING
44
5- from infrahub_sdk .protocols import (
6- CoreNode , # noqa: TC002
7- CoreTransformPython ,
8- )
5+ from infrahub_sdk .protocols import CoreTransformPython
96from infrahub_sdk .template import Jinja2Template
107from prefect import flow
118from prefect .client .orchestration import get_client
2825from infrahub .workflows .utils import add_tags , wait_for_schema_to_converge
2926
3027from .gather import gather_trigger_computed_attribute_jinja2 , gather_trigger_computed_attribute_python
31- from .models import (
32- PythonTransformTarget ,
33- )
28+ from .models import ComputedAttrJinja2GraphQL , ComputedAttrJinja2GraphQLResponse , PythonTransformTarget
3429
3530if TYPE_CHECKING :
3631 from infrahub .core .schema .computed_attribute import ComputedAttribute
@@ -167,49 +162,33 @@ async def trigger_update_python_computed_attributes(
167162 flow_run_name = "Update value for computed attribute {attribute_name}" ,
168163)
169164async def update_computed_attribute_value_jinja2 (
170- branch_name : str , obj : CoreNode , attribute_name : str , template_value : str , service : InfrahubServices
165+ branch_name : str ,
166+ obj : ComputedAttrJinja2GraphQLResponse ,
167+ node_kind : str ,
168+ attribute_name : str ,
169+ template : Jinja2Template ,
170+ service : InfrahubServices ,
171171) -> None :
172172 log = get_run_logger ()
173173
174- await add_tags (branches = [branch_name ], nodes = [obj .id ], db_change = True )
175-
176- jinja_template = Jinja2Template (template = template_value )
177- variables = {}
178- for variable in jinja_template .get_variables ():
179- components = variable .split ("__" )
180- if len (components ) == 2 :
181- property_name = components [0 ]
182- property_value = components [1 ]
183- attribute_property = getattr (obj , property_name )
184- variables [variable ] = getattr (attribute_property , property_value )
185- elif len (components ) == 3 :
186- relationship_name = components [0 ]
187- property_name = components [1 ]
188- property_value = components [2 ]
189- relationship = getattr (obj , relationship_name )
190- try :
191- attribute_property = getattr (relationship .peer , property_name )
192- variables [variable ] = getattr (attribute_property , property_value )
193- except ValueError :
194- variables [variable ] = ""
195-
196- value = await jinja_template .render (variables = variables )
197- existing_value = getattr (obj , attribute_name ).value
198- if value == existing_value :
174+ await add_tags (branches = [branch_name ], nodes = [obj .node_id ], db_change = True )
175+
176+ value = await template .render (variables = obj .variables )
177+ if value == obj .computed_attribute_value :
199178 log .debug (f"Ignoring to update { obj } with existing value on { attribute_name } ={ value } " )
200179 return
201180
202181 await service .client .execute_graphql (
203182 query = UPDATE_ATTRIBUTE ,
204183 variables = {
205- "id" : obj .id ,
206- "kind" : obj . get_kind () ,
184+ "id" : obj .node_id ,
185+ "kind" : node_kind ,
207186 "attribute" : attribute_name ,
208187 "value" : value ,
209188 },
210189 branch_name = branch_name ,
211190 )
212- log .info (f"Updating computed attribute { obj . get_kind () } .{ attribute_name } ='{ value } ' ({ obj .id } )" )
191+ log .info (f"Updating computed attribute { node_kind } .{ attribute_name } ='{ value } ' ({ obj .node_id } )" )
213192
214193
215194@flow (
@@ -235,41 +214,43 @@ async def process_jinja2(
235214 branch_name if branch_name in registry .get_altered_schema_branches () else registry .default_branch
236215 )
237216 schema_branch = registry .schema .get_schema_branch (name = target_branch_schema )
238- await service .client .schema .all (branch = branch_name , refresh = True , schema_hash = schema_branch .get_hash ())
239-
217+ node_schema = schema_branch .get_node (name = computed_attribute_kind , duplicate = False )
240218 computed_macros = [
241219 attrib
242220 for attrib in schema_branch .computed_attributes .get_impacted_jinja2_targets (kind = node_kind , updates = updates )
243221 if attrib .kind == computed_attribute_kind and attrib .attribute .name == computed_attribute_name
244222 ]
245223 for computed_macro in computed_macros :
246- found : list [CoreNode ] = []
224+ found : list [ComputedAttrJinja2GraphQLResponse ] = []
225+ template_string = "n/a"
226+ if computed_macro .attribute .computed_attribute and computed_macro .attribute .computed_attribute .jinja2_template :
227+ template_string = computed_macro .attribute .computed_attribute .jinja2_template
228+
229+ jinja_template = Jinja2Template (template = template_string )
230+ variables = jinja_template .get_variables ()
231+
232+ attribute_graphql = ComputedAttrJinja2GraphQL (
233+ node_schema = node_schema , attribute_schema = computed_macro .attribute , variables = variables
234+ )
235+
247236 for id_filter in computed_macro .node_filters :
248- filters = {id_filter : object_id }
249- nodes : list [CoreNode ] = await service .client .filters (
250- kind = computed_macro .kind ,
251- branch = branch_name ,
252- prefetch_relationships = True ,
253- populate_store = True ,
254- ** filters ,
255- )
256- found .extend (nodes )
237+ query = attribute_graphql .render_graphql_query (query_filter = id_filter , filter_id = object_id )
238+ response = await service .client .execute_graphql (query = query , branch_name = branch_name )
239+ output = attribute_graphql .parse_response (response = response )
240+ found .extend (output )
257241
258242 if not found :
259243 log .debug ("No nodes found that requires updates" )
260244
261- template_string = "n/a"
262- if computed_macro .attribute .computed_attribute and computed_macro .attribute .computed_attribute .jinja2_template :
263- template_string = computed_macro .attribute .computed_attribute .jinja2_template
264-
265245 batch = await service .client .create_batch ()
266246 for node in found :
267247 batch .add (
268248 task = update_computed_attribute_value_jinja2 ,
269249 branch_name = branch_name ,
270250 obj = node ,
251+ node_kind = node_schema .kind ,
271252 attribute_name = computed_macro .attribute .name ,
272- template_value = template_string ,
253+ template = jinja_template ,
273254 service = service ,
274255 )
275256
0 commit comments