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

DNM - s3_bucket_info - remove call to ListBuckets when name option is specified #2184

Closed
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_updates:
- s3_bucket_info - module should not call ListBuckets when `name` option is specified (https://github.com/ansible-collections/amazon.aws/issues/2183).
44 changes: 16 additions & 28 deletions plugins/modules/s3_bucket_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
name:
description:
- Name of bucket to query.
- This parameter is mutually exclusive with O(name_filter).
type: str
default: ""
version_added: 1.4.0
name_filter:
description:
- Limits buckets to only buckets who's name contain the string in O(name_filter).
- This parameter is mutually exclusive with O(name_filter).
type: str
default: ""
version_added: 1.4.0
Expand Down Expand Up @@ -516,6 +518,10 @@
version_added: 7.2.0
"""

from typing import Any
from typing import Dict
from typing import List

try:
import botocore
except ImportError:
Expand All @@ -528,41 +534,20 @@
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict


def get_bucket_list(module, connection, name="", name_filter=""):
def get_bucket_list(module: AnsibleAWSModule, connection) -> List[Dict[str, Any]]:
"""
Return result of list_buckets json encoded
Filter only buckets matching 'name' or name_filter if defined
:param module:
:param connection:
:return:
"""
buckets = []
filtered_buckets = []
final_buckets = []

# Get all buckets
try:
buckets = camel_dict_to_snake_dict(connection.list_buckets())["buckets"]
return camel_dict_to_snake_dict(connection.list_buckets())["buckets"]
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as err_code:
module.fail_json_aws(err_code, msg="Failed to list buckets")

# Filter buckets if requested
if name_filter:
for bucket in buckets:
if name_filter in bucket["name"]:
filtered_buckets.append(bucket)
elif name:
for bucket in buckets:
if name == bucket["name"]:
filtered_buckets.append(bucket)

# Return proper list (filtered or all)
if name or name_filter:
final_buckets = filtered_buckets
else:
final_buckets = buckets
return final_buckets


def get_buckets_facts(connection, buckets, requested_facts, transform_location):
"""
Expand Down Expand Up @@ -721,13 +706,16 @@ def main():
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)

# Add information about name/name_filter to result
if name:
bucket_list = [{"name": name}]
# Add information about name to result
result["bucket_name"] = name
elif name_filter:
result["bucket_name_filter"] = name_filter
else:
bucket_list = get_bucket_list(module, connection, name, name_filter)
if name_filter:
bucket_list = [bucket for bucket in bucket_list if name_filter in bucket["name"]]
# Add information about name/name_filter to result
result["bucket_name_filter"] = name_filter

# Gather detailed information about buckets if requested
bucket_facts = module.params.get("bucket_facts")
Expand Down
Loading