Skip to content

Commit 27a6c27

Browse files
committed
Fix setting value of relationship of type pool
1 parent b008784 commit 27a6c27

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

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]] = {}

0 commit comments

Comments
 (0)