From 5e1be68b01d1d38b96cc96faddeb2553035c4b7f Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 11:35:05 +0200 Subject: [PATCH] [PR #8790/573a7b97 backport][stable-9] Fix gitlab_project container_expiration_policy for project create (#8801) Fix gitlab_project container_expiration_policy for project create (#8790) * Fix gitlab_project container_expiration_policy for project create * Check for container_expiration_policy presence before renaming it * Add missing links to changelog fragment * Fix changelog grammar (cherry picked from commit 573a7b97c699c46e8bb37d7b4ea52d711f1b4c07) Co-authored-by: Veikko Virrankoski <71337077+vvirrank@users.noreply.github.com> --- ...-gitlab_project-fix-cleanup-policy-on-project-create.yml | 3 +++ plugins/modules/gitlab_project.py | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/8790-gitlab_project-fix-cleanup-policy-on-project-create.yml diff --git a/changelogs/fragments/8790-gitlab_project-fix-cleanup-policy-on-project-create.yml b/changelogs/fragments/8790-gitlab_project-fix-cleanup-policy-on-project-create.yml new file mode 100644 index 0000000000..ba171a1178 --- /dev/null +++ b/changelogs/fragments/8790-gitlab_project-fix-cleanup-policy-on-project-create.yml @@ -0,0 +1,3 @@ +bugfixes: + - gitlab_project - fix crash caused by old Gitlab projects not having a ``container_expiration_policy`` attribute (https://github.com/ansible-collections/community.general/pull/8790). + - gitlab_project - fix ``container_expiration_policy`` not being applied when creating a new project (https://github.com/ansible-collections/community.general/pull/8790). diff --git a/plugins/modules/gitlab_project.py b/plugins/modules/gitlab_project.py index c98e442cb0..87ad83dbc3 100644 --- a/plugins/modules/gitlab_project.py +++ b/plugins/modules/gitlab_project.py @@ -512,6 +512,8 @@ class GitLabProject(object): return True arguments['namespace_id'] = namespace.id + if 'container_expiration_policy' in arguments: + arguments['container_expiration_policy_attributes'] = arguments['container_expiration_policy'] try: project = self._gitlab.projects.create(arguments) except (gitlab.exceptions.GitlabCreateError) as e: @@ -539,9 +541,9 @@ class GitLabProject(object): for arg_key, arg_value in arguments.items(): if arguments[arg_key] is not None: - if getattr(project, arg_key) != arguments[arg_key]: + if getattr(project, arg_key, None) != arguments[arg_key]: if arg_key == 'container_expiration_policy': - old_val = getattr(project, arg_key) + old_val = getattr(project, arg_key, {}) final_val = {key: value for key, value in arg_value.items() if value is not None} if final_val.get('older_than') == '0d':