1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Remove get_exception usage from amazon modules

This commit is contained in:
Toshio Kuratomi 2017-07-29 03:24:30 -07:00
parent 8de6cff2b1
commit 9a55487dff
2 changed files with 49 additions and 66 deletions

View file

@ -119,15 +119,21 @@ result:
type: dictionary type: dictionary
''' '''
import time
import traceback
try: try:
import json
import time
import botocore import botocore
import boto3 import boto3
HAS_BOTO3 = True HAS_BOTO3 = True
except ImportError: except ImportError:
HAS_BOTO3 = False 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): def get_vgw_info(vgws):
if not isinstance(vgws, list): if not isinstance(vgws, list):
return return
@ -162,9 +168,8 @@ def wait_for_status(client, module, vpn_gateway_id, status):
break break
else: else:
time.sleep(polling_increment_secs) time.sleep(polling_increment_secs)
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
result = response result = response
return status_achieved, result return status_achieved, result
@ -176,9 +181,8 @@ def attach_vgw(client, module, vpn_gateway_id):
try: try:
response = client.attach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=params['VpcId']) response = client.attach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=params['VpcId'])
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
status_achieved, vgw = wait_for_status(client, module, [vpn_gateway_id], 'attached') status_achieved, vgw = wait_for_status(client, module, [vpn_gateway_id], 'attached')
if not status_achieved: if not status_achieved:
@ -195,15 +199,13 @@ def detach_vgw(client, module, vpn_gateway_id, vpc_id=None):
if vpc_id: if vpc_id:
try: try:
response = client.detach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=vpc_id) response = client.detach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=vpc_id)
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
else: else:
try: try:
response = client.detach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=params['VpcId']) response = client.detach_vpn_gateway(VpnGatewayId=vpn_gateway_id, VpcId=params['VpcId'])
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
status_achieved, vgw = wait_for_status(client, module, [vpn_gateway_id], 'detached') status_achieved, vgw = wait_for_status(client, module, [vpn_gateway_id], 'detached')
if not status_achieved: if not status_achieved:
@ -219,9 +221,8 @@ def create_vgw(client, module):
try: try:
response = client.create_vpn_gateway(Type=params['Type']) response = client.create_vpn_gateway(Type=params['Type'])
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
result = response result = response
return result return result
@ -231,9 +232,8 @@ def delete_vgw(client, module, vpn_gateway_id):
try: try:
response = client.delete_vpn_gateway(VpnGatewayId=vpn_gateway_id) response = client.delete_vpn_gateway(VpnGatewayId=vpn_gateway_id)
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
#return the deleted VpnGatewayId as this is not included in the above response #return the deleted VpnGatewayId as this is not included in the above response
result = vpn_gateway_id result = vpn_gateway_id
@ -245,9 +245,8 @@ def create_tags(client, module, vpn_gateway_id):
try: try:
response = client.create_tags(Resources=[vpn_gateway_id],Tags=load_tags(module)) response = client.create_tags(Resources=[vpn_gateway_id],Tags=load_tags(module))
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
result = response result = response
return result return result
@ -259,15 +258,13 @@ def delete_tags(client, module, vpn_gateway_id, tags_to_delete=None):
if tags_to_delete: if tags_to_delete:
try: try:
response = client.delete_tags(Resources=[vpn_gateway_id], Tags=tags_to_delete) response = client.delete_tags(Resources=[vpn_gateway_id], Tags=tags_to_delete)
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
else: else:
try: try:
response = client.delete_tags(Resources=[vpn_gateway_id]) response = client.delete_tags(Resources=[vpn_gateway_id])
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
result = response result = response
return result return result
@ -292,9 +289,8 @@ def find_tags(client, module, resource_id=None):
response = client.describe_tags(Filters=[ response = client.describe_tags(Filters=[
{'Name': 'resource-id', 'Values': [resource_id]} {'Name': 'resource-id', 'Values': [resource_id]}
]) ])
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
result = response result = response
return result return result
@ -340,9 +336,8 @@ def find_vpc(client, module):
if params['vpc_id']: if params['vpc_id']:
try: try:
response = client.describe_vpcs(VpcIds=[params['vpc_id']]) response = client.describe_vpcs(VpcIds=[params['vpc_id']])
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
result = response result = response
return result return result
@ -360,17 +355,15 @@ def find_vgw(client, module, vpn_gateway_id=None):
{'Name': 'type', 'Values': [params['Type']]}, {'Name': 'type', 'Values': [params['Type']]},
{'Name': 'tag:Name', 'Values': [params['Name']]} {'Name': 'tag:Name', 'Values': [params['Name']]}
]) ])
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
else: else:
if vpn_gateway_id: if vpn_gateway_id:
try: try:
response = client.describe_vpn_gateways(VpnGatewayIds=vpn_gateway_id) response = client.describe_vpn_gateways(VpnGatewayIds=vpn_gateway_id)
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
else: else:
try: try:
@ -378,9 +371,8 @@ def find_vgw(client, module, vpn_gateway_id=None):
{'Name': 'type', 'Values': [params['Type']]}, {'Name': 'type', 'Values': [params['Type']]},
{'Name': 'tag:Name', 'Values': [params['Name']]} {'Name': 'tag:Name', 'Values': [params['Name']]}
]) ])
except botocore.exceptions.ClientError: except botocore.exceptions.ClientError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
result = response['VpnGateways'] result = response['VpnGateways']
return result return result
@ -583,9 +575,8 @@ def main():
try: try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) 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) client = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_kwargs)
except botocore.exceptions.NoCredentialsError: except botocore.exceptions.NoCredentialsError as e:
e = get_exception() module.fail_json(msg="Can't authorize connection - %s" % to_native(e), exception=traceback.format_exc())
module.fail_json(msg="Can't authorize connection - "+str(e))
if state == 'present': if state == 'present':
(changed, results) = ensure_vgw_present(client, module) (changed, results) = ensure_vgw_present(client, module)
@ -594,10 +585,5 @@ def main():
module.exit_json(changed=changed, vgw=results) 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__': if __name__ == '__main__':
main() main()

View file

@ -129,14 +129,8 @@ EXAMPLES = """
delegate_to: localhost delegate_to: localhost
""" """
try: import json
import json import traceback
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
try: try:
import boto import boto
@ -146,6 +140,10 @@ try:
except ImportError: except ImportError:
HAS_BOTO = False 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): def arn_topic_lookup(connection, short_topic):
response = connection.get_all_topics() response = connection.get_all_topics()
@ -196,9 +194,8 @@ def main():
module.fail_json(msg="region must be specified") module.fail_json(msg="region must be specified")
try: try:
connection = connect_to_aws(boto.sns, region, **aws_connect_params) connection = connect_to_aws(boto.sns, region, **aws_connect_params)
except boto.exception.NoAuthHandlerFound: except boto.exception.NoAuthHandlerFound as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
if not message_structure == 'string' and message_attributes: 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.") 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, connection.publish(topic=arn_topic, subject=subject,
message_structure=message_structure, message=json_msg, message_structure=message_structure, message=json_msg,
message_attributes=message_attributes) message_attributes=message_attributes)
except boto.exception.BotoServerError: except boto.exception.BotoServerError as e:
e = get_exception() module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.fail_json(msg=str(e))
module.exit_json(msg="OK") module.exit_json(msg="OK")
if __name__ == '__main__': if __name__ == '__main__':
main() main()