-
Notifications
You must be signed in to change notification settings - Fork 47
simpler create/update for Template relationship pools #8535
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
09f90d3
simpler create/update pool handling for relationships on templates
ajtmccarty e27d7b2
use latest python-sdk commit
ajtmccarty 9f3815d
test updates
ajtmccarty df47203
generate graphql schema
ajtmccarty f4b66bb
chore: regenerate GraphQL types
ajtmccarty a2d2105
set _from_resource_pool rels to read_only
ajtmccarty c9213da
update sdk commit
ajtmccarty 52541a6
tiny test fix
ajtmccarty 81740de
Add some clarification regarding an existing test and add another tes…
polmichel 0506f47
update sdk commit: not critical as this is test-only
polmichel 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
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
91 changes: 91 additions & 0 deletions
91
backend/infrahub/graphql/mutations/template_pool_router.py
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,91 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from infrahub.core.constants import InfrahubKind | ||
| from infrahub.core.constants.schema import RESOURCE_POOL_REL_SUFFIX | ||
| from infrahub.exceptions import ValidationError | ||
|
|
||
| if TYPE_CHECKING: | ||
| from infrahub.core.schema.template_schema import TemplateSchema | ||
|
|
||
| POOL_PEER_KINDS = {InfrahubKind.IPADDRESSPOOL, InfrahubKind.IPPREFIXPOOL} | ||
|
|
||
|
|
||
| class TemplatePoolHandler: | ||
| """Routes pool-eligible relationship data to the correct internal relationship. | ||
|
|
||
| For template mutations, a user can provide a pool reference via ``from_pool`` | ||
| on a regular relationship field (e.g., ``primary_ip: { from_pool: { id: "..." } }``). | ||
| This handler detects the ``from_pool`` key and moves the data to the internal | ||
| ``_from_resource_pool`` relationship, using the pool's ``id`` as the relationship peer. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def get_pool_eligible_relationships(schema: TemplateSchema) -> dict[str, str]: | ||
| """Return a mapping of relationship name -> pool relationship name for pool-eligible relationships. | ||
|
|
||
| A relationship pair is only eligible if: | ||
| - A non-suffixed relationship exists (e.g., "prefix") | ||
| - A corresponding "_from_resource_pool" relationship exists (e.g., "prefix_from_resource_pool") | ||
| - The pool relationship's peer is one of the known pool kinds | ||
| """ | ||
| eligible = {} | ||
| pool_rel_names = {rel.name for rel in schema.relationships if rel.name.endswith(RESOURCE_POOL_REL_SUFFIX)} | ||
|
|
||
| for rel in schema.relationships: | ||
| if rel.name.endswith(RESOURCE_POOL_REL_SUFFIX): | ||
| continue | ||
| pool_rel_name = f"{rel.name}{RESOURCE_POOL_REL_SUFFIX}" | ||
| if pool_rel_name not in pool_rel_names: | ||
| continue | ||
| pool_rel = schema.get_relationship(pool_rel_name) | ||
| if pool_rel.peer in POOL_PEER_KINDS: | ||
| eligible[rel.name] = pool_rel_name | ||
| return eligible | ||
|
|
||
| @staticmethod | ||
| def route_relationships( | ||
| data: dict[str, Any], | ||
| schema: TemplateSchema, | ||
| ) -> dict[str, Any]: | ||
| """Route relationship data to the correct internal relationship based on from_pool presence. | ||
|
|
||
| For pool-eligible relationships on templates, if the user provides ``from_pool`` | ||
| in the relationship data, the pool reference is extracted and moved to the | ||
| ``_from_resource_pool`` relationship key. The direct relationship is cleared. | ||
|
|
||
| If ``from_pool`` is not present (just ``id`` or ``hfid``), the data stays on the | ||
| direct relationship and the pool relationship is cleared. | ||
| """ | ||
| eligible = TemplatePoolHandler.get_pool_eligible_relationships(schema) | ||
|
|
||
| for rel_name, pool_rel_name in eligible.items(): | ||
| if rel_name not in data: | ||
| continue | ||
|
|
||
| rel_data = data[rel_name] | ||
|
|
||
| # When the user sends null, clear both the direct and pool relationships | ||
| if rel_data is None: | ||
| data[pool_rel_name] = None | ||
| continue | ||
|
|
||
| if not isinstance(rel_data, dict): | ||
| continue | ||
|
|
||
| from_pool = rel_data.get("from_pool") | ||
| has_direct = rel_data.get("id") is not None or rel_data.get("hfid") is not None | ||
| if from_pool is not None and has_direct: | ||
| raise ValidationError( | ||
| input_value=f"Cannot specify both a direct reference and from_pool on '{rel_name}'" | ||
| ) | ||
| if from_pool is not None: | ||
| # Route to the pool relationship using the pool's id from from_pool | ||
| data[pool_rel_name] = {"id": from_pool["id"]} | ||
| data[rel_name] = None | ||
| # Direct relationship — clear the pool counterpart to allow swapping | ||
| elif pool_rel_name not in data: | ||
| data[pool_rel_name] = None | ||
|
|
||
| return data |
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
Empty file.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.