Skip to content

Commit

Permalink
Add force_absent parameter to ecs_ecr module (#1316)
Browse files Browse the repository at this point in the history
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 <None>
Reviewed-by: Josiah Vranyes <None>
  • Loading branch information
vranyes authored Jul 6, 2022
1 parent 76fa1ef commit 5592372
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1315-ecs_ecr-force_absent.yaml
Original file line number Diff line number Diff line change
@@ -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).
15 changes: 12 additions & 3 deletions plugins/modules/ecs_ecr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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'],
Expand Down

0 comments on commit 5592372

Please sign in to comment.