Skip to content

Commit 45584a1

Browse files
authored
IFC-1952 Add generic/attribute relationships in profile schema
This change is relatively simple and just adds relationships of kind generic and kind attribute to profile schemas corresponding to node schemas having these relationships. When we load a schema, we make sure to clear all the relationships, except the ones of kind PROFILE such as related_nodes, to create a brand new list of relationships which will account for deleted and new ones. This is very similar to what we do for templates already.
1 parent 4a6b778 commit 45584a1

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

backend/infrahub/core/schema/schema_branch.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,36 @@ def add_hierarchy_node(self) -> None:
21492149

21502150
self.set(name=node_name, schema=node)
21512151

2152+
def add_relationships_to_profile(self, profile: ProfileSchema, node: NodeSchema | GenericSchema) -> None:
2153+
# Remove previous relationships to account for new ones
2154+
profile.relationships = [r for r in profile.relationships if r.kind == RelationshipKind.PROFILE]
2155+
2156+
for relationship in node.relationships:
2157+
if relationship.kind not in [RelationshipKind.ATTRIBUTE, RelationshipKind.GENERIC]:
2158+
continue
2159+
2160+
identifier = (
2161+
f"profile_{relationship.identifier}"
2162+
if relationship.identifier
2163+
else self._generate_identifier_string(profile.kind, relationship.peer)
2164+
)
2165+
2166+
profile.relationships.append(
2167+
RelationshipSchema(
2168+
name=relationship.name,
2169+
peer=relationship.peer,
2170+
kind=relationship.kind,
2171+
cardinality=relationship.cardinality,
2172+
direction=relationship.direction,
2173+
branch=relationship.branch,
2174+
identifier=identifier,
2175+
min_count=relationship.min_count,
2176+
max_count=relationship.max_count,
2177+
label=relationship.label,
2178+
inherited=False,
2179+
)
2180+
)
2181+
21522182
def manage_profile_schemas(self) -> None:
21532183
if not self.has(name=InfrahubKind.PROFILE):
21542184
# TODO: This logic is actually only for testing purposes as since 1.0.9 CoreProfile is loaded in db.
@@ -2170,6 +2200,7 @@ def manage_profile_schemas(self) -> None:
21702200
continue
21712201

21722202
profile = self.generate_profile_from_node(node=node)
2203+
self.add_relationships_to_profile(profile=profile, node=node)
21732204
self.set(name=profile.kind, schema=profile)
21742205
profile_schema_kinds.add(profile.kind)
21752206

@@ -2253,13 +2284,13 @@ def generate_profile_from_node(self, node: NodeSchema) -> ProfileSchema:
22532284
core_name_attr = core_profile_schema.get_attribute(name="profile_name")
22542285
name_attr_schema_class = get_attribute_schema_class_for_kind(kind=core_name_attr.kind)
22552286
profile_name_attr = name_attr_schema_class(
2256-
**core_name_attr.model_dump(exclude=["id", "inherited"]),
2287+
**core_name_attr.model_dump(exclude={"id", "inherited"}),
22572288
)
22582289
profile_name_attr.branch = node.branch
22592290
core_priority_attr = core_profile_schema.get_attribute(name="profile_priority")
22602291
priority_attr_schema_class = get_attribute_schema_class_for_kind(kind=core_priority_attr.kind)
22612292
profile_priority_attr = priority_attr_schema_class(
2262-
**core_priority_attr.model_dump(exclude=["id", "inherited"]),
2293+
**core_priority_attr.model_dump(exclude={"id", "inherited"}),
22632294
)
22642295
profile_priority_attr.branch = node.branch
22652296
profile = ProfileSchema(
@@ -2292,7 +2323,7 @@ def generate_profile_from_node(self, node: NodeSchema) -> ProfileSchema:
22922323
attr_schema_class = get_attribute_schema_class_for_kind(kind=node_attr.kind)
22932324
attr = attr_schema_class(
22942325
optional=True,
2295-
**node_attr.model_dump(exclude=["id", "unique", "optional", "read_only", "default_value", "inherited"]),
2326+
**node_attr.model_dump(exclude={"id", "unique", "optional", "read_only", "default_value", "inherited"}),
22962327
)
22972328
profile.attributes.append(attr)
22982329

@@ -2428,9 +2459,7 @@ def generate_object_template_from_node(
24282459
)
24292460
core_name_attr = core_template_schema.get_attribute(name=OBJECT_TEMPLATE_NAME_ATTR)
24302461
name_attr_schema_class = get_attribute_schema_class_for_kind(kind=core_name_attr.kind)
2431-
template_name_attr = name_attr_schema_class(
2432-
**core_name_attr.model_dump(exclude=["id", "inherited"]),
2433-
)
2462+
template_name_attr = name_attr_schema_class(**core_name_attr.model_dump(exclude={"id", "inherited"}))
24342463
template_name_attr.branch = node.branch
24352464

24362465
template: TemplateSchema | GenericSchema
@@ -2489,7 +2518,7 @@ def generate_object_template_from_node(
24892518
attr_schema_class = get_attribute_schema_class_for_kind(kind=node_attr.kind)
24902519
attr = attr_schema_class(
24912520
optional=node_attr.optional if is_autogenerated_subtemplate else True,
2492-
**node_attr.model_dump(exclude=["id", "unique", "optional", "read_only", "order_weight"]),
2521+
**node_attr.model_dump(exclude={"id", "unique", "optional", "read_only", "order_weight"}),
24932522
)
24942523
template.attributes.append(attr)
24952524

backend/tests/unit/core/schema_manager/test_manager_schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,10 @@ async def test_schema_branch_add_profile_schema(schema_all_in_one) -> None:
720720
assert node_profile.get_attribute("profile_name").branch == BranchSupportType.AGNOSTIC.value
721721
assert node_profile.get_attribute("profile_priority").branch == BranchSupportType.AGNOSTIC.value
722722
assert set(node_profile.attribute_names) == {"profile_name", "profile_priority", "description", "mybool"}
723+
assert set(node_profile.relationship_names) == {"badges", "primary_tag", "related_nodes", "status", "tags"}
723724
generic_profile = schema.get(name="ProfileInfraGenericInterface", duplicate=False)
724725
assert set(generic_profile.attribute_names) == {"profile_name", "profile_priority", "mybool"}
726+
assert set(generic_profile.relationship_names) == {"badges", "primary_tag", "related_nodes", "status"}
725727
core_profile_schema = schema.get("CoreProfile")
726728
core_node_schema = schema.get("CoreNode")
727729
assert set(core_profile_schema.used_by) == {

0 commit comments

Comments
 (0)