@@ -84,9 +84,13 @@ def find_matching_relationship(
8484 if self .peer_rel and not force :
8585 return self .peer_rel
8686
87- self .peer_rel = peer_schema .get_matching_relationship (
88- id = self .rel_schema .identifier or "" , direction = self .rel_schema .direction
89- )
87+ try :
88+ self .peer_rel = peer_schema .get_matching_relationship (
89+ id = self .rel_schema .identifier or "" , direction = self .rel_schema .direction
90+ )
91+ except ValueError :
92+ pass
93+
9094 return self .peer_rel
9195
9296
@@ -184,7 +188,7 @@ async def validate_object(
184188 branch : str | None = None ,
185189 ) -> list [ObjectValidationError ]:
186190 errors : list [ObjectValidationError ] = []
187- context = context or {}
191+ context = context . copy () if context else {}
188192
189193 # First validate if all mandatory fields are present
190194 for element in schema .mandatory_input_names :
@@ -245,7 +249,7 @@ async def validate_related_nodes(
245249 context : dict | None = None ,
246250 branch : str | None = None ,
247251 ) -> list [ObjectValidationError ]:
248- context = context or {}
252+ context = context . copy () if context else {}
249253 errors : list [ObjectValidationError ] = []
250254
251255 if isinstance (data , (list , str )) and rel_info .format == RelationshipDataFormat .ONE_REF :
@@ -338,13 +342,14 @@ async def create_node(
338342 branch : str | None = None ,
339343 default_schema_kind : str | None = None ,
340344 ) -> InfrahubNode :
341- context = context or {}
345+ context = context . copy () if context else {}
342346
343347 errors = await cls .validate_object (
344348 client = client , position = position , schema = schema , data = data , context = context , branch = branch
345349 )
346350 if errors :
347- raise ObjectValidationError (position = position , message = "Object is not valid" )
351+ messages = [str (error ) for error in errors ]
352+ raise ObjectValidationError (position = position , message = "Object is not valid - " + ", " .join (messages ))
348353
349354 clean_data : dict [str , Any ] = {}
350355
@@ -424,13 +429,11 @@ async def create_node(
424429
425430 # If there is a peer relationship, we add the node id to the context
426431 rel_info = rels_info [rel ]
427- if rel_info .peer_rel :
428- # TODO were making the assumption that the peer relationship is of cardinality one
429- # we need to check that and not sure how we should handle
430- context [rel_info .peer_rel .name ] = node .id
432+ context .update (rel_info .get_context (value = node .id ))
431433
432434 await cls .create_related_nodes (
433435 client = client ,
436+ node = node ,
434437 rel_info = rel_info ,
435438 position = position ,
436439 data = data [rel ],
@@ -448,17 +451,22 @@ async def create_related_nodes(
448451 rel_info : RelationshipInfo ,
449452 position : list [int | str ],
450453 data : dict | list [dict ],
454+ node : InfrahubNode | None = None ,
451455 context : dict | None = None ,
452456 branch : str | None = None ,
453457 default_schema_kind : str | None = None ,
454458 ) -> list [InfrahubNode ]:
455459 nodes : list [InfrahubNode ] = []
456- context = context or {}
460+ context = context . copy () if context else {}
457461
458462 if isinstance (data , dict ) and rel_info .format == RelationshipDataFormat .ONE_OBJ :
459463 peer_kind = data .get ("kind" ) or rel_info .peer_kind
460464 peer_schema = await client .schema .get (kind = peer_kind , branch = branch )
461465
466+ if node :
467+ rel_info .find_matching_relationship (peer_schema = peer_schema )
468+ context .update (rel_info .get_context (value = node .id ))
469+
462470 node = await cls .create_node (
463471 client = client ,
464472 schema = peer_schema ,
@@ -474,13 +482,17 @@ async def create_related_nodes(
474482 peer_kind = data .get ("kind" ) or rel_info .peer_kind
475483 peer_schema = await client .schema .get (kind = peer_kind , branch = branch )
476484
485+ if node :
486+ rel_info .find_matching_relationship (peer_schema = peer_schema )
487+ context .update (rel_info .get_context (value = node .id ))
488+
477489 for idx , peer_data in enumerate (data ["data" ]):
478490 context ["list_index" ] = idx
479491 if isinstance (peer_data , dict ):
480492 node = await cls .create_node (
481493 client = client ,
482494 schema = peer_schema ,
483- position = position + [idx + 1 ],
495+ position = position + [rel_info . name , idx + 1 ],
484496 data = peer_data ,
485497 context = context ,
486498 branch = branch ,
@@ -496,10 +508,14 @@ async def create_related_nodes(
496508 peer_kind = item .get ("kind" ) or rel_info .peer_kind
497509 peer_schema = await client .schema .get (kind = peer_kind , branch = branch )
498510
511+ if node :
512+ rel_info .find_matching_relationship (peer_schema = peer_schema )
513+ context .update (rel_info .get_context (value = node .id ))
514+
499515 node = await cls .create_node (
500516 client = client ,
501517 schema = peer_schema ,
502- position = position + [idx + 1 ],
518+ position = position + [rel_info . name , idx + 1 ],
503519 data = item ["data" ],
504520 context = context ,
505521 branch = branch ,
0 commit comments