Skip to content

Commit

Permalink
Add CoreProfile to diff
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarros committed Dec 13, 2024
1 parent 358575d commit 8c7d3b3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
5 changes: 4 additions & 1 deletion backend/infrahub/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,12 @@ def _get_updated_list_value(self, field_name: str, attr_local: list[Any], attr_o

return attr_other

def _get_field_names_for_diff(self) -> list[str]:
return list(self.model_fields.keys())

def diff(self, other: Self) -> HashableModelDiff:
in_both, local_only, other_only = compare_lists(
list1=list(self.model_fields.keys()), list2=list(other.model_fields.keys())
list1=self._get_field_names_for_diff(), list2=other._get_field_names_for_diff()
)
diff_result = HashableModelDiff(added=dict.fromkeys(local_only), removed=dict.fromkeys(other_only))

Expand Down
5 changes: 5 additions & 0 deletions backend/infrahub/core/schema/generic_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ def get_hierarchy_schema(self, db: InfrahubDatabase, branch: Optional[Union[Bran
def get_labels(self) -> list[str]:
"""Return the labels for this object"""
return [self.kind]

def _get_field_names_for_diff(self) -> list[str]:
"""Exclude used_by from the diff for generic nodes"""
fields = super()._get_field_names_for_diff()
return [field for field in fields if field not in ["used_by"]]
2 changes: 1 addition & 1 deletion backend/infrahub/core/schema/schema_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def profile_names(self) -> list[str]:
def get_all_kind_id_map(self, exclude_profiles: bool = False) -> dict[str, str]:
kind_id_map = {}
if exclude_profiles:
names = self.node_names + [gn for gn in self.generic_names if gn != InfrahubKind.PROFILE]
names = self.node_names + self.generic_names
else:
names = self.all_names
for name in names:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ async def test_check_schema_02(self, client: InfrahubClient, branch_1: Branch, l
},
},
},
"LocationGeneric": {"added": {}, "changed": {"used_by": None}, "removed": {}},
},
},
}
Expand Down
20 changes: 19 additions & 1 deletion backend/tests/unit/core/schema_manager/test_manager_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,26 @@ async def test_schema_branch_add_profile_schema(schema_all_in_one):
}


async def test_schema_branch_diff_core_profile(schema_all_in_one):
core_profile_schema = _get_schema_by_kind(core_models, kind=InfrahubKind.PROFILE)
schema_all_in_one["generics"].append(core_profile_schema)

schema = SchemaBranch(cache={}, name="test")
schema.load_schema(schema=SchemaRoot(**schema_all_in_one))
schema.process_inheritance()
schema.manage_profile_schemas()

new_schema = schema.duplicate()
profile_schema = new_schema.get(name=InfrahubKind.PROFILE, duplicate=True)
profile_schema.description = "New description"
new_schema.set(name=InfrahubKind.PROFILE, schema=profile_schema)

diff = new_schema.diff(other=schema)
assert diff.all == ["CoreProfile"]


async def test_schema_branch_add_profile_schema_respects_flag(schema_all_in_one):
core_profile_schema = _get_schema_by_kind(core_models, kind="CoreProfile")
core_profile_schema = _get_schema_by_kind(core_models, kind=InfrahubKind.PROFILE)
schema_all_in_one["generics"].append(core_profile_schema)
builtin_tag_schema = _get_schema_by_kind(schema_all_in_one, kind="BuiltinTag")
builtin_tag_schema["generate_profile"] = False
Expand Down

0 comments on commit 8c7d3b3

Please sign in to comment.