| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 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 |
|
| 10 |
version_added: 1.0.0 |
| 11 |
module: cloudfront_invalidation |
| 12 |
|
| 13 |
short_description: create invalidations for AWS CloudFront distributions |
| 14 |
description: |
| 15 |
- Allows for invalidation of a batch of paths for a CloudFront distribution. |
| 16 |
|
| 17 |
author: |
| 18 |
- Willem van Ketwich (@wilvk) |
| 19 |
|
| 20 |
options: |
| 21 |
distribution_id: |
| 22 |
description: |
| 23 |
- The ID of the CloudFront distribution to invalidate paths for. Can be specified instead of the alias. |
| 24 |
required: false |
| 25 |
type: str |
| 26 |
alias: |
| 27 |
description: |
| 28 |
- The alias of the CloudFront distribution to invalidate paths for. Can be specified instead of distribution_id. |
| 29 |
required: false |
| 30 |
type: str |
| 31 |
caller_reference: |
| 32 |
description: |
| 33 |
- A unique reference identifier for the invalidation paths. |
| 34 |
- Defaults to current datetime stamp. |
| 35 |
required: false |
| 36 |
default: |
| 37 |
type: str |
| 38 |
target_paths: |
| 39 |
description: |
| 40 |
- A list of paths on the distribution to invalidate. Each path should begin with C(/). Wildcards are allowed. eg. C(/foo/bar/*) |
| 41 |
required: true |
| 42 |
type: list |
| 43 |
elements: str |
| 44 |
|
| 45 |
notes: |
| 46 |
- does not support check mode |
| 47 |
|
| 48 |
extends_documentation_fragment: |
| 49 |
- amazon.aws.common.modules |
| 50 |
- amazon.aws.region.modules |
| 51 |
- amazon.aws.boto3 |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
- name: create a batch of invalidations using a distribution_id for a reference |
| 56 |
community.aws.cloudfront_invalidation: |
| 57 |
distribution_id: E15BU8SDCGSG57 |
| 58 |
caller_reference: testing 123 |
| 59 |
target_paths: |
| 60 |
- /testpathone/test1.css |
| 61 |
- /testpathtwo/test2.js |
| 62 |
- /testpaththree/test3.ss |
| 63 |
|
| 64 |
- name: create a batch of invalidations using an alias as a reference and one path using a wildcard match |
| 65 |
community.aws.cloudfront_invalidation: |
| 66 |
alias: alias.test.com |
| 67 |
caller_reference: testing 123 |
| 68 |
target_paths: |
| 69 |
- /testpathone/test4.css |
| 70 |
- /testpathtwo/test5.js |
| 71 |
- /testpaththree/* |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r""" |
| 75 |
invalidation: |
| 76 |
description: The invalidation's information. |
| 77 |
returned: always |
| 78 |
type: complex |
| 79 |
contains: |
| 80 |
create_time: |
| 81 |
description: The date and time the invalidation request was first made. |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
sample: '2018-02-01T15:50:41.159000+00:00' |
| 85 |
id: |
| 86 |
description: The identifier for the invalidation request. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: I2G9MOWJZFV612 |
| 90 |
invalidation_batch: |
| 91 |
description: The current invalidation information for the batch request. |
| 92 |
returned: always |
| 93 |
type: complex |
| 94 |
contains: |
| 95 |
caller_reference: |
| 96 |
description: The value used to uniquely identify an invalidation request. |
| 97 |
returned: always |
| 98 |
type: str |
| 99 |
sample: testing 123 |
| 100 |
paths: |
| 101 |
description: A dict that contains information about the objects that you want to invalidate. |
| 102 |
returned: always |
| 103 |
type: complex |
| 104 |
contains: |
| 105 |
items: |
| 106 |
description: |
| 107 |
- A list of the paths that you want to invalidate. |
| 108 |
- This return value has been deprecated and will be removed in a release after 2026-12-16. |
| 109 |
Use RV(invalidation.invalidation_batch.paths.elements) instead. |
| 110 |
returned: always |
| 111 |
type: list |
| 112 |
sample: |
| 113 |
- /testpathtwo/test2.js |
| 114 |
- /testpathone/test1.css |
| 115 |
- /testpaththree/test3.ss |
| 116 |
elements: |
| 117 |
description: |
| 118 |
- A list of the paths that you want to invalidate. |
| 119 |
returned: always |
| 120 |
type: list |
| 121 |
sample: |
| 122 |
- /testpathtwo/test2.js |
| 123 |
- /testpathone/test1.css |
| 124 |
- /testpaththree/test3.ss |
| 125 |
version_added: 10.1.0 |
| 126 |
quantity: |
| 127 |
description: The number of objects that you want to invalidate. |
| 128 |
returned: always |
| 129 |
type: int |
| 130 |
sample: 3 |
| 131 |
status: |
| 132 |
description: The status of the invalidation request. |
| 133 |
returned: always |
| 134 |
type: str |
| 135 |
sample: Completed |
| 136 |
location: |
| 137 |
description: The fully qualified URI of the distribution and invalidation batch request. |
| 138 |
returned: always |
| 139 |
type: str |
| 140 |
sample: https://cloudfront.amazonaws.com/2017-03-25/distribution/E1ZID6KZJECZY7/invalidation/I2G9MOWJZFV622 |
| 141 |
""" |
| 142 |
|
| 143 |
import datetime |
| 144 |
|
| 145 |
try: |
| 146 |
import botocore |
| 147 |
except ImportError: |
| 148 |
pass # caught by imported AnsibleAWSModule |
| 149 |
|
| 150 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 151 |
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict |
| 152 |
|
| 153 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_message |
| 154 |
from ansible_collections.amazon.aws.plugins.module_utils.cloudfront_facts import CloudFrontFactsServiceManager |
| 155 |
|
| 156 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 157 |
|
| 158 |
|
| 159 |
class CloudFrontInvalidationServiceManager(object): |
| 160 |
""" |
| 161 |
Handles CloudFront service calls to AWS for invalidations |
| 162 |
""" |
| 163 |
|
| 164 |
def __init__(self, module, cloudfront_facts_mgr): |
| 165 |
self.module = module |
| 166 |
self.client = module.client("cloudfront") |
| 167 |
self.__cloudfront_facts_mgr = cloudfront_facts_mgr |
| 168 |
|
| 169 |
def create_invalidation(self, distribution_id, invalidation_batch): |
| 170 |
current_invalidation_response = self.get_invalidation(distribution_id, invalidation_batch["CallerReference"]) |
| 171 |
try: |
| 172 |
response = self.client.create_invalidation( |
| 173 |
DistributionId=distribution_id, InvalidationBatch=invalidation_batch |
| 174 |
) |
| 175 |
response.pop("ResponseMetadata", None) |
| 176 |
if current_invalidation_response: |
| 177 |
return response, False |
| 178 |
else: |
| 179 |
return response, True |
| 180 |
except is_boto3_error_message( |
| 181 |
"Your request contains a caller reference that was used for a previous invalidation " |
| 182 |
"batch for the same distribution." |
| 183 |
): |
| 184 |
self.module.warn( |
| 185 |
"InvalidationBatch target paths are not modifiable. " |
| 186 |
"To make a new invalidation please update caller_reference." |
| 187 |
) |
| 188 |
return current_invalidation_response, False |
| 189 |
except ( |
| 190 |
botocore.exceptions.ClientError, |
| 191 |
botocore.exceptions.BotoCoreError, |
| 192 |
) as e: # pylint: disable=duplicate-except |
| 193 |
self.module.fail_json_aws(e, msg="Error creating CloudFront invalidations.") |
| 194 |
|
| 195 |
def get_invalidation(self, distribution_id, caller_reference): |
| 196 |
# find all invalidations for the distribution |
| 197 |
invalidations = self.__cloudfront_facts_mgr.list_invalidations(distribution_id=distribution_id) |
| 198 |
|
| 199 |
# check if there is an invalidation with the same caller reference |
| 200 |
for invalidation in invalidations: |
| 201 |
invalidation_info = self.__cloudfront_facts_mgr.get_invalidation( |
| 202 |
distribution_id=distribution_id, id=invalidation["Id"] |
| 203 |
) |
| 204 |
if invalidation_info.get("InvalidationBatch", {}).get("CallerReference") == caller_reference: |
| 205 |
invalidation_info.pop("ResponseMetadata", None) |
| 206 |
return invalidation_info |
| 207 |
return {} |
| 208 |
|
| 209 |
|
| 210 |
class CloudFrontInvalidationValidationManager(object): |
| 211 |
""" |
| 212 |
Manages CloudFront validations for invalidation batches |
| 213 |
""" |
| 214 |
|
| 215 |
def __init__(self, module, cloudfront_facts_mgr): |
| 216 |
self.module = module |
| 217 |
self.__cloudfront_facts_mgr = cloudfront_facts_mgr |
| 218 |
|
| 219 |
def validate_distribution_id(self, distribution_id, alias): |
| 220 |
try: |
| 221 |
if distribution_id is None and alias is None: |
| 222 |
self.module.fail_json(msg="distribution_id or alias must be specified") |
| 223 |
if distribution_id is None: |
| 224 |
distribution_id = self.__cloudfront_facts_mgr.get_distribution_id_from_domain_name(alias) |
| 225 |
return distribution_id |
| 226 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 227 |
self.module.fail_json_aws(e, msg="Error validating parameters.") |
| 228 |
|
| 229 |
def create_aws_list(self, invalidation_batch): |
| 230 |
aws_list = {} |
| 231 |
aws_list["Quantity"] = len(invalidation_batch) |
| 232 |
aws_list["Items"] = invalidation_batch |
| 233 |
return aws_list |
| 234 |
|
| 235 |
def validate_invalidation_batch(self, invalidation_batch, caller_reference): |
| 236 |
try: |
| 237 |
if caller_reference is not None: |
| 238 |
valid_caller_reference = caller_reference |
| 239 |
else: |
| 240 |
valid_caller_reference = datetime.datetime.now().isoformat() |
| 241 |
valid_invalidation_batch = { |
| 242 |
"paths": self.create_aws_list(invalidation_batch), |
| 243 |
"caller_reference": valid_caller_reference, |
| 244 |
} |
| 245 |
return valid_invalidation_batch |
| 246 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 247 |
self.module.fail_json_aws(e, msg="Error validating invalidation batch.") |
| 248 |
|
| 249 |
|
| 250 |
def main(): |
| 251 |
argument_spec = dict( |
| 252 |
caller_reference=dict(), |
| 253 |
distribution_id=dict(), |
| 254 |
alias=dict(), |
| 255 |
target_paths=dict(required=True, type="list", elements="str"), |
| 256 |
) |
| 257 |
|
| 258 |
module = AnsibleAWSModule( |
| 259 |
argument_spec=argument_spec, supports_check_mode=False, mutually_exclusive=[["distribution_id", "alias"]] |
| 260 |
) |
| 261 |
|
| 262 |
cloudfront_facts_mgr = CloudFrontFactsServiceManager(module) |
| 263 |
validation_mgr = CloudFrontInvalidationValidationManager(module, cloudfront_facts_mgr) |
| 264 |
service_mgr = CloudFrontInvalidationServiceManager(module, cloudfront_facts_mgr) |
| 265 |
|
| 266 |
caller_reference = module.params.get("caller_reference") |
| 267 |
distribution_id = module.params.get("distribution_id") |
| 268 |
alias = module.params.get("alias") |
| 269 |
target_paths = module.params.get("target_paths") |
| 270 |
|
| 271 |
result = {} |
| 272 |
|
| 273 |
distribution_id = validation_mgr.validate_distribution_id(distribution_id, alias) |
| 274 |
valid_target_paths = validation_mgr.validate_invalidation_batch(target_paths, caller_reference) |
| 275 |
valid_pascal_target_paths = snake_dict_to_camel_dict(valid_target_paths, True) |
| 276 |
result, changed = service_mgr.create_invalidation(distribution_id, valid_pascal_target_paths) |
| 277 |
|
| 278 |
# Duplicate 'Invalidation.InvalidationBatch.Paths.Items' to 'Invalidation.InvalidationBatch.Paths.Elements' |
| 279 |
result["Invalidation"]["InvalidationBatch"]["Paths"]["Elements"] = result["Invalidation"]["InvalidationBatch"][ |
| 280 |
"Paths" |
| 281 |
]["Items"] |
| 282 |
|
| 283 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(result)) |
| 284 |
|
| 285 |
|
| 286 |
if __name__ == "__main__": |
| 287 |
main() |