diff --git a/lib/ansible/modules/cloud/amazon/ec2_vpc_vgw.py b/lib/ansible/modules/cloud/amazon/ec2_vpc_vgw.py index 52a6f5f9c6..121652b27a 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_vpc_vgw.py +++ b/lib/ansible/modules/cloud/amazon/ec2_vpc_vgw.py @@ -119,15 +119,21 @@ result: type: dictionary ''' +import time +import traceback + try: - import json - import time import botocore import boto3 HAS_BOTO3 = True except ImportError: HAS_BOTO3 = False +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.ec2 import HAS_BOTO3, boto3_conn, ec2_argument_spec, get_aws_connection_info +from ansible.module_utils._text import to_native + + def get_vgw_info(vgws): if not isinstance(vgws, list): return @@ -162,9 +168,8 @@ def wait_for_status(client, module, vpn_gateway_id, status): break else: time.sleep(polling_increment_secs) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) result = response return status_achieved, result @@ -176,9 +181,8 @@ def attach_vgw(client, module, vpn_gateway_id): try: response = client.attach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=params['VpcId']) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) status_achieved, vgw = wait_for_status(client, module, [vpn_gateway_id], 'attached') if not status_achieved: @@ -195,15 +199,13 @@ def detach_vgw(client, module, vpn_gateway_id, vpc_id=None): if vpc_id: try: response = client.detach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=vpc_id) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) else: try: response = client.detach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=params['VpcId']) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) status_achieved, vgw = wait_for_status(client, module, [vpn_gateway_id], 'detached') if not status_achieved: @@ -219,9 +221,8 @@ def create_vgw(client, module): try: response = client.create_vpn_gateway(Type=params['Type']) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) result = response return result @@ -231,9 +232,8 @@ def delete_vgw(client, module, vpn_gateway_id): try: response = client.delete_vpn_gateway(VpnGatewayId=vpn_gateway_id) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) #return the deleted VpnGatewayId as this is not included in the above response result = vpn_gateway_id @@ -245,9 +245,8 @@ def create_tags(client, module, vpn_gateway_id): try: response = client.create_tags(Resources=[vpn_gateway_id],Tags=load_tags(module)) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) result = response return result @@ -259,15 +258,13 @@ def delete_tags(client, module, vpn_gateway_id, tags_to_delete=None): if tags_to_delete: try: response = client.delete_tags(Resources=[vpn_gateway_id], Tags=tags_to_delete) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) else: try: response = client.delete_tags(Resources=[vpn_gateway_id]) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) result = response return result @@ -292,9 +289,8 @@ def find_tags(client, module, resource_id=None): response = client.describe_tags(Filters=[ {'Name': 'resource-id', 'Values': [resource_id]} ]) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) result = response return result @@ -340,9 +336,8 @@ def find_vpc(client, module): if params['vpc_id']: try: response = client.describe_vpcs(VpcIds=[params['vpc_id']]) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) result = response return result @@ -360,17 +355,15 @@ def find_vgw(client, module, vpn_gateway_id=None): {'Name': 'type', 'Values': [params['Type']]}, {'Name': 'tag:Name', 'Values': [params['Name']]} ]) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) else: if vpn_gateway_id: try: response = client.describe_vpn_gateways(VpnGatewayIds=vpn_gateway_id) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) else: try: @@ -378,9 +371,8 @@ def find_vgw(client, module, vpn_gateway_id=None): {'Name': 'type', 'Values': [params['Type']]}, {'Name': 'tag:Name', 'Values': [params['Name']]} ]) - except botocore.exceptions.ClientError: - e = get_exception() - module.fail_json(msg=str(e)) + except botocore.exceptions.ClientError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) result = response['VpnGateways'] return result @@ -583,9 +575,8 @@ def main(): try: region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) client = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_kwargs) - except botocore.exceptions.NoCredentialsError: - e = get_exception() - module.fail_json(msg="Can't authorize connection - "+str(e)) + except botocore.exceptions.NoCredentialsError as e: + module.fail_json(msg="Can't authorize connection - %s" % to_native(e), exception=traceback.format_exc()) if state == 'present': (changed, results) = ensure_vgw_present(client, module) @@ -594,10 +585,5 @@ def main(): module.exit_json(changed=changed, vgw=results) -# import module snippets -from ansible.module_utils.basic import * -from ansible.module_utils.ec2 import * - if __name__ == '__main__': main() - diff --git a/lib/ansible/modules/cloud/amazon/sns.py b/lib/ansible/modules/cloud/amazon/sns.py index ab43511db1..65c8197e77 100644 --- a/lib/ansible/modules/cloud/amazon/sns.py +++ b/lib/ansible/modules/cloud/amazon/sns.py @@ -129,14 +129,8 @@ EXAMPLES = """ delegate_to: localhost """ -try: - import json -except ImportError: - import simplejson as json - -from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ec2 import ec2_argument_spec, connect_to_aws, get_aws_connection_info -from ansible.module_utils.pycompat24 import get_exception +import json +import traceback try: import boto @@ -146,6 +140,10 @@ try: except ImportError: HAS_BOTO = False +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.ec2 import ec2_argument_spec, connect_to_aws, get_aws_connection_info +from ansible.module_utils._text import to_native + def arn_topic_lookup(connection, short_topic): response = connection.get_all_topics() @@ -196,9 +194,8 @@ def main(): module.fail_json(msg="region must be specified") try: connection = connect_to_aws(boto.sns, region, **aws_connect_params) - except boto.exception.NoAuthHandlerFound: - e = get_exception() - module.fail_json(msg=str(e)) + except boto.exception.NoAuthHandlerFound as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) if not message_structure == 'string' and message_attributes: module.fail_json(msg="when specifying message_attributes, the message_structure must be set to 'string'; otherwise the attributes will not be sent.") @@ -237,11 +234,11 @@ def main(): connection.publish(topic=arn_topic, subject=subject, message_structure=message_structure, message=json_msg, message_attributes=message_attributes) - except boto.exception.BotoServerError: - e = get_exception() - module.fail_json(msg=str(e)) + except boto.exception.BotoServerError as e: + module.fail_json(msg=to_native(e), exception=traceback.format_exc()) module.exit_json(msg="OK") + if __name__ == '__main__': main()