-
Notifications
You must be signed in to change notification settings - Fork 28
Add common_parent
relationship attribute
#6626
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3964228
Add `common_parent` relationship attribute
gmazoyer 496fbeb
Add schema validation when `common_parent` is set
gmazoyer 7b8022d
Add peer parent relationship constraint validator
gmazoyer d387c92
Support peer parent validation in `RelationshipAdd`
gmazoyer 4d228a7
Fix typo
gmazoyer 364060a
Add function test
gmazoyer 3b81750
Really add test
gmazoyer 9ff96b7
Remove extra `from None` on exception raise
gmazoyer 46bc442
Add some comments
gmazoyer f7d9fe7
Set `common_*` rel attribute update support to allowed
gmazoyer 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
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
from infrahub.core.node import Node | ||
from infrahub.core.schema import MainSchemaTypes | ||
|
||
from ..model import RelationshipManager | ||
|
||
|
||
class RelationshipManagerConstraintInterface(ABC): | ||
@abstractmethod | ||
async def check(self, relm: RelationshipManager, node_schema: MainSchemaTypes) -> None: ... | ||
async def check(self, relm: RelationshipManager, node_schema: MainSchemaTypes, node: Node) -> None: ... |
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
56 changes: 56 additions & 0 deletions
56
backend/infrahub/core/relationship/constraints/peer_parent.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,56 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Mapping | ||
|
||
from infrahub.exceptions import ValidationError | ||
|
||
from .interface import RelationshipManagerConstraintInterface | ||
|
||
if TYPE_CHECKING: | ||
from infrahub.core.branch import Branch | ||
from infrahub.core.node import Node | ||
from infrahub.core.schema import MainSchemaTypes | ||
from infrahub.database import InfrahubDatabase | ||
|
||
from ..model import RelationshipManager | ||
|
||
|
||
class RelationshipPeerParentConstraint(RelationshipManagerConstraintInterface): | ||
def __init__(self, db: InfrahubDatabase, branch: Branch | None = None): | ||
self.db = db | ||
self.branch = branch | ||
|
||
async def _check_relationship_peers_parent( | ||
self, relm: RelationshipManager, parent_rel_name: str, node: Node, peers: Mapping[str, Node] | ||
) -> None: | ||
"""Validate that all peers of a given `relm` have the same parent for the given `relationship_name`.""" | ||
node_parent = await node.get_parent_relationship_peer(db=self.db, name=parent_rel_name) | ||
if not node_parent: | ||
# If the schema is properly validated we are not expecting this to happen | ||
raise ValidationError(f"Node {node.id} ({node.get_kind()}) does not have a parent peer") | ||
|
||
parents: set[str] = {node_parent.id} | ||
for peer in peers.values(): | ||
parent = await peer.get_parent_relationship_peer(db=self.db, name=parent_rel_name) | ||
if not parent: | ||
# If the schema is properly validated we are not expecting this to happen | ||
raise ValidationError(f"Peer {peer.id} ({peer.get_kind()}) does not have a parent peer") | ||
parents.add(parent.id) | ||
|
||
if len(parents) != 1: | ||
raise ValidationError( | ||
f"All the elements of the '{relm.name}' relationship on node {node.id} ({node.get_kind()}) must have the same parent " | ||
"as the node" | ||
) | ||
|
||
async def check(self, relm: RelationshipManager, node_schema: MainSchemaTypes, node: Node) -> None: # noqa: ARG002 | ||
if not relm.schema.common_parent: | ||
return | ||
|
||
peers = await relm.get_peers(db=self.db) | ||
if not peers: | ||
return | ||
|
||
await self._check_relationship_peers_parent( | ||
relm=relm, parent_rel_name=relm.schema.common_parent, node=node, peers=peers | ||
) |
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
8 changes: 8 additions & 0 deletions
8
backend/infrahub/dependencies/builder/constraint/relationship_manager/peer_parent.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,8 @@ | ||
from infrahub.core.relationship.constraints.peer_parent import RelationshipPeerParentConstraint | ||
from infrahub.dependencies.interface import DependencyBuilder, DependencyBuilderContext | ||
|
||
|
||
class RelationshipPeerParentConstraintDependency(DependencyBuilder[RelationshipPeerParentConstraint]): | ||
@classmethod | ||
def build(cls, context: DependencyBuilderContext) -> RelationshipPeerParentConstraint: | ||
return RelationshipPeerParentConstraint(db=context.db, branch=context.branch) |
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.
Oops, something went wrong.
Add this suggestion to a batch that c
3139
an 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.
As we're adding the node as an argument here and we can access the schema with node.get_schema() the node_schema parameter gets a bit redundant now. But we can perhaps consider removing that as a cleanup step.
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.
I will do that in a dedicated PR as it needs to change some code in other constraint checkers.