cloudfront_facts module improvements

* Make more use of AWS Exception information
* Use pythonic dict assignments
* Improve documentation formatting to make option names clearer
  in descriptions
* Meet ansible line length requirements
* Tidy up flake8 messages
pull/21836/head
Will Thames 8 years ago committed by Brian Coca
parent bd4003e3f2
commit ebfc7bac94

@ -32,11 +32,12 @@ author: Willem van Ketwich (@wilvk)
options: options:
distribution_id: distribution_id:
description: description:
- The id of the CloudFront distribution. Used with distribution, distribution_config, invalidation, streaming_distribution, streaming_distribution_config, list_invalidations. - The id of the CloudFront distribution. Used with I(distribution), I(distribution_config),
I(invalidation), I(streaming_distribution), I(streaming_distribution_config), I(list_invalidations).
required: false required: false
invalidation_id: invalidation_id:
description: description:
- The id of the invalidation to get information about. Used with invalidation. - The id of the invalidation to get information about. Used with I(invalidation).
required: false required: false
origin_access_identity_id: origin_access_identity_id:
description: description:
@ -44,55 +45,62 @@ options:
required: false required: false
web_acl_id: web_acl_id:
description: description:
- Used with list_distributions_by_web_acl_id. - Used with I(list_distributions_by_web_acl_id).
required: false required: false
domain_name_alias: domain_name_alias:
description: description:
- Can be used instead of distribution_id - uses the aliased CNAME for the cloudfront distribution to get the distribution id where required. - Can be used instead of I(distribution_id) - uses the aliased CNAME for the cloudfront
distribution to get the distribution id where required.
required: false required: false
all_lists: all_lists:
description: description:
- Get all cloudfront lists that do not require parameters. - Get all cloudfront lists that do not require parameters.
required: false required: false
default: false default: false
origin_access_identity: origin_access_identity:
description: description:
- Get information about an origin access identity. Requires origin_access_identity_id to be specified. - Get information about an origin access identity. Requires I(origin_access_identity_id)
to be specified.
required: false required: false
default: false default: false
origin_access_identity_config: origin_access_identity_config:
description: description:
- Get the configuration information about an origin access identity. Requires origin_access_identity_id to be specified. - Get the configuration information about an origin access identity. Requires
I(origin_access_identity_id) to be specified.
required: false required: false
default: false default: false
distribution: distribution:
description: description:
- Get information about a distribution. Requires distribution_id or domain_name_alias to be specified. - Get information about a distribution. Requires I(distribution_id) or I(domain_name_alias)
to be specified.
required: false required: false
default: false default: false
distribution_config: distribution_config:
description: description:
- Get the configuration information about a distribution. Requires distribution_id or domain_name_alias to be specified. - Get the configuration information about a distribution. Requires I(distribution_id)
or I(domain_name_alias) to be specified.
required: false required: false
default: false default: false
invalidation: invalidation:
description: description:
- Get information about an invalidation. Requires invalidation_id to be specified. - Get information about an invalidation. Requires I(invalidation_id) to be specified.
required: false required: false
default: false default: false
streaming_distribution: streaming_distribution:
description: description:
- Get information about a specified RTMP distribution. Requires distribution_id or domain_name_alias to be specified. - Get information about a specified RTMP distribution. Requires I(distribution_id) or
I(domain_name_alias) to be specified.
required: false required: false
default: false default: false
streaming_distribution_configuration: streaming_distribution_configuration:
description: description:
- Get the configuration information about a specified RTMP distribution. Requires distribution_id or domain_name_alias to be specified. - Get the configuration information about a specified RTMP distribution.
Requires I(distribution_id) or I(domain_name_alias) to be specified.
required: false required: false
default: false default: false
list_origin_access_identities: list_origin_access_identities:
description: description:
- Get a list of cloudfront origin access identities. Requires origin_access_identity_id to be set. - Get a list of cloudfront origin access identities. Requires I(origin_access_identity_id) to be set.
required: false required: false
default: false default: false
list_distributions: list_distributions:
@ -102,12 +110,12 @@ options:
default: false default: false
list_distributions_by_web_acl_id: list_distributions_by_web_acl_id:
description: description:
- Get a list of distributions using web acl id as a filter. Requires web_acl_id to be set. - Get a list of distributions using web acl id as a filter. Requires I(web_acl_id) to be set.
required: false required: false
default: false default: false
list_invalidations: list_invalidations:
description: description:
- Get a list of invalidations. Requires distribution_id or domain_name_alias to be specified. - Get a list of invalidations. Requires I(distribution_id) or I(domain_name_alias) to be specified.
required: false required: false
default: false default: false
list_streaming_distributions: list_streaming_distributions:
@ -117,7 +125,8 @@ options:
default: false default: false
summary: summary:
description: description:
- Returns a summary of all distributions, streaming distributions and origin_access_identities. This is the default behaviour if no option is selected. - Returns a summary of all distributions, streaming distributions and origin_access_identities.
This is the default behaviour if no option is selected.
required: false required: false
default: false default: false
@ -173,32 +182,42 @@ EXAMPLES = '''
RETURN = ''' RETURN = '''
origin_access_identity: origin_access_identity:
description: Describes the origin access identity information. Requires origin_access_identity_id to be set. description: Describes the origin access identity information. Requires I(origin_access_identity_id) to be set.
returned: only if origin_access_identity is true returned: only if I(origin_access_identity) is true
type: dict type: dict
origin_access_identity_configuration: origin_access_identity_configuration:
description: Describes the origin access identity information configuration information. Requires origin_access_identity_id to be set. description: Describes the origin access identity information configuration information. Requires I(origin_access_identity_id) to be set.
returned: only if origin_access_identity_configuration is true returned: only if I(origin_access_identity_configuration) is true
type: dict type: dict
distribution: distribution:
description: Facts about a cloudfront distribution. Requires distribution_id or domain_name_alias to be specified. Requires origin_access_identity_id to be set. description: >
Facts about a cloudfront distribution. Requires I(distribution_id) or I(domain_name_alias)
to be specified. Requires I(origin_access_identity_id) to be set.
returned: only if distribution is true returned: only if distribution is true
type: dict type: dict
distribution_config: distribution_config:
description: Facts about a cloudfront distribution's config. Requires distribution_id or domain_name_alias to be specified. description: >
returned: only if distribution_config is true Facts about a cloudfront distribution's config. Requires I(distribution_id) or I(domain_name_alias)
to be specified.
returned: only if I(distribution_config) is true
type: dict type: dict
invalidation: invalidation:
description: Describes the invalidation information for the distribution. Requires invalidation_id to be specified and either distribution_id or domain_name_alias. description: >
Describes the invalidation information for the distribution. Requires
I(invalidation_id) to be specified and either I(distribution_id) or I(domain_name_alias.)
returned: only if invalidation is true returned: only if invalidation is true
type: dict type: dict
streaming_distribution: streaming_distribution:
description: Describes the streaming information for the distribution. Requires distribution_id or domain_name_alias to be specified. description: >
returned: only if streaming_distribution is true Describes the streaming information for the distribution. Requires
I(distribution_id) or I(domain_name_alias) to be specified.
returned: only if I(streaming_distribution) is true
type: dict type: dict
streaming_distribution_configuration: streaming_distribution_configuration:
description: Describes the streaming configuration information for the distribution. Requires distribution_id or domain_name_alias to be specified. description: >
returned: only if streaming_distribution_configuration is true Describes the streaming configuration information for the distribution.
Requires I(distribution_id) or I(domain_name_alias) to be specified.
returned: only if I(streaming_distribution_configuration) is true
type: dict type: dict
summary: summary:
description: Gives a summary of distributions, streaming distributions and origin access identities. description: Gives a summary of distributions, streaming distributions and origin access identities.
@ -230,77 +249,87 @@ class CloudFrontServiceManager:
resource='cloudfront', region=region, resource='cloudfront', region=region,
endpoint=ec2_url, **aws_connect_kwargs) endpoint=ec2_url, **aws_connect_kwargs)
except botocore.exceptions.NoRegionError: except botocore.exceptions.NoRegionError:
self.module.fail_json(msg="Region must be specified as a parameter, in AWS_DEFAULT_REGION environment variable or in boto configuration file") self.module.fail_json(msg="Region must be specified as a parameter, in AWS_DEFAULT_REGION "
except Exception as e: "environment variable or in boto configuration file")
except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Can't establish connection - " + str(e), self.module.fail_json(msg="Can't establish connection - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def get_distribution(self, distribution_id): def get_distribution(self, distribution_id):
try: try:
func = partial(self.client.get_distribution,Id=distribution_id) func = partial(self.client.get_distribution,Id=distribution_id)
return self.paginated_response(func) return self.paginated_response(func)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error describing distribution - " + str(e), self.module.fail_json(msg="Error describing distribution - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def get_distribution_config(self, distribution_id): def get_distribution_config(self, distribution_id):
try: try:
func = partial(self.client.get_distribution_config,Id=distribution_id) func = partial(self.client.get_distribution_config,Id=distribution_id)
return self.paginated_response(func) return self.paginated_response(func)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error describing distribution configuration - " + str(e), self.module.fail_json(msg="Error describing distribution configuration - " + str(e),
exception=traceback.format_exec()) exception=traceback.format_exec(e),
**camel_dict_to_snake_dict(e.response))
def get_origin_access_identity(self, origin_access_identity_id): def get_origin_access_identity(self, origin_access_identity_id):
try: try:
func = partial(self.client.get_cloud_front_origin_access_identity,Id=origin_access_identity_id) func = partial(self.client.get_cloud_front_origin_access_identity,Id=origin_access_identity_id)
return self.paginated_response(func) return self.paginated_response(func)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error describing origin access identity - " + str(e), self.module.fail_json(msg="Error describing origin access identity - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def get_origin_access_identity_config(self, origin_access_identity_id): def get_origin_access_identity_config(self, origin_access_identity_id):
try: try:
func = partial(self.client.get_cloud_front_origin_access_identity_config,Id=origin_access_identity_id) func = partial(self.client.get_cloud_front_origin_access_identity_config,Id=origin_access_identity_id)
return self.paginated_response(func) return self.paginated_response(func)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error describing origin access identity configuration - " + str(e), self.module.fail_json(msg="Error describing origin access identity configuration - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def get_invalidation(self, distribution_id, invalidation_id): def get_invalidation(self, distribution_id, invalidation_id):
try: try:
func = partial(self.client.get_invalidation,DistributionId=distribution_id,Id=invalidation_id) func = partial(self.client.get_invalidation,DistributionId=distribution_id,Id=invalidation_id)
return self.paginated_response(func) return self.paginated_response(func)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error describing invalidation - " + str(e), self.module.fail_json(msg="Error describing invalidation - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def get_streaming_distribution(self, distribution_id): def get_streaming_distribution(self, distribution_id):
try: try:
func = partial(self.client.get_streaming_distribution,Id=distribution_id) func = partial(self.client.get_streaming_distribution,Id=distribution_id)
return self.paginated_response(func) return self.paginated_response(func)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error describing streaming distribution - " + str(e), self.module.fail_json(msg="Error describing streaming distribution - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def get_streaming_distribution_config(self, distribution_id): def get_streaming_distribution_config(self, distribution_id):
try: try:
func = partial(self.client.get_streaming_distribution_config,Id=distribution_id) func = partial(self.client.get_streaming_distribution_config,Id=distribution_id)
return self.paginated_response(func) return self.paginated_response(func)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error describing streaming distribution - " + str(e), self.module.fail_json(msg="Error describing streaming distribution - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def list_origin_access_identities(self): def list_origin_access_identities(self):
try: try:
func = partial(self.client.list_cloud_front_origin_access_identities) func = partial(self.client.list_cloud_front_origin_access_identities)
origin_access_identity_list = self.paginated_response(func, 'CloudFrontOriginAccessIdentityList') origin_access_identity_list = self.paginated_response(func, 'CloudFrontOriginAccessIdentityList')
if origin_access_identity_list['Quantity'] > 0: if origin_access_identity_list['Quantity'] > 0:
return origin_access_identity_list['Items'] return origin_access_identity_list['Items']
return {} return {}
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error listing cloud front origin access identities - " + str(e), self.module.fail_json(msg="Error listing cloud front origin access identities - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def list_distributions(self, keyed=True): def list_distributions(self, keyed=True):
try: try:
@ -313,9 +342,10 @@ class CloudFrontServiceManager:
if not keyed: if not keyed:
return distribution_list return distribution_list
return self.keyed_list_helper(distribution_list) return self.keyed_list_helper(distribution_list)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error listing distributions - " + str(e), self.module.fail_json(msg="Error listing distributions - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def list_distributions_by_web_acl_id(self, web_acl_id): def list_distributions_by_web_acl_id(self, web_acl_id):
try: try:
@ -326,9 +356,10 @@ class CloudFrontServiceManager:
else: else:
distribution_list = distribution_list['Items'] distribution_list = distribution_list['Items']
return self.keyed_list_helper(distribution_list) return self.keyed_list_helper(distribution_list)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error listing distributions by web acl id - " + str(e), self.module.fail_json(msg="Error listing distributions by web acl id - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def list_invalidations(self, distribution_id): def list_invalidations(self, distribution_id):
try: try:
@ -337,9 +368,10 @@ class CloudFrontServiceManager:
if invalidation_list['Quantity'] > 0: if invalidation_list['Quantity'] > 0:
return invalidation_list['Items'] return invalidation_list['Items']
return {} return {}
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error listing invalidations - " + str(e), self.module.fail_json(msg="Error listing invalidations - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def list_streaming_distributions(self, keyed=True): def list_streaming_distributions(self, keyed=True):
try: try:
@ -352,9 +384,10 @@ class CloudFrontServiceManager:
if not keyed: if not keyed:
return streaming_distribution_list return streaming_distribution_list
return self.keyed_list_helper(streaming_distribution_list) return self.keyed_list_helper(streaming_distribution_list)
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error listing streaming distributions - " + str(e), self.module.fail_json(msg="Error listing streaming distributions - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def summary(self): def summary(self):
summary_dict = {} summary_dict = {}
@ -373,9 +406,10 @@ class CloudFrontServiceManager:
oai_summary = { 'Id': oai_id, 'ETag': oai_full_response['ETag'] } oai_summary = { 'Id': oai_id, 'ETag': oai_full_response['ETag'] }
origin_access_identity_list['origin_access_identities'].append( oai_summary ) origin_access_identity_list['origin_access_identities'].append( oai_summary )
return origin_access_identity_list return origin_access_identity_list
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error generating summary of origin access identities - " + str(e), self.module.fail_json(msg="Error generating summary of origin access identities - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def summary_get_distribution_list(self, streaming=False): def summary_get_distribution_list(self, streaming=False):
try: try:
@ -386,21 +420,22 @@ class CloudFrontServiceManager:
for dist in distributions: for dist in distributions:
temp_distribution = {} temp_distribution = {}
for key_name in key_list: for key_name in key_list:
temp_distribution.update( { key_name: dist[key_name] } ) temp_distribution[key_name] = dist[key_name]
temp_distribution.update( { 'Aliases': [] } ) temp_distribution['Aliases'] = [alias for alias in dist['Aliases'].get('Items', [])]
temp_distribution.update( { 'ETag': self.get_etag_from_distribution_id(dist['Id'], streaming) } ) temp_distribution['ETag'] = self.get_etag_from_distribution_id(dist['Id'], streaming)
if 'Items' in dist['Aliases']:
for alias in dist['Aliases']['Items']:
temp_distribution['Aliases'].append(alias)
if not streaming: if not streaming:
temp_distribution.update( { 'WebACLId': dist['WebACLId'] } ) temp_distribution['WebACLId'] = dist['WebACLId']
invalidation_ids = self.get_list_of_invalidation_ids_from_distribution_id(dist['Id']) invalidation_ids = self.get_list_of_invalidation_ids_from_distribution_id(dist['Id'])
if invalidation_ids: if invalidation_ids:
temp_distribution.update( { 'Invalidations': invalidation_ids } ) temp_distribution['Invalidations'] = invalidation_ids
resource_tags = self.client.list_tags_for_resource(Resource=dist['ARN']) resource_tags = self.client.list_tags_for_resource(Resource=dist['ARN'])
temp_distribution['Tags'] = boto3_tag_list_to_ansible_dict(resource_tags['Tags'].get('Items', [])) temp_distribution['Tags'] = boto3_tag_list_to_ansible_dict(resource_tags['Tags'].get('Items', []))
distribution_list[list_name].append(temp_distribution) distribution_list[list_name].append(temp_distribution)
return distribution_list return distribution_list
except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error generating summary of distributions - " + str(e),
exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
except Exception as e: except Exception as e:
self.module.fail_json(msg="Error generating summary of distributions - " + str(e), self.module.fail_json(msg="Error generating summary of distributions - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc())
@ -420,9 +455,10 @@ class CloudFrontServiceManager:
for invalidation in invalidations: for invalidation in invalidations:
invalidation_ids.append(invalidation['Id']) invalidation_ids.append(invalidation['Id'])
return invalidation_ids return invalidation_ids
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error getting list of invalidation ids - " + str(e), self.module.fail_json(msg="Error getting list of invalidation ids - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def get_distribution_id_from_domain_name(self, domain_name): def get_distribution_id_from_domain_name(self, domain_name):
try: try:
@ -436,9 +472,10 @@ class CloudFrontServiceManager:
distribution_id = dist['Id'] distribution_id = dist['Id']
break break
return distribution_id return distribution_id
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error getting distribution id from domain name - " + str(e), self.module.fail_json(msg="Error getting distribution id from domain name - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def get_aliases_from_distribution_id(self, distribution_id): def get_aliases_from_distribution_id(self, distribution_id):
aliases = [] aliases = []
@ -450,9 +487,10 @@ class CloudFrontServiceManager:
aliases.append(alias) aliases.append(alias)
break break
return aliases return aliases
except Exception as e: except botocore.exceptions.ClientError as e:
self.module.fail_json(msg="Error getting list of aliases from distribution_id - " + str(e), self.module.fail_json(msg="Error getting list of aliases from distribution_id - " + str(e),
exception=traceback.format_exc()) exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
def paginated_response(self, func, result_key=""): def paginated_response(self, func, result_key=""):
''' '''

@ -17,7 +17,6 @@ lib/ansible/modules/cloud/amazon/_ec2_vpc.py
lib/ansible/modules/cloud/amazon/aws_kms.py lib/ansible/modules/cloud/amazon/aws_kms.py
lib/ansible/modules/cloud/amazon/cloudformation.py lib/ansible/modules/cloud/amazon/cloudformation.py
lib/ansible/modules/cloud/amazon/cloudformation_facts.py lib/ansible/modules/cloud/amazon/cloudformation_facts.py
lib/ansible/modules/cloud/amazon/cloudfront_facts.py
lib/ansible/modules/cloud/amazon/cloudtrail.py lib/ansible/modules/cloud/amazon/cloudtrail.py
lib/ansible/modules/cloud/amazon/cloudwatchevent_rule.py lib/ansible/modules/cloud/amazon/cloudwatchevent_rule.py
lib/ansible/modules/cloud/amazon/dynamodb_table.py lib/ansible/modules/cloud/amazon/dynamodb_table.py

Loading…
Cancel
Save