Skip to content

Commit

Permalink
Use boto3_tag_list_to_ansible_dict for bucket tags
Browse files Browse the repository at this point in the history
  • Loading branch information
zeten30 committed Nov 19, 2020
1 parent 63a90fe commit b04699a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
38 changes: 32 additions & 6 deletions plugins/modules/aws_s3_bucket_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@
pass # Handled by AnsibleAWSModule

from ansible.module_utils._text import to_native

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry, boto3_tag_list_to_ansible_dict
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict


Expand All @@ -199,11 +198,12 @@ def get_bucket_list(module, connection, name="", name_filter=""):
buckets = []
filtered_buckets = []
final_buckets = []

# Get all buckets
try:
buckets = camel_dict_to_snake_dict(connection.list_buckets())['buckets']
except botocore.exceptions.ClientError as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response))
except botocore.exceptions.ClientError as err_code:
module.fail_json(msg=to_native(err_code), exception=traceback.format_exc(), **camel_dict_to_snake_dict(err_code.response))

# Filter buckets if requested
if name_filter:
Expand Down Expand Up @@ -251,6 +251,13 @@ def get_bucket_details(connection, name, requested_facts, transform_location):
# we just pass on error - error means that resources is undefined
except botocore.exceptions.ClientError:
pass
elif key == 'bucket_tagging':
all_facts[key] = {}
try:
all_facts[key] = get_bucket_tagging(name, connection)
# we just pass on error - error means that resources is undefined
except botocore.exceptions.ClientError:
pass
else:
all_facts[key] = {}
try:
Expand Down Expand Up @@ -284,6 +291,25 @@ def get_bucket_location(name, connection, transform_location=False):
return(data)


@AWSRetry.exponential_backoff(max_delay=120, catch_extra_error_codes=['NoSuchBucket', 'OperationAborted'])
def get_bucket_tagging(name, connection):
"""
Get bucket tags and transform them using `boto3_tag_list_to_ansible_dict` function
"""
data = connection.get_bucket_tagging(Bucket=name)

try:
bucket_tags = boto3_tag_list_to_ansible_dict(data['TagSet'])
return(bucket_tags)
except KeyError:
# Strip response metadata (not needed)
try:
data.pop('ResponseMetadata')
return(data)
except KeyError:
return(data)


@AWSRetry.exponential_backoff(max_delay=120, catch_extra_error_codes=['NoSuchBucket', 'OperationAborted'])
def get_bucket_property(name, connection, get_api_name):
"""
Expand Down Expand Up @@ -355,8 +381,8 @@ def main():
connection = {}
try:
connection = module.client('s3')
except (connection.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='Failed to connect to AWS')
except (connection.exceptions.ClientError, botocore.exceptions.BotoCoreError) as err_code:
module.fail_json_aws(err_code, msg='Failed to connect to AWS')

# Get basic bucket list (name + creation date)
bucket_list = get_bucket_list(module, connection, name, name_filter)
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/targets/aws_s3_bucket_info/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@
that:
- item.bucket_acl.Owner is defined
- item.bucket_location.LocationConstraint is search(aws_region)
- item.bucket_tagging.TagSet is defined
- item.bucket_tagging.TagSet is search("defined")
- item.bucket_tagging.TagSet is search("verified")
- item.bucket_tagging.facts is search("defined")
- item.bucket_tagging.integration is search("verified")
loop: "{{ bucket_list.buckets }}"
loop_control:
label: "{{ item.name }}"
Expand Down

0 comments on commit b04699a

Please sign in to comment.