Skip to content

Commit

Permalink
Bulk migration to fail_json_aws (#361)
Browse files Browse the repository at this point in the history
* Split imports and sort
* Move camel_dict_to_snake_dict imports to ansible.module_utils.common.dict_transformations
* Cleanup unused imports
* Bulk migration to fail_json_aws
* Changelog
  • Loading branch information
tremble authored Jan 27, 2021
1 parent 1715d79 commit 6c88315
Show file tree
Hide file tree
Showing 44 changed files with 362 additions and 663 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/361-fail_json_aws.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- various community.aws modules - migrate exception error message handling from fail_json to fail_json_aws (https://github.com/ansible-collections/community.aws/pull/361).
5 changes: 3 additions & 2 deletions plugins/modules/aws_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,17 @@
'''

import json
import traceback

try:
import botocore
except ImportError:
pass # Handled by AnsibleAWSModule

import traceback
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict


def main():
Expand Down
36 changes: 12 additions & 24 deletions plugins/modules/aws_codepipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,18 @@
'''

import copy
import traceback

from ansible.module_utils._text import to_native
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule, is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict, compare_policies


try:
import botocore
except ImportError:
pass # caught by AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import compare_policies


def create_pipeline(client, name, role_arn, artifact_store, stages, version, module):
pipeline_dict = {'name': name, 'roleArn': role_arn, 'artifactStore': artifact_store, 'stages': stages}
Expand All @@ -214,36 +214,24 @@ def create_pipeline(client, name, role_arn, artifact_store, stages, version, mod
try:
resp = client.create_pipeline(pipeline=pipeline_dict)
return resp
except botocore.exceptions.ClientError as e:
module.fail_json(msg="Unable create pipeline {0}: {1}".format(name, to_native(e)),
exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response))
except botocore.exceptions.BotoCoreError as e:
module.fail_json(msg="Unable to create pipeline {0}: {1}".format(name, to_native(e)),
exception=traceback.format_exc())
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Unable create pipeline {0}".format(pipeline_dict['name']))


def update_pipeline(client, pipeline_dict, module):
try:
resp = client.update_pipeline(pipeline=pipeline_dict)
return resp
except botocore.exceptions.ClientError as e:
module.fail_json(msg="Unable update pipeline {0}: {1}".format(pipeline_dict['name'], to_native(e)),
exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response))
except botocore.exceptions.BotoCoreError as e:
module.fail_json(msg="Unable to update pipeline {0}: {1}".format(pipeline_dict['name'], to_native(e)),
exception=traceback.format_exc())
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Unable update pipeline {0}".format(pipeline_dict['name']))


def delete_pipeline(client, name, module):
try:
resp = client.delete_pipeline(name=name)
return resp
except botocore.exceptions.ClientError as e:
module.fail_json(msg="Unable delete pipeline {0}: {1}".format(name, to_native(e)),
exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response))
except botocore.exceptions.BotoCoreError as e:
module.fail_json(msg="Unable to delete pipeline {0}: {1}".format(name, to_native(e)),
exception=traceback.format_exc())
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Unable delete pipeline {0}".format(name))


def describe_pipeline(client, name, version, module):
Expand Down
9 changes: 6 additions & 3 deletions plugins/modules/aws_direct_connect_confirm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@
'''

import traceback
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import DirectConnectError
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import (camel_dict_to_snake_dict, AWSRetry)

try:
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass # handled by imported AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import DirectConnectError
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry

retry_params = {"tries": 10, "delay": 5, "backoff": 1.2, "catch_extra_error_codes": ["DirectConnectClientException"]}


Expand Down
17 changes: 9 additions & 8 deletions plugins/modules/aws_direct_connect_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,21 @@
"""

import traceback
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import (camel_dict_to_snake_dict, AWSRetry)
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import (
DirectConnectError,
delete_connection,
associate_connection_and_lag,
disassociate_connection_and_lag,
)

try:
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass # handled by imported AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import DirectConnectError
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import associate_connection_and_lag
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import delete_connection
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import disassociate_connection_and_lag
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry

retry_params = {"tries": 10, "delay": 5, "backoff": 1.2, "catch_extra_error_codes": ["DirectConnectClientException"]}


Expand Down
20 changes: 9 additions & 11 deletions plugins/modules/aws_direct_connect_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,23 @@
'''

import time
import traceback

try:
import botocore
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils._text import to_native
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict


def dx_gateway_info(client, gateway_id, module):
try:
resp = client.describe_direct_connect_gateways(
directConnectGatewayId=gateway_id)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json_aws(e, msg="Failed to fetch gateway information.")
if resp['directConnectGateways']:
return resp['directConnectGateways'][0]

Expand All @@ -142,7 +140,7 @@ def wait_for_status(client, module, gateway_id, virtual_gateway_id, status):
status_achieved = True
break
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json_aws(e, msg="Failed while waiting for gateway association.")

result = response
return status_achieved, result
Expand All @@ -156,7 +154,7 @@ def associate_direct_connect_gateway(client, module, gateway_id):
directConnectGatewayId=gateway_id,
virtualGatewayId=params['virtual_gateway_id'])
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json_aws(e, 'Failed to associate gateway')

status_achieved, dxgw = wait_for_status(client, module, gateway_id, params['virtual_gateway_id'], 'associating')
if not status_achieved:
Expand All @@ -172,7 +170,7 @@ def delete_association(client, module, gateway_id, virtual_gateway_id):
directConnectGatewayId=gateway_id,
virtualGatewayId=virtual_gateway_id)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json_aws(e, msg="Failed to delete gateway association.")

status_achieved, dxgw = wait_for_status(client, module, gateway_id, virtual_gateway_id, 'disassociating')
if not status_achieved:
Expand All @@ -191,7 +189,7 @@ def create_dx_gateway(client, module):
directConnectGatewayName=params['name'],
amazonSideAsn=int(params['amazon_asn']))
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json_aws(e, msg="Failed to create direct connect gateway.")

result = response
return result
Expand All @@ -206,7 +204,7 @@ def find_dx_gateway(client, module, gateway_id=None):
try:
resp = client.describe_direct_connect_gateways(**params)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json_aws(e, msg="Failed to describe gateways")
gateways.extend(resp['directConnectGateways'])
if 'nextToken' in resp:
params['nextToken'] = resp['nextToken']
Expand All @@ -233,7 +231,7 @@ def check_dxgw_association(client, module, gateway_id, virtual_gateway_id=None):
virtualGatewayId=virtual_gateway_id,
)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json_aws(e, msg="Failed to check gateway association")
return resp


Expand Down Expand Up @@ -330,7 +328,7 @@ def ensure_absent(client, module):
directConnectGatewayId=dx_gateway_id
)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json_aws(e, msg="Failed to delete gateway")
result = resp['directConnectGateway']
return changed

Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/aws_direct_connect_link_aggregation_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import DirectConnectError
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import delete_connection
Expand Down
10 changes: 7 additions & 3 deletions plugins/modules/aws_direct_connect_virtual_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,20 @@
'''

import traceback
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import DirectConnectError, delete_virtual_interface
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry, camel_dict_to_snake_dict

try:
from botocore.exceptions import ClientError, BotoCoreError
except ImportError:
# handled by AnsibleAWSModule
pass

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import DirectConnectError
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import delete_virtual_interface
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry


def try_except_ClientError(failure_msg):
'''
Expand Down
37 changes: 12 additions & 25 deletions plugins/modules/aws_kms_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,16 @@
'''


import traceback

try:
import botocore
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict

# Caching lookup for aliases
Expand Down Expand Up @@ -309,9 +308,7 @@ def get_kms_tags(connection, module, key_id):
tags.extend(tag_response['Tags'])
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] != 'AccessDeniedException':
module.fail_json(msg="Failed to obtain key tags",
exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
module.fail_json_aws(e, msg="Failed to obtain key tags")
else:
tag_response = {}
if tag_response.get('NextMarker'):
Expand All @@ -328,9 +325,7 @@ def get_kms_policies(connection, module, key_id):
policy in policies]
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] != 'AccessDeniedException':
module.fail_json(msg="Failed to obtain key policies",
exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
module.fail_json_aws(e, msg="Failed to obtain key policies")
else:
return []

Expand Down Expand Up @@ -360,18 +355,14 @@ def get_key_details(connection, module, key_id, tokens=None):
tokens = []
try:
result = get_kms_metadata_with_backoff(connection, key_id)['KeyMetadata']
except botocore.exceptions.ClientError as e:
module.fail_json(msg="Failed to obtain key metadata",
exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to obtain key metadata")
result['KeyArn'] = result.pop('Arn')

try:
aliases = get_kms_aliases_lookup(connection)
except botocore.exceptions.ClientError as e:
module.fail_json(msg="Failed to obtain aliases",
exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to obtain aliases")
result['aliases'] = aliases.get(result['KeyId'], [])

if result['Origin'] == 'AWS_KMS':
Expand All @@ -384,10 +375,8 @@ def get_key_details(connection, module, key_id, tokens=None):

try:
result['grants'] = get_kms_grants_with_backoff(connection, key_id, tokens=tokens)['Grants']
except botocore.exceptions.ClientError as e:
module.fail_json(msg="Failed to obtain key grants",
exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to obtain key grants")
tags = get_kms_tags(connection, module, key_id)

result = camel_dict_to_snake_dict(result)
Expand All @@ -399,10 +388,8 @@ def get_key_details(connection, module, key_id, tokens=None):
def get_kms_info(connection, module):
try:
keys = get_kms_keys_with_backoff(connection)['Keys']
except botocore.exceptions.ClientError as e:
module.fail_json(msg="Failed to obtain keys",
exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to obtain keys")

return [get_key_details(connection, module, key['KeyId']) for key in keys]

Expand Down
9 changes: 3 additions & 6 deletions plugins/modules/aws_s3_bucket_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,14 @@
type: list
'''

import traceback

try:
import botocore
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils._text import to_native
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict


def get_bucket_list(module, connection):
Expand All @@ -71,8 +68,8 @@ def get_bucket_list(module, connection):
"""
try:
buckets = camel_dict_to_snake_dict(connection.list_buckets())['buckets']
except botocore.exceptions.ClientError as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to list buckets")

return buckets

Expand Down
Loading

0 comments on commit 6c88315

Please sign in to comment.