Skip to content

Commit

Permalink
s3_sync - add storage_class feature (ansible-collections#497)
Browse files Browse the repository at this point in the history
* s3_sync - add support for choosing storage_class when objects are added to an S3 bucket.

Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis authored Mar 24, 2021
1 parent a560fa1 commit 77c646b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/497-s3_sync-add-storage_class.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- s3_sync - new ``storage_class`` feature allowing to specify the storage class when any object is added to an S3 bucket (https://github.com/ansible-collections/community.aws/issues/358).
30 changes: 29 additions & 1 deletion plugins/modules/s3_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@
- Directives are separated by commas.
required: false
type: str
storage_class:
description:
- Storage class to be associated to each object added to the S3 bucket.
required: false
choices:
- 'STANDARD'
- 'REDUCED_REDUNDANCY'
- 'STANDARD_IA'
- 'ONEZONE_IA'
- 'INTELLIGENT_TIERING'
- 'GLACIER'
- 'DEEP_ARCHIVE'
- 'OUTPOSTS'
default: 'STANDARD'
type: str
version_added: 1.5.0
delete:
description:
- Remove remote files that exist in bucket but are not present in the file root.
Expand Down Expand Up @@ -131,6 +147,12 @@
bucket: tedder
file_root: roles/s3/files/
- name: basic upload using the glacier storage class
community.aws.s3_sync:
bucket: tedder
file_root: roles/s3/files/
storage_class: GLACIER
- name: all the options
community.aws.s3_sync:
bucket: tedder
Expand All @@ -142,6 +164,7 @@
file_change_strategy: force
permission: public-read
cache_control: "public, max-age=31536000"
storage_class: "GLACIER"
include: "*"
exclude: "*.txt,.*"
'''
Expand Down Expand Up @@ -470,6 +493,8 @@ def upload_files(s3, bucket, filelist, params):
args['ACL'] = params['permission']
if params.get('cache_control'):
args['CacheControl'] = params['cache_control']
if params.get('storage_class'):
args['StorageClass'] = params['storage_class']
# if this fails exception is caught in main()
s3.upload_file(entry['fullpath'], bucket, entry['s3_path'], ExtraArgs=args, Callback=None, Config=None)
ret.append(entry)
Expand Down Expand Up @@ -507,7 +532,10 @@ def main():
include=dict(required=False, default="*"),
cache_control=dict(required=False, default=''),
delete=dict(required=False, type='bool', default=False),
# future options: encoding, metadata, storage_class, retries
storage_class=dict(required=False, default='STANDARD',
choices=['STANDARD', 'REDUCED_REDUNDANCY', 'STANDARD_IA', 'ONEZONE_IA',
'INTELLIGENT_TIERING', 'GLACIER', 'DEEP_ARCHIVE', 'OUTPOSTS']),
# future options: encoding, metadata, retries
)

module = AnsibleAWSModule(
Expand Down
28 changes: 27 additions & 1 deletion tests/integration/targets/s3_sync/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
dd if=/dev/zero of=test4.txt bs=1M count=10
args:
chdir: "{{ output_dir }}/s3_sync"

- name: Sync files with remote bucket
s3_sync:
bucket: "{{ resource_prefix }}-testbucket-ansible"
Expand All @@ -55,6 +55,30 @@
- output is changed

# ============================================================
- name: Create a second s3_bucket
s3_bucket:
name: "{{ resource_prefix }}-testbucket-ansible-2"
state: present
register: output

- assert:
that:
- output.changed
- output.name == '{{ resource_prefix }}-testbucket-ansible-2'
- not output.requester_pays

- name: Sync files with remote bucket using glacier storage class
s3_sync:
bucket: "{{ resource_prefix }}-testbucket-ansible-2"
file_root: "{{ output_dir }}/s3_sync"
storage_class: "GLACIER"
register: output

- assert:
that:
- output is changed
# ============================================================

- name: Sync files already present
s3_sync:
bucket: "{{ resource_prefix }}-testbucket-ansible"
Expand All @@ -74,6 +98,7 @@
- assert:
that:
- output is not changed


# ============================================================
# DOCUMENTATION EXAMPLES
Expand Down Expand Up @@ -106,3 +131,4 @@
ignore_errors: yes
with_items:
- "{{ resource_prefix }}-testbucket-ansible"
- "{{ resource_prefix }}-testbucket-ansible-2"

0 comments on commit 77c646b

Please sign in to comment.