Skip to content

Commit

Permalink
s3_lifecycle - ability to set the number of newest noncurrent version…
Browse files Browse the repository at this point in the history
…s to retain
  • Loading branch information
davejames committed Nov 26, 2022
1 parent fad3589 commit aadfa5f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- s3_lifecycle - add parameter `noncurrent_version_keep_newer` to set the number of newest noncurrent versions to retain
15 changes: 12 additions & 3 deletions plugins/modules/s3_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
- The number of days after which non-current versions should be deleted.
required: false
type: int
noncurrent_version_keep_newer:
description:
- The minimum number of non-current versions to retain.
required: false
type: int
noncurrent_version_storage_class:
description:
- The storage class to which non-current versions are transitioned.
Expand Down Expand Up @@ -269,6 +274,7 @@ def build_rule(client, module):
noncurrent_version_transition_days = module.params.get("noncurrent_version_transition_days")
noncurrent_version_transitions = module.params.get("noncurrent_version_transitions")
noncurrent_version_storage_class = module.params.get("noncurrent_version_storage_class")
noncurrent_version_keep_newer = module.params.get("noncurrent_version_keep_newer")
prefix = module.params.get("prefix") or ""
rule_id = module.params.get("rule_id")
status = module.params.get("status")
Expand All @@ -294,10 +300,12 @@ def build_rule(client, module):
rule['Expiration'] = dict(Date=expiration_date.isoformat())
elif expire_object_delete_marker is not None:
rule['Expiration'] = dict(ExpiredObjectDeleteMarker=expire_object_delete_marker)

if noncurrent_version_expiration_days or noncurrent_version_keep_newer:
rule['NoncurrentVersionExpiration'] = dict()
if noncurrent_version_expiration_days is not None:
rule['NoncurrentVersionExpiration'] = dict(NoncurrentDays=noncurrent_version_expiration_days)

rule['NoncurrentVersionExpiration']['NoncurrentDays'] = noncurrent_version_expiration_days
if noncurrent_version_keep_newer is not None:
rule['NoncurrentVersionExpiration']['NewerNoncurrentVersions'] = noncurrent_version_keep_newer
if transition_days is not None:
rule['Transitions'] = [dict(Days=transition_days, StorageClass=storage_class.upper()), ]

Expand Down Expand Up @@ -567,6 +575,7 @@ def main():
expiration_date=dict(),
expire_object_delete_marker=dict(type='bool'),
noncurrent_version_expiration_days=dict(type='int'),
noncurrent_version_keep_newer=dict(type='int'),
noncurrent_version_storage_class=dict(default='glacier', type='str', choices=s3_storage_class),
noncurrent_version_transition_days=dict(type='int'),
noncurrent_version_transitions=dict(type='list', elements='dict'),
Expand Down

0 comments on commit aadfa5f

Please sign in to comment.