Skip to content

Allow recursive convertion to InfrahubNode objects #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
24 changes: 23 additions & 1 deletion infrahub_sdk/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,12 @@
await self._process_mutation_result(mutation_name=mutation_name, response=response, timeout=timeout)

async def _process_relationships(
self, node_data: dict[str, Any], branch: str, related_nodes: list[InfrahubNode], timeout: int | None = None
self,
node_data: dict[str, Any],
branch: str,
related_nodes: list[InfrahubNode],
timeout: int | None = None,
recursive: bool = False,
) -> None:
"""Processes the Relationships of a InfrahubNode and add Related Nodes to a list.

Expand All @@ -907,6 +912,10 @@
client=self._client, branch=branch, data=relation, timeout=timeout
)
related_nodes.append(related_node)
if recursive:
await related_node._process_relationships(

Check warning on line 916 in infrahub_sdk/node/node.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/node/node.py#L916

Added line #L916 was not covered by tests
node_data=relation, branch=branch, related_nodes=related_nodes, recursive=recursive
)
elif rel and isinstance(rel, RelationshipManager):
peers = node_data["node"].get(rel_name, None)
if peers and peers["edges"]:
Expand All @@ -915,6 +924,10 @@
client=self._client, branch=branch, data=peer, timeout=timeout
)
related_nodes.append(related_node)
if recursive:
await related_node._process_relationships(
node_data=peer, branch=branch, related_nodes=related_nodes, recursive=recursive
)

async def get_pool_allocated_resources(self, resource: InfrahubNode) -> list[InfrahubNode]:
"""Fetch all nodes that were allocated for the pool and a given resource.
Expand Down Expand Up @@ -1416,6 +1429,7 @@
branch: str,
related_nodes: list[InfrahubNodeSync],
timeout: int | None = None,
recursive: bool = False,
) -> None:
"""Processes the Relationships of a InfrahubNodeSync and add Related Nodes to a list.

Expand All @@ -1435,6 +1449,10 @@
client=self._client, branch=branch, data=relation, timeout=timeout
)
related_nodes.append(related_node)
if recursive:
related_node._process_relationships(

Check warning on line 1453 in infrahub_sdk/node/node.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/node/node.py#L1453

Added line #L1453 was not covered by tests
node_data=relation, branch=branch, related_nodes=related_nodes, recursive=recursive
)
elif rel and isinstance(rel, RelationshipManagerSync):
peers = node_data["node"].get(rel_name)
if peers:
Expand All @@ -1443,6 +1461,10 @@
client=self._client, branch=branch, data=peer, timeout=timeout
)
related_nodes.append(related_node)
if recursive:
related_node._process_relationships(

Check warning on line 1465 in infrahub_sdk/node/node.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/node/node.py#L1465

Added line #L1465 was not covered by tests
node_data=peer, branch=branch, related_nodes=related_nodes, recursive=recursive
)

def get_pool_allocated_resources(self, resource: InfrahubNodeSync) -> list[InfrahubNodeSync]:
"""Fetch all nodes that were allocated for the pool and a given resource.
Expand Down
2 changes: 1 addition & 1 deletion infrahub_sdk/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def process_nodes(self, data: dict) -> None:
)
self._nodes.append(node)
await node._process_relationships(
node_data=result, branch=self.branch_name, related_nodes=self._related_nodes
node_data=result, branch=self.branch_name, related_nodes=self._related_nodes, recursive=True
)

for node in self._nodes + self._related_nodes:
Expand Down