@@ -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
0 commit comments