From 1c610130d6f5de44abdd08a101c0c1a5c53650c5 Mon Sep 17 00:00:00 2001 From: Patrick Ogenstad Date: Fri, 29 Nov 2024 12:45:51 +0100 Subject: [PATCH] Support updating artifact definition targets Fixes #5060 --- backend/infrahub/git/integrator.py | 8 +++++++- backend/infrahub/git/tasks.py | 4 +++- changelog/5060.fixed.md | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changelog/5060.fixed.md diff --git a/backend/infrahub/git/integrator.py b/backend/infrahub/git/integrator.py index 989b912c45..1617479a70 100644 --- a/backend/infrahub/git/integrator.py +++ b/backend/infrahub/git/integrator.py @@ -312,7 +312,9 @@ async def import_artifact_definitions( artifact_defs_in_graph = { artdef.name.value: artdef - for artdef in await self.sdk.filters(kind=CoreArtifactDefinition, branch=branch_name) + for artdef in await self.sdk.filters( + kind=CoreArtifactDefinition, branch=branch_name, prefetch_relationships=True, populate_store=True + ) } local_artifact_defs: dict[str, InfrahubRepositoryArtifactDefinitionConfig] = {} @@ -380,6 +382,7 @@ async def compare_artifact_definition( existing_artifact_definition.artifact_name.value != local_artifact_definition.artifact_name or existing_artifact_definition.parameters.value != local_artifact_definition.parameters or existing_artifact_definition.content_type.value != local_artifact_definition.content_type + or existing_artifact_definition.targets.peer.name.value != local_artifact_definition.targets ): return False @@ -399,6 +402,9 @@ async def update_artifact_definition( if existing_artifact_definition.content_type.value != local_artifact_definition.content_type: existing_artifact_definition.content_type.value = local_artifact_definition.content_type + if existing_artifact_definition.targets.peer.name.value != local_artifact_definition.targets: + existing_artifact_definition.targets = local_artifact_definition.targets + await existing_artifact_definition.save() async def get_repository_config(self, branch_name: str, commit: str) -> Optional[InfrahubRepositoryConfig]: diff --git a/backend/infrahub/git/tasks.py b/backend/infrahub/git/tasks.py index babb6837ac..4d7155eb25 100644 --- a/backend/infrahub/git/tasks.py +++ b/backend/infrahub/git/tasks.py @@ -174,6 +174,7 @@ async def generate_request_artifact_definition(model: RequestArtifactDefinitionG await artifact_definition.targets.fetch() group = artifact_definition.targets.peer await group.members.fetch() + current_members = [member.id for member in group.members.peers] existing_artifacts = await service.client.filters( kind=InfrahubKind.ARTIFACT, @@ -183,7 +184,8 @@ async def generate_request_artifact_definition(model: RequestArtifactDefinitionG ) artifacts_by_member = {} for artifact in existing_artifacts: - artifacts_by_member[artifact.object.peer.id] = artifact.id + if artifact.object.id in current_members: + artifacts_by_member[artifact.object.peer.id] = artifact.id await artifact_definition.transformation.fetch() transformation_repository = artifact_definition.transformation.peer.repository diff --git a/changelog/5060.fixed.md b/changelog/5060.fixed.md new file mode 100644 index 0000000000..b1d452f82e --- /dev/null +++ b/changelog/5060.fixed.md @@ -0,0 +1 @@ +Fix artifact definition targets when changed in repo so that it's reflected in the database