Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set the HFID on related nodes for cardinality many relationships and add hfid support to the RelationshipManager add, extend and remove methods
25 changes: 22 additions & 3 deletions infrahub_sdk/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@
"""
data: dict[str, Any] = {
"count": None,
"edges": {"node": {"id": None, "display_label": None, "__typename": None}},
"edges": {"node": {"id": None, "hfid": None, "display_label": None, "__typename": None}},
}

properties: dict[str, Any] = {}
Expand Down Expand Up @@ -569,7 +569,9 @@
raise UninitializedError("Must call fetch() on RelationshipManager before editing members")
new_node = RelatedNode(schema=self.schema, client=self.client, branch=self.branch, data=data)

if new_node.id and new_node.id not in self.peer_ids:
if (new_node.id and new_node.id not in self.peer_ids) or (
new_node.hfid and new_node.hfid not in self.peer_hfids
):
self.peers.append(new_node)
self._has_update = True

Expand All @@ -591,6 +593,14 @@
self.peers.pop(idx)
self._has_update = True

elif node_to_remove.hfid and node_to_remove.hfid in self.peer_hfids:
idx = self.peer_hfids.index(node_to_remove.hfid)

Check warning on line 597 in infrahub_sdk/node.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/node.py#L597

Added line #L597 was not covered by tests
if self.peers[idx].hfid != node_to_remove.hfid:
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.hfid}")

Check warning on line 599 in infrahub_sdk/node.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/node.py#L599

Added line #L599 was not covered by tests

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

Check warning on line 602 in infrahub_sdk/node.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/node.py#L601-L602

Added lines #L601 - L602 were not covered by tests


class RelationshipManagerSync(RelationshipManagerBase):
"""Manages relationships of a node in a synchronous context."""
Expand Down Expand Up @@ -664,7 +674,9 @@
raise UninitializedError("Must call fetch() on RelationshipManager before editing members")
new_node = RelatedNodeSync(schema=self.schema, client=self.client, branch=self.branch, data=data)

if new_node.id and new_node.id not in self.peer_ids:
if (new_node.id and new_node.id not in self.peer_ids) or (
new_node.hfid and new_node.hfid not in self.peer_hfids
):
self.peers.append(new_node)
self._has_update = True

Expand All @@ -682,6 +694,13 @@
idx = self.peer_ids.index(node_to_remove.id)
if self.peers[idx].id != node_to_remove.id:
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.id}")
self.peers.pop(idx)
self._has_update = True

elif node_to_remove.hfid and node_to_remove.hfid in self.peer_hfids:
idx = self.peer_hfids.index(node_to_remove.hfid)

Check warning on line 701 in infrahub_sdk/node.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/node.py#L701

Added line #L701 was not covered by tests
if self.peers[idx].hfid != node_to_remove.hfid:
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.hfid}")

Check warning on line 703 in infrahub_sdk/node.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/node.py#L703

Added line #L703 was not covered by tests

self.peers.pop(idx)
self._has_update = True
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/sdk/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,7 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
},
"node": {
"id": None,
"hfid": None,
"display_label": None,
"__typename": None,
},
Expand Down Expand Up @@ -1157,6 +1158,7 @@ async def test_query_data_include(client, location_schema: NodeSchemaAPI, client
"edges": {
"node": {
"id": None,
"hfid": None,
"display_label": None,
"__typename": None,
},
Expand Down