From a9c55535bb787c73f93254440143c97df2d42416 Mon Sep 17 00:00:00 2001 From: Francesc Navarro Date: Wed, 23 Feb 2022 21:35:13 +0100 Subject: [PATCH] Awsretry/cloudfront distribution (#297) Awsretry/cloudfront distribution SUMMARY Adding AWSRetry.exponential_backoff when updating a cloudfront distribution. Fixes #296 ISSUE TYPE Bugfix Pull Request COMPONENT NAME cloudfront_distribution Reviewed-by: matej Reviewed-by: Mark Chappell Reviewed-by: Francesc Navarro Reviewed-by: Alina Buzachis Reviewed-by: Markus Bergholz --- .../297-aws-retry-cloudfront-distribution.yml | 2 ++ plugins/modules/cloudfront_distribution.py | 18 +++++++++--------- .../cloudfront_distribution/tasks/main.yml | 11 +++++++++++ 3 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/297-aws-retry-cloudfront-distribution.yml diff --git a/changelogs/fragments/297-aws-retry-cloudfront-distribution.yml b/changelogs/fragments/297-aws-retry-cloudfront-distribution.yml new file mode 100644 index 00000000000..c85e22c2607 --- /dev/null +++ b/changelogs/fragments/297-aws-retry-cloudfront-distribution.yml @@ -0,0 +1,2 @@ +minor_changes: +- Added suport for retries (AWSRetry.jittered_backoff) for cloudfront_distribution (https://github.com/ansible-collections/community.aws/issues/296) diff --git a/plugins/modules/cloudfront_distribution.py b/plugins/modules/cloudfront_distribution.py index 946b93e2041..486da18b461 100644 --- a/plugins/modules/cloudfront_distribution.py +++ b/plugins/modules/cloudfront_distribution.py @@ -1378,7 +1378,7 @@ from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible_collections.amazon.aws.plugins.module_utils.cloudfront_facts import CloudFrontFactsServiceManager from ansible.module_utils.common.dict_transformations import recursive_diff -from ansible_collections.amazon.aws.plugins.module_utils.ec2 import compare_aws_tags, ansible_dict_to_boto3_tag_list, boto3_tag_list_to_ansible_dict +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry, compare_aws_tags, ansible_dict_to_boto3_tag_list, boto3_tag_list_to_ansible_dict from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict, snake_dict_to_camel_dict import datetime @@ -1433,7 +1433,7 @@ def ansible_list_to_cloudfront_list(list_items=None, include_quantity=True): def create_distribution(client, module, config, tags): try: if not tags: - return client.create_distribution(DistributionConfig=config)['Distribution'] + return client.create_distribution(aws_retry=True, DistributionConfig=config)['Distribution'] else: distribution_config_with_tags = { 'DistributionConfig': config, @@ -1441,42 +1441,42 @@ def create_distribution(client, module, config, tags): 'Items': tags } } - return client.create_distribution_with_tags(DistributionConfigWithTags=distribution_config_with_tags)['Distribution'] + return client.create_distribution_with_tags(aws_retry=True, DistributionConfigWithTags=distribution_config_with_tags)['Distribution'] except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Error creating distribution") def delete_distribution(client, module, distribution): try: - return client.delete_distribution(Id=distribution['Distribution']['Id'], IfMatch=distribution['ETag']) + return client.delete_distribution(aws_retry=True, Id=distribution['Distribution']['Id'], IfMatch=distribution['ETag']) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Error deleting distribution %s" % to_native(distribution['Distribution'])) def update_distribution(client, module, config, distribution_id, e_tag): try: - return client.update_distribution(DistributionConfig=config, Id=distribution_id, IfMatch=e_tag)['Distribution'] + return client.update_distribution(aws_retry=True, DistributionConfig=config, Id=distribution_id, IfMatch=e_tag)['Distribution'] except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Error updating distribution to %s" % to_native(config)) def tag_resource(client, module, arn, tags): try: - return client.tag_resource(Resource=arn, Tags=dict(Items=tags)) + return client.tag_resource(aws_retry=True, Resource=arn, Tags=dict(Items=tags)) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Error tagging resource") def untag_resource(client, module, arn, tag_keys): try: - return client.untag_resource(Resource=arn, TagKeys=dict(Items=tag_keys)) + return client.untag_resource(aws_retry=True, Resource=arn, TagKeys=dict(Items=tag_keys)) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Error untagging resource") def list_tags_for_resource(client, module, arn): try: - response = client.list_tags_for_resource(Resource=arn) + response = client.list_tags_for_resource(aws_retry=True, Resource=arn) return boto3_tag_list_to_ansible_dict(response.get('Tags').get('Items')) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Error listing tags for resource") @@ -2152,7 +2152,7 @@ def main(): ] ) - client = module.client('cloudfront') + client = module.client('cloudfront', retry_decorator=AWSRetry.jittered_backoff()) validation_mgr = CloudFrontValidationManager(module) diff --git a/tests/integration/targets/cloudfront_distribution/tasks/main.yml b/tests/integration/targets/cloudfront_distribution/tasks/main.yml index dff726c4791..a6ac0571a2d 100644 --- a/tests/integration/targets/cloudfront_distribution/tasks/main.yml +++ b/tests/integration/targets/cloudfront_distribution/tasks/main.yml @@ -240,6 +240,17 @@ state: present register: cf_update_default_root_object + - name: update default_root_object of existing distribution with retries + cloudfront_distribution: + distribution_id: "{{ distribution_id }}" + origins: + - domain_name: "{{ resource_prefix }}2.example.com" + default_root_object: index.php + state: present + retries: 3 + delay: 3 + register: cf_update_default_root_object + - name: ensure origin was updated assert: that: