From 986f52f864f61e5bb59bfd0cb6fe0a84c467b17e Mon Sep 17 00:00:00 2001 From: Damien Garros Date: Thu, 19 Dec 2024 14:30:33 +0100 Subject: [PATCH] Fix issue with menu load due to missing direction --- changelog/+3bf1c5c.fixed.md | 1 + infrahub_sdk/schema.py | 22 ++++++++++++++++++++++ infrahub_sdk/spec/object.py | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 changelog/+3bf1c5c.fixed.md diff --git a/changelog/+3bf1c5c.fixed.md b/changelog/+3bf1c5c.fixed.md new file mode 100644 index 00000000..c18b00ed --- /dev/null +++ b/changelog/+3bf1c5c.fixed.md @@ -0,0 +1 @@ +Fix an issue with with `infrahubctl menu load` that would fail while loading the menu \ No newline at end of file diff --git a/infrahub_sdk/schema.py b/infrahub_sdk/schema.py index 9d5135d9..f04444ee 100644 --- a/infrahub_sdk/schema.py +++ b/infrahub_sdk/schema.py @@ -282,6 +282,12 @@ class RelationshipCardinality(str, Enum): MANY = "many" +class RelationshipDirection(str, Enum): + BIDIR = "bidirectional" + OUTBOUND = "outbound" + INBOUND = "inbound" + + class BranchSupportType(str, Enum): AWARE = "aware" AGNOSTIC = "agnostic" @@ -339,6 +345,7 @@ class RelationshipSchema(BaseModel): state: SchemaState = SchemaState.PRESENT name: str peer: str + direction: RelationshipDirection = RelationshipDirection.BIDIR kind: RelationshipKind = RelationshipKind.GENERIC label: Optional[str] = None description: Optional[str] = None @@ -413,6 +420,21 @@ def get_relationship_by_identifier(self, id: str, raise_on_error: bool = True) - raise ValueError(f"Unable to find the relationship {id}") + def get_matching_relationship( + self, id: str, direction: RelationshipDirection = RelationshipDirection.BIDIR + ) -> RelationshipSchema: + valid_direction = RelationshipDirection.BIDIR + if direction == RelationshipDirection.INBOUND: + valid_direction = RelationshipDirection.OUTBOUND + elif direction == RelationshipDirection.OUTBOUND: + valid_direction = RelationshipDirection.INBOUND + + for item in self.relationships: + if item.identifier == id and item.direction == valid_direction: + return item + + raise ValueError(f"Unable to find the relationship {id} / ({valid_direction.value})") + @property def attribute_names(self) -> list[str]: return [item.name for item in self.attributes] diff --git a/infrahub_sdk/spec/object.py b/infrahub_sdk/spec/object.py index b9efb20d..24278f47 100644 --- a/infrahub_sdk/spec/object.py +++ b/infrahub_sdk/spec/object.py @@ -78,7 +78,7 @@ async def create_node( if rel_schema.identifier is None: raise ValueError("identifier must be defined") - peer_rel = peer_schema.get_relationship_by_identifier(id=rel_schema.identifier) + peer_rel = peer_schema.get_matching_relationship(id=rel_schema.identifier, direction=rel_schema.direction) rel_data = data[rel]["data"] context = {}