Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s3-lifecycle: fix remove rule with empty prefix #1398

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/1398-s3_lifecycle-no-prefix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- s3_lifecycle - fix bug when deleting rules with an empty prefix (https://github.com/ansible-collections/community.aws/pull/1398).
6 changes: 3 additions & 3 deletions plugins/modules/s3_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ def compare_and_update_configuration(client, module, current_lifecycle_rules, ru
if current_lifecycle_rules:
# If rule ID exists, use that for comparison otherwise compare based on prefix
for existing_rule in current_lifecycle_rules:
if rule.get('ID') == existing_rule.get('ID') and rule['Filter']['Prefix'] != existing_rule.get('Filter', {}).get('Prefix', ''):
if rule.get('ID') == existing_rule.get('ID') and rule['Filter'].get('Prefix', '') != existing_rule.get('Filter', {}).get('Prefix', ''):
existing_rule.pop('ID')
elif rule_id is None and rule['Filter']['Prefix'] == existing_rule.get('Filter', {}).get('Prefix', ''):
elif rule_id is None and rule['Filter'].get('Prefix', '') == existing_rule.get('Filter', {}).get('Prefix', ''):
existing_rule.pop('ID')
if rule.get('ID') == existing_rule.get('ID'):
changed_, appended_ = update_or_append_rule(rule, existing_rule, purge_transitions, lifecycle_configuration)
Expand Down Expand Up @@ -407,7 +407,7 @@ def compare_and_remove_rule(current_lifecycle_rules, rule_id=None, prefix=None):
lifecycle_configuration['Rules'].append(existing_rule)
else:
for existing_rule in current_lifecycle_rules:
if prefix == existing_rule['Filter']['Prefix']:
if prefix == existing_rule['Filter'].get('Prefix', ''):
# We're not keeping the rule (i.e. deleting) so mark as changed
changed = True
else:
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/targets/s3_lifecycle/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,31 @@
that:
- output is not changed

# Check create and delete lifecycle policy with an empty prefix
- name: Create rule with no prefix
s3_lifecycle:
name: "{{ bucket_name }}"
rule_id: empty-prefix
state: present
status: enabled
expiration_days: 30
register: output
- assert:
that:
- output is changed

- name: Delete rule with no prefix
s3_lifecycle:
name: "{{ bucket_name }}"
rule_id: empty-prefix
state: absent
status: enabled
expiration_days: 30
register: output
- assert:
that:
- output is changed

# ============================================================
always:
- name: Ensure all buckets are deleted
Expand Down