Skip to content

Commit 8a5cb3b

Browse files
committed
add hfid support to Relationshipmanager add, extend & remove methods
1 parent c4c8d7e commit 8a5cb3b

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

infrahub_sdk/node.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,9 @@ def add(self, data: str | RelatedNode | dict) -> None:
569569
raise UninitializedError("Must call fetch() on RelationshipManager before editing members")
570570
new_node = RelatedNode(schema=self.schema, client=self.client, branch=self.branch, data=data)
571571

572-
if new_node.id and new_node.id not in self.peer_ids:
572+
if (new_node.id and new_node.id not in self.peer_ids) or (
573+
new_node.hfid and new_node.hfid not in self.peer_hfids
574+
):
573575
self.peers.append(new_node)
574576
self._has_update = True
575577

@@ -591,6 +593,14 @@ def remove(self, data: str | RelatedNode | dict) -> None:
591593
self.peers.pop(idx)
592594
self._has_update = True
593595

596+
elif node_to_remove.hfid and node_to_remove.hfid in self.peer_hfids:
597+
idx = self.peer_hfids.index(node_to_remove.hfid)
598+
if self.peers[idx].hfid != node_to_remove.hfid:
599+
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.hfid}")
600+
601+
self.peers.pop(idx)
602+
self._has_update = True
603+
594604

595605
class RelationshipManagerSync(RelationshipManagerBase):
596606
"""Manages relationships of a node in a synchronous context."""
@@ -664,7 +674,9 @@ def add(self, data: str | RelatedNodeSync | dict) -> None:
664674
raise UninitializedError("Must call fetch() on RelationshipManager before editing members")
665675
new_node = RelatedNodeSync(schema=self.schema, client=self.client, branch=self.branch, data=data)
666676

667-
if new_node.id and new_node.id not in self.peer_ids:
677+
if (new_node.id and new_node.id not in self.peer_ids) or (
678+
new_node.hfid and new_node.hfid not in self.peer_hfids
679+
):
668680
self.peers.append(new_node)
669681
self._has_update = True
670682

@@ -682,6 +694,13 @@ def remove(self, data: str | RelatedNodeSync | dict) -> None:
682694
idx = self.peer_ids.index(node_to_remove.id)
683695
if self.peers[idx].id != node_to_remove.id:
684696
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.id}")
697+
self.peers.pop(idx)
698+
self._has_update = True
699+
700+
elif node_to_remove.hfid and node_to_remove.hfid in self.peer_hfids:
701+
idx = self.peer_hfids.index(node_to_remove.hfid)
702+
if self.peers[idx].hfid != node_to_remove.hfid:
703+
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.hfid}")
685704

686705
self.peers.pop(idx)
687706
self._has_update = True

0 commit comments

Comments
 (0)