From f83108f14d54eba77484449da1dccf1bfa96ecf7 Mon Sep 17 00:00:00 2001 From: Josiah Vranyes <35812920+vranyes@users.noreply.github.com> Date: Wed, 6 Jul 2022 09:26:17 -0500 Subject: [PATCH] Add force_absent parameter to ecs_ecr module (#1316) Add force_absent parameter to ecs_ecr module SUMMARY Adds a force_absent parameter to the ecs_ecr module. ISSUE TYPE Feature Pull Request COMPONENT NAME ecs_ecr module ADDITIONAL INFORMATION It would be useful for the ecs_ecr module to have capability for removing repositories which still contain images. This PR adds that ability by adding an additional parameter force_absent which has a default value of false and essentially just gets passed to the boto3 call for repo deletion. The following was run against a repository with an image in it. - name: Add ecr repos and sync with external sources. hosts: localhost connection: local gather_facts: false tasks: - name: test the changes register: state ecs_ecr: state: absent force_absent: true region: us-east-1 name: myimage/test - debug: var: state This was run with force_absent: true, force_absent: false and force_absent not defined. The expected behavior was seen in all three scenarios. I haven't added any new cases to the integration tests because there doesn't seem to be a great way to sync images into the repo that is created as part of the tests. Reviewed-by: Mark Chappell Reviewed-by: Josiah Vranyes (cherry picked from commit 55923724d4f140bf2dd42ff152cb6f0436c75da8) --- .../fragments/1315-ecs_ecr-force_absent.yaml | 2 ++ plugins/modules/ecs_ecr.py | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/1315-ecs_ecr-force_absent.yaml diff --git a/changelogs/fragments/1315-ecs_ecr-force_absent.yaml b/changelogs/fragments/1315-ecs_ecr-force_absent.yaml new file mode 100644 index 00000000000..d8ad2168fa9 --- /dev/null +++ b/changelogs/fragments/1315-ecs_ecr-force_absent.yaml @@ -0,0 +1,2 @@ +minor_changes: +- ecs_ecr - add `force_absent` parameter for removing repositories that contain images (https://github.com/ansible-collections/community.aws/pull/1316). \ No newline at end of file diff --git a/plugins/modules/ecs_ecr.py b/plugins/modules/ecs_ecr.py index 294459135fe..aa08e97d239 100644 --- a/plugins/modules/ecs_ecr.py +++ b/plugins/modules/ecs_ecr.py @@ -33,6 +33,13 @@ - JSON or dict that represents the new policy. required: false type: json + force_absent: + description: + - If I(force_absent=true), the repository will be removed, even if images are present. + required: false + default: false + type: bool + version_added: 4.1.0 force_set_policy: description: - If I(force_set_policy=false), it prevents setting a policy that would prevent you from @@ -277,10 +284,10 @@ def set_repository_policy(self, registry_id, name, policy_text, force): 'could not find repository {0}'.format(printable)) return - def delete_repository(self, registry_id, name): + def delete_repository(self, registry_id, name, force): if not self.check_mode: repo = self.ecr.delete_repository( - repositoryName=name, **build_kwargs(registry_id)) + repositoryName=name, force=force, **build_kwargs(registry_id)) self.changed = True return repo else: @@ -397,6 +404,7 @@ def run(ecr, params): state = params['state'] policy_text = params['policy'] purge_policy = params['purge_policy'] + force_absent = params['force_absent'] registry_id = params['registry_id'] force_set_policy = params['force_set_policy'] image_tag_mutability = params['image_tag_mutability'].upper() @@ -514,7 +522,7 @@ def run(ecr, params): elif state == 'absent': result['name'] = name if repo: - ecr.delete_repository(registry_id, name) + ecr.delete_repository(registry_id, name, force_absent) result['changed'] = True except Exception as err: @@ -540,6 +548,7 @@ def main(): registry_id=dict(required=False), state=dict(required=False, choices=['present', 'absent'], default='present'), + force_absent=dict(required=False, type='bool', default=False), force_set_policy=dict(required=False, type='bool', default=False), policy=dict(required=False, type='json'), image_tag_mutability=dict(required=False, choices=['mutable', 'immutable'],