Skip to content

Commit 65bd9a3

Browse files
authored
Fix setting value of relationship of type pool (#118)
Fixes #27
1 parent 2ed711b commit 65bd9a3

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

changelog/27.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix generated GraphQL query when having a relationship to a pool node

infrahub_sdk/node.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ def typename(self) -> Optional[str]:
241241
return self._peer.typename
242242
return self._typename
243243

244-
def _generate_input_data(self) -> dict[str, Any]:
244+
def _generate_input_data(self, allocate_from_pool: bool = False) -> dict[str, Any]:
245245
data: dict[str, Any] = {}
246246

247-
if self.is_resource_pool:
247+
if self.is_resource_pool and allocate_from_pool:
248248
return {"from_pool": {"id": self.id}}
249249

250250
if self.id is not None:
@@ -424,8 +424,8 @@ def peer_hfids_str(self) -> list[str]:
424424
def has_update(self) -> bool:
425425
return self._has_update
426426

427-
def _generate_input_data(self) -> list[dict]:
428-
return [peer._generate_input_data() for peer in self.peers]
427+
def _generate_input_data(self, allocate_from_pool: bool = False) -> list[dict]:
428+
return [peer._generate_input_data(allocate_from_pool=allocate_from_pool) for peer in self.peers]
429429

430430
def _generate_mutation_query(self) -> dict[str, Any]:
431431
# Does nothing for now
@@ -818,6 +818,7 @@ def _generate_input_data(self, exclude_unmodified: bool = False, exclude_hfid: b
818818
data[item_name] = attr_data
819819

820820
for item_name in self._relationships:
821+
allocate_from_pool = False
821822
rel_schema = self._schema.get_relationship(name=item_name)
822823
if not rel_schema or rel_schema.read_only:
823824
continue
@@ -836,7 +837,12 @@ def _generate_input_data(self, exclude_unmodified: bool = False, exclude_hfid: b
836837
if rel is None or not rel.initialized:
837838
continue
838839

839-
rel_data = rel._generate_input_data()
840+
if isinstance(rel, (RelatedNode, RelatedNodeSync)) and rel.is_resource_pool:
841+
# If the relatiionship is a resource pool and the expected schema is different from the one of the pool, this means we expect to get
842+
# a resource from the pool itself
843+
allocate_from_pool = rel_schema.peer != rel.peer._schema.kind
844+
845+
rel_data = rel._generate_input_data(allocate_from_pool=allocate_from_pool)
840846

841847
if rel_data and isinstance(rel_data, dict):
842848
if variable_values := rel_data.get("data"):
@@ -1426,7 +1432,7 @@ async def get_pool_allocated_resources(self, resource: InfrahubNode) -> list[Inf
14261432
list[InfrahubNode]: The allocated nodes.
14271433
"""
14281434
if not self.is_resource_pool():
1429-
raise ValueError("Allocate resources can only be fetched from resource pool nodes.")
1435+
raise ValueError("Allocated resources can only be fetched from resource pool nodes.")
14301436

14311437
graphql_query_name = "InfrahubResourcePoolAllocated"
14321438
node_ids_per_kind: dict[str, list[str]] = {}

tests/unit/sdk/conftest.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,15 @@ async def simple_device_schema() -> NodeSchema:
821821
"optional": True,
822822
"cardinality": "one",
823823
"kind": "Attribute",
824-
}
824+
},
825+
{
826+
"name": "ip_address_pool",
827+
"peer": "CoreIPAddressPool",
828+
"label": "Address allocator",
829+
"optional": True,
830+
"cardinality": "one",
831+
"kind": "Attribute",
832+
},
825833
],
826834
}
827835
return NodeSchema(**data) # type: ignore

tests/unit/sdk/test_node.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,9 @@ async def test_create_input_data_with_resource_pool_relationship(
14871487
},
14881488
)
14891489
device = InfrahubNode(
1490-
client=client, schema=simple_device_schema, data={"name": "device-01", "primary_address": ip_pool}
1490+
client=client,
1491+
schema=simple_device_schema,
1492+
data={"name": "device-01", "primary_address": ip_pool, "ip_address_pool": ip_pool},
14911493
)
14921494
else:
14931495
ip_prefix = InfrahubNodeSync(client=client, schema=ipam_ipprefix_schema, data=ipam_ipprefix_data)
@@ -1504,13 +1506,16 @@ async def test_create_input_data_with_resource_pool_relationship(
15041506
},
15051507
)
15061508
device = InfrahubNode(
1507-
client=client, schema=simple_device_schema, data={"name": "device-01", "primary_address": ip_pool}
1509+
client=client,
1510+
schema=simple_device_schema,
1511+
data={"name": "device-01", "primary_address": ip_pool, "ip_address_pool": ip_pool},
15081512
)
15091513

15101514
assert device._generate_input_data()["data"] == {
15111515
"data": {
15121516
"name": {"value": "device-01"},
15131517
"primary_address": {"from_pool": {"id": "pppppppp-pppp-pppp-pppp-pppppppppppp"}},
1518+
"ip_address_pool": {"id": "pppppppp-pppp-pppp-pppp-pppppppppppp"},
15141519
},
15151520
}
15161521

@@ -1534,7 +1539,9 @@ async def test_create_mutation_query_with_resource_pool_relationship(
15341539
},
15351540
)
15361541
device = InfrahubNode(
1537-
client=client, schema=simple_device_schema, data={"name": "device-01", "primary_address": ip_pool}
1542+
client=client,
1543+
schema=simple_device_schema,
1544+
data={"name": "device-01", "primary_address": ip_pool, "ip_address_pool": ip_pool},
15381545
)
15391546
else:
15401547
ip_prefix = InfrahubNodeSync(client=client, schema=ipam_ipprefix_schema, data=ipam_ipprefix_data)
@@ -1551,11 +1558,17 @@ async def test_create_mutation_query_with_resource_pool_relationship(
15511558
},
15521559
)
15531560
device = InfrahubNode(
1554-
client=client, schema=simple_device_schema, data={"name": "device-01", "primary_address": ip_pool}
1561+
client=client,
1562+
schema=simple_device_schema,
1563+
data={"name": "device-01", "primary_address": ip_pool, "ip_address_pool": ip_pool},
15551564
)
15561565

15571566
assert device._generate_mutation_query() == {
1558-
"object": {"id": None, "primary_address": {"node": {"__typename": None, "display_label": None, "id": None}}},
1567+
"object": {
1568+
"id": None,
1569+
"primary_address": {"node": {"__typename": None, "display_label": None, "id": None}},
1570+
"ip_address_pool": {"node": {"__typename": None, "display_label": None, "id": None}},
1571+
},
15591572
"ok": None,
15601573
}
15611574

0 commit comments

Comments
 (0)