Skip to content

Commit 57b3651

Browse files
Updated _process_relationships to process sub relationships for both clients.
1 parent 565f729 commit 57b3651

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When processing relationships to add to the store, traverse tree and add all nodes to the store.

infrahub_sdk/node.py

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,34 @@ async def update(
15211521
)
15221522
await self._process_mutation_result(mutation_name=mutation_name, response=response, timeout=timeout)
15231523

1524+
async def _process_sub_relationships(
1525+
self, node_data: dict[str, Any], branch: str, related_nodes: list[InfrahubNode], timeout: int | None = None
1526+
) -> None:
1527+
"""Recursively processes the Relationships of a InfrahubNode and add Related Nodes to a list.
1528+
1529+
Args:
1530+
node_data (dict[str, Any]): The item from the GraphQL response corresponding to the node.
1531+
branch (str): The branch name.
1532+
related_nodes (list[InfrahubNode]): The list to which related nodes will be appended.
1533+
timeout (int, optional): Overrides default timeout used when querying the graphql API. Specified in seconds.
1534+
"""
1535+
1536+
sub_relationships = {
1537+
node: info["node"]
1538+
for node, info in node_data["node"].items()
1539+
if isinstance(info, dict) and info.get("node", {}).get("__typename")
1540+
}
1541+
for rel_name in sub_relationships:
1542+
relation = node_data["node"].get(rel_name, None)
1543+
if relation and relation.get("node", None):
1544+
related_node = await InfrahubNode.from_graphql(
1545+
client=self._client, branch=branch, data=relation, timeout=timeout
1546+
)
1547+
related_nodes.append(related_node)
1548+
await self._process_sub_relationships(
1549+
node_data=relation, branch=branch, related_nodes=related_nodes, timeout=timeout
1550+
)
1551+
15241552
async def _process_relationships(
15251553
self, node_data: dict[str, Any], branch: str, related_nodes: list[InfrahubNode], timeout: int | None = None
15261554
) -> None:
@@ -1541,6 +1569,9 @@ async def _process_relationships(
15411569
client=self._client, branch=branch, data=relation, timeout=timeout
15421570
)
15431571
related_nodes.append(related_node)
1572+
await self._process_sub_relationships(
1573+
node_data=relation, branch=branch, related_nodes=related_nodes, timeout=timeout
1574+
)
15441575
elif rel and isinstance(rel, RelationshipManager):
15451576
peers = node_data["node"].get(rel_name, None)
15461577
if peers and peers["edges"]:
@@ -1549,6 +1580,9 @@ async def _process_relationships(
15491580
client=self._client, branch=branch, data=peer, timeout=timeout
15501581
)
15511582
related_nodes.append(related_node)
1583+
await self._process_sub_relationships(
1584+
node_data=peer, branch=branch, related_nodes=related_nodes, timeout=timeout
1585+
)
15521586

15531587
async def get_pool_allocated_resources(self, resource: InfrahubNode) -> list[InfrahubNode]:
15541588
"""Fetch all nodes that were allocated for the pool and a given resource.
@@ -2044,12 +2078,37 @@ def update(
20442078
)
20452079
self._process_mutation_result(mutation_name=mutation_name, response=response, timeout=timeout)
20462080

2081+
def _process_sub_relationships(
2082+
self, node_data: dict[str, Any], branch: str, related_nodes: list[InfrahubNodeSync], timeout: int | None = None
2083+
) -> None:
2084+
"""Recursively processes the Relationships of a InfrahubNodeSync and add Related Nodes to a list.
2085+
2086+
Args:
2087+
node_data (dict[str, Any]): The item from the GraphQL response corresponding to the node.
2088+
branch (str): The branch name.
2089+
related_nodes (list[InfrahubNode]): The list to which related nodes will be appended.
2090+
timeout (int, optional): Overrides default timeout used when querying the graphql API. Specified in seconds.
2091+
2092+
"""
2093+
2094+
sub_relationships = {
2095+
node: info["node"]
2096+
for node, info in node_data["node"].items()
2097+
if isinstance(info, dict) and info.get("node", {}).get("__typename")
2098+
}
2099+
for rel_name in sub_relationships:
2100+
relation = node_data["node"].get(rel_name, None)
2101+
if relation and relation.get("node", None):
2102+
related_node = InfrahubNodeSync.from_graphql(
2103+
client=self._client, branch=branch, data=relation, timeout=timeout
2104+
)
2105+
related_nodes.append(related_node)
2106+
self._process_sub_relationships(
2107+
node_data=relation, branch=branch, related_nodes=related_nodes, timeout=timeout
2108+
)
2109+
20472110
def _process_relationships(
2048-
self,
2049-
node_data: dict[str, Any],
2050-
branch: str,
2051-
related_nodes: list[InfrahubNodeSync],
2052-
timeout: int | None = None,
2111+
self, node_data: dict[str, Any], branch: str, related_nodes: list[InfrahubNodeSync], timeout: int | None = None
20532112
) -> None:
20542113
"""Processes the Relationships of a InfrahubNodeSync and add Related Nodes to a list.
20552114
@@ -2063,20 +2122,26 @@ def _process_relationships(
20632122
for rel_name in self._relationships:
20642123
rel = getattr(self, rel_name)
20652124
if rel and isinstance(rel, RelatedNodeSync):
2066-
relation = node_data["node"].get(rel_name)
2067-
if relation.get("node", None):
2125+
relation = node_data["node"].get(rel_name, None)
2126+
if relation and relation.get("node", None):
20682127
related_node = InfrahubNodeSync.from_graphql(
20692128
client=self._client, branch=branch, data=relation, timeout=timeout
20702129
)
20712130
related_nodes.append(related_node)
2131+
self._process_sub_relationships(
2132+
node_data=relation, branch=branch, related_nodes=related_nodes, timeout=timeout
2133+
)
20722134
elif rel and isinstance(rel, RelationshipManagerSync):
2073-
peers = node_data["node"].get(rel_name)
2074-
if peers:
2135+
peers = node_data["node"].get(rel_name, None)
2136+
if peers and peers["edges"]:
20752137
for peer in peers["edges"]:
20762138
related_node = InfrahubNodeSync.from_graphql(
20772139
client=self._client, branch=branch, data=peer, timeout=timeout
20782140
)
20792141
related_nodes.append(related_node)
2142+
self._process_sub_relationships(
2143+
node_data=peer, branch=branch, related_nodes=related_nodes, timeout=timeout
2144+
)
20802145

20812146
def get_pool_allocated_resources(self, resource: InfrahubNodeSync) -> list[InfrahubNodeSync]:
20822147
"""Fetch all nodes that were allocated for the pool and a given resource.

0 commit comments

Comments
 (0)