| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: s3_metrics_configuration |
| 10 |
version_added: 1.3.0 |
| 11 |
short_description: Manage s3 bucket metrics configuration in AWS |
| 12 |
description: |
| 13 |
- Manage s3 bucket metrics configuration in AWS which allows to get the CloudWatch request metrics for the objects in a bucket |
| 14 |
author: |
| 15 |
- Dmytro Vorotyntsev (@vorotech) |
| 16 |
notes: |
| 17 |
- This modules manages single metrics configuration, the s3 bucket might have up to 1,000 metrics configurations |
| 18 |
- To request metrics for the entire bucket, create a metrics configuration without a filter |
| 19 |
- Metrics configurations are necessary only to enable request metric, bucket-level daily storage metrics are always turned on |
| 20 |
options: |
| 21 |
bucket_name: |
| 22 |
description: |
| 23 |
- "Name of the s3 bucket" |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
id: |
| 27 |
description: |
| 28 |
- "The ID used to identify the metrics configuration" |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
filter_prefix: |
| 32 |
description: |
| 33 |
- "A prefix used when evaluating a metrics filter" |
| 34 |
required: false |
| 35 |
type: str |
| 36 |
filter_tags: |
| 37 |
description: |
| 38 |
- "A dictionary of one or more tags used when evaluating a metrics filter" |
| 39 |
required: false |
| 40 |
aliases: ['filter_tag'] |
| 41 |
type: dict |
| 42 |
default: {} |
| 43 |
state: |
| 44 |
description: |
| 45 |
- "Create or delete metrics configuration" |
| 46 |
default: present |
| 47 |
choices: ['present', 'absent'] |
| 48 |
type: str |
| 49 |
extends_documentation_fragment: |
| 50 |
- amazon.aws.common.modules |
| 51 |
- amazon.aws.region.modules |
| 52 |
- amazon.aws.boto3 |
| 53 |
""" |
| 54 |
|
| 55 |
RETURN = r""" # """ |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 59 |
|
| 60 |
- name: Create a metrics configuration that enables metrics for an entire bucket |
| 61 |
community.aws.s3_metrics_configuration: |
| 62 |
bucket_name: my-bucket |
| 63 |
id: EntireBucket |
| 64 |
state: present |
| 65 |
|
| 66 |
- name: Put a metrics configuration that enables metrics for objects starting with a prefix |
| 67 |
community.aws.s3_metrics_configuration: |
| 68 |
bucket_name: my-bucket |
| 69 |
id: Assets |
| 70 |
filter_prefix: assets |
| 71 |
state: present |
| 72 |
|
| 73 |
- name: Put a metrics configuration that enables metrics for objects with specific tag |
| 74 |
community.aws.s3_metrics_configuration: |
| 75 |
bucket_name: my-bucket |
| 76 |
id: Assets |
| 77 |
filter_tag: |
| 78 |
kind: asset |
| 79 |
state: present |
| 80 |
|
| 81 |
- name: Put a metrics configuration that enables metrics for objects that start with a particular prefix and have specific tags applied |
| 82 |
community.aws.s3_metrics_configuration: |
| 83 |
bucket_name: my-bucket |
| 84 |
id: ImportantBlueDocuments |
| 85 |
filter_prefix: documents |
| 86 |
filter_tags: |
| 87 |
priority: high |
| 88 |
class: blue |
| 89 |
state: present |
| 90 |
|
| 91 |
- name: Delete metrics configuration |
| 92 |
community.aws.s3_metrics_configuration: |
| 93 |
bucket_name: my-bucket |
| 94 |
id: EntireBucket |
| 95 |
state: absent |
| 96 |
""" |
| 97 |
|
| 98 |
try: |
| 99 |
from botocore.exceptions import BotoCoreError |
| 100 |
from botocore.exceptions import ClientError |
| 101 |
except ImportError: |
| 102 |
pass # Handled by AnsibleAWSModule |
| 103 |
|
| 104 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 105 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 106 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 107 |
|
| 108 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 109 |
|
| 110 |
|
| 111 |
def _create_metrics_configuration(mc_id, filter_prefix, filter_tags): |
| 112 |
payload = {"Id": mc_id} |
| 113 |
# Just a filter_prefix or just a single tag filter is a special case |
| 114 |
if filter_prefix and not filter_tags: |
| 115 |
payload["Filter"] = {"Prefix": filter_prefix} |
| 116 |
elif not filter_prefix and len(filter_tags) == 1: |
| 117 |
payload["Filter"] = {"Tag": ansible_dict_to_boto3_tag_list(filter_tags)[0]} |
| 118 |
# Otherwise we need to use 'And' |
| 119 |
elif filter_tags: |
| 120 |
payload["Filter"] = {"And": {"Tags": ansible_dict_to_boto3_tag_list(filter_tags)}} |
| 121 |
if filter_prefix: |
| 122 |
payload["Filter"]["And"]["Prefix"] = filter_prefix |
| 123 |
|
| 124 |
return payload |
| 125 |
|
| 126 |
|
| 127 |
def create_or_update_metrics_configuration(client, module): |
| 128 |
bucket_name = module.params.get("bucket_name") |
| 129 |
mc_id = module.params.get("id") |
| 130 |
filter_prefix = module.params.get("filter_prefix") |
| 131 |
filter_tags = module.params.get("filter_tags") |
| 132 |
|
| 133 |
try: |
| 134 |
response = client.get_bucket_metrics_configuration(aws_retry=True, Bucket=bucket_name, Id=mc_id) |
| 135 |
metrics_configuration = response["MetricsConfiguration"] |
| 136 |
except is_boto3_error_code("NoSuchConfiguration"): |
| 137 |
metrics_configuration = None |
| 138 |
except (BotoCoreError, ClientError) as e: # pylint: disable=duplicate-except |
| 139 |
module.fail_json_aws(e, msg="Failed to get bucket metrics configuration") |
| 140 |
|
| 141 |
new_configuration = _create_metrics_configuration(mc_id, filter_prefix, filter_tags) |
| 142 |
|
| 143 |
if metrics_configuration: |
| 144 |
if metrics_configuration == new_configuration: |
| 145 |
module.exit_json(changed=False) |
| 146 |
|
| 147 |
if module.check_mode: |
| 148 |
module.exit_json(changed=True) |
| 149 |
|
| 150 |
try: |
| 151 |
client.put_bucket_metrics_configuration( |
| 152 |
aws_retry=True, Bucket=bucket_name, Id=mc_id, MetricsConfiguration=new_configuration |
| 153 |
) |
| 154 |
except (BotoCoreError, ClientError) as e: # pylint: disable=duplicate-except |
| 155 |
module.fail_json_aws(e, msg=f"Failed to put bucket metrics configuration '{mc_id}'") |
| 156 |
|
| 157 |
module.exit_json(changed=True) |
| 158 |
|
| 159 |
|
| 160 |
def delete_metrics_configuration(client, module): |
| 161 |
bucket_name = module.params.get("bucket_name") |
| 162 |
mc_id = module.params.get("id") |
| 163 |
|
| 164 |
try: |
| 165 |
client.get_bucket_metrics_configuration(aws_retry=True, Bucket=bucket_name, Id=mc_id) |
| 166 |
except is_boto3_error_code("NoSuchConfiguration"): |
| 167 |
module.exit_json(changed=False) |
| 168 |
except (BotoCoreError, ClientError) as e: # pylint: disable=duplicate-except |
| 169 |
module.fail_json_aws(e, msg="Failed to get bucket metrics configuration") |
| 170 |
|
| 171 |
if module.check_mode: |
| 172 |
module.exit_json(changed=True) |
| 173 |
|
| 174 |
try: |
| 175 |
client.delete_bucket_metrics_configuration(aws_retry=True, Bucket=bucket_name, Id=mc_id) |
| 176 |
except is_boto3_error_code("NoSuchConfiguration"): |
| 177 |
module.exit_json(changed=False) |
| 178 |
except (BotoCoreError, ClientError) as e: # pylint: disable=duplicate-except |
| 179 |
module.fail_json_aws(e, msg=f"Failed to delete bucket metrics configuration '{mc_id}'") |
| 180 |
|
| 181 |
module.exit_json(changed=True) |
| 182 |
|
| 183 |
|
| 184 |
def main(): |
| 185 |
argument_spec = dict( |
| 186 |
bucket_name=dict(type="str", required=True), |
| 187 |
id=dict(type="str", required=True), |
| 188 |
filter_prefix=dict(type="str", required=False), |
| 189 |
filter_tags=dict(default={}, type="dict", required=False, aliases=["filter_tag"]), |
| 190 |
state=dict(default="present", type="str", choices=["present", "absent"]), |
| 191 |
) |
| 192 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 193 |
|
| 194 |
state = module.params.get("state") |
| 195 |
|
| 196 |
try: |
| 197 |
client = module.client("s3", retry_decorator=AWSRetry.exponential_backoff(retries=10, delay=3)) |
| 198 |
except (BotoCoreError, ClientError) as e: |
| 199 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 200 |
|
| 201 |
if state == "present": |
| 202 |
create_or_update_metrics_configuration(client, module) |
| 203 |
elif state == "absent": |
| 204 |
delete_metrics_configuration(client, module) |
| 205 |
|
| 206 |
|
| 207 |
if __name__ == "__main__": |
| 208 |
main() |