-
Notifications
You must be signed in to change notification settings - Fork 6
Add ability to use convert_query_response with Python Transforms #376
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
Merged
ogenstad
merged 1 commit into
develop
from
pog-transform-convert-query-response-IHS-105
Apr 29, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added ability to convert the query response to InfrahubNode objects when using Python Transforms in the same way you can with Generators. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from .repository import GitRepoManager | ||
|
|
||
| if TYPE_CHECKING: | ||
| from . import InfrahubClient | ||
| from .node import InfrahubNode | ||
| from .store import NodeStore | ||
|
|
||
|
|
||
| class InfrahubOperation: | ||
| def __init__( | ||
| self, | ||
| client: InfrahubClient, | ||
| infrahub_node: type[InfrahubNode], | ||
| convert_query_response: bool, | ||
| branch: str, | ||
| root_directory: str, | ||
| ): | ||
| self.branch = branch | ||
| self.convert_query_response = convert_query_response | ||
| self.root_directory = root_directory or os.getcwd() | ||
| self.infrahub_node = infrahub_node | ||
| self._nodes: list[InfrahubNode] = [] | ||
| self._related_nodes: list[InfrahubNode] = [] | ||
| self._init_client = client.clone(branch=self.branch_name) | ||
| self.git: GitRepoManager | None = None | ||
|
|
||
| @property | ||
| def branch_name(self) -> str: | ||
| """Return the name of the current git branch.""" | ||
|
|
||
| if self.branch: | ||
| return self.branch | ||
|
|
||
| if not hasattr(self, "git") or not self.git: | ||
| self.git = GitRepoManager(self.root_directory) | ||
|
|
||
| self.branch = str(self.git.active_branch) | ||
|
|
||
| return self.branch | ||
|
|
||
| @property | ||
| def store(self) -> NodeStore: | ||
| """The store will be populated with nodes based on the query during the collection of data if activated""" | ||
| return self._init_client.store | ||
|
|
||
| @property | ||
| def nodes(self) -> list[InfrahubNode]: | ||
| """Returns nodes collected and parsed during the data collection process if this feature is enabled""" | ||
| return self._nodes | ||
|
|
||
| @property | ||
| def related_nodes(self) -> list[InfrahubNode]: | ||
| """Returns nodes collected and parsed during the data collection process if this feature is enabled""" | ||
| return self._related_nodes | ||
|
|
||
| async def process_nodes(self, data: dict) -> None: | ||
| if not self.convert_query_response: | ||
| return | ||
|
|
||
| await self._init_client.schema.all(branch=self.branch_name) | ||
|
|
||
| for kind in data: | ||
| if kind in self._init_client.schema.cache[self.branch_name].nodes.keys(): | ||
| for result in data[kind].get("edges", []): | ||
| node = await self.infrahub_node.from_graphql( | ||
| client=self._init_client, branch=self.branch_name, data=result | ||
| ) | ||
| self._nodes.append(node) | ||
| await node._process_relationships( | ||
| node_data=result, branch=self.branch_name, related_nodes=self._related_nodes | ||
| ) | ||
|
|
||
| for node in self._nodes + self._related_nodes: | ||
| if node.id: | ||
| self._init_client.store.set(node=node) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think it is worth passing the whole
transform_confighere instead of justconvert_query_response? might make it easier to add other parameters like this in the futureThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I do, but it would require more changes in the backend when we use this outside of the CTL in situations where we haven't stored the config anywhere. I think for now we should leave it like this to not change too many things. But could be that we should do a larger refactor and consider what parts should be kept in the database and what options should only exist in Git.