add traceback and more helpful errors for ec2_vpc_route_table (#25704)

This commit is contained in:
Sloane Hertel 2017-06-20 22:01:52 -04:00 committed by Will Thames
parent b41b4fe2fd
commit 04e93e22cb

View file

@ -131,7 +131,7 @@ EXAMPLES = '''
''' '''
import re import re
import traceback
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import AnsibleAWSError, connect_to_aws, ec2_argument_spec, get_aws_connection_info from ansible.module_utils.ec2 import AnsibleAWSError, connect_to_aws, ec2_argument_spec, get_aws_connection_info
@ -147,7 +147,9 @@ except ImportError:
class AnsibleRouteTableException(Exception): class AnsibleRouteTableException(Exception):
pass def __init__(self, message, error_traceback=None):
self.message = message
self.error_traceback = error_traceback
class AnsibleIgwSearchException(AnsibleRouteTableException): class AnsibleIgwSearchException(AnsibleRouteTableException):
@ -274,7 +276,8 @@ def ensure_tags(vpc_conn, resource_id, tags, add_only, check_mode):
return {'changed': True, 'tags': latest_tags} return {'changed': True, 'tags': latest_tags}
except EC2ResponseError as e: except EC2ResponseError as e:
raise AnsibleTagCreationException( raise AnsibleTagCreationException(
'Unable to update tags for {0}, error: {1}'.format(resource_id, e)) message='Unable to update tags for {0}, error: {1}'.format(resource_id, e),
error_traceback=traceback.format_exc())
def get_route_table_by_id(vpc_conn, vpc_id, route_table_id): def get_route_table_by_id(vpc_conn, vpc_id, route_table_id):
@ -472,16 +475,18 @@ def ensure_route_table_absent(connection, module):
try: try:
route_table = get_route_table_by_tags(connection, vpc_id, tags) route_table = get_route_table_by_tags(connection, vpc_id, tags)
except EC2ResponseError as e: except EC2ResponseError as e:
module.fail_json(msg=e.message) module.fail_json(msg="Error finding route table with lookup 'tag': {0}".format(e.message),
exception=traceback.format_exc())
except RuntimeError as e: except RuntimeError as e:
module.fail_json(msg=e.args[0]) module.fail_json(msg=e.args[0], exception=traceback.format_exc())
else: else:
route_table = None route_table = None
elif lookup == 'id': elif lookup == 'id':
try: try:
route_table = get_route_table_by_id(connection, vpc_id, route_table_id) route_table = get_route_table_by_id(connection, vpc_id, route_table_id)
except EC2ResponseError as e: except EC2ResponseError as e:
module.fail_json(msg=e.message) module.fail_json(msg="Error finding route table with lookup 'id': {0}".format(e.message),
exception=traceback.format_exc())
if route_table is None: if route_table is None:
return {'changed': False} return {'changed': False}
@ -494,7 +499,8 @@ def ensure_route_table_absent(connection, module):
if e.error_code == 'DryRunOperation': if e.error_code == 'DryRunOperation':
pass pass
else: else:
module.fail_json(msg=e.message) module.fail_json(msg="Error deleting route table: {0}".format(e.message),
exception=traceback.format_exc())
return {'changed': True} return {'changed': True}
@ -540,7 +546,8 @@ def ensure_route_table_present(connection, module):
try: try:
routes = create_route_spec(connection, module, vpc_id) routes = create_route_spec(connection, module, vpc_id)
except AnsibleIgwSearchException as e: except AnsibleIgwSearchException as e:
module.fail_json(msg=e[0]) module.fail_json(msg="Failed to find the Internet gateway for the given VPC ID {0}: {1}".format(vpc_id, e[0]),
exception=traceback.format_exc())
changed = False changed = False
tags_valid = False tags_valid = False
@ -550,16 +557,18 @@ def ensure_route_table_present(connection, module):
try: try:
route_table = get_route_table_by_tags(connection, vpc_id, tags) route_table = get_route_table_by_tags(connection, vpc_id, tags)
except EC2ResponseError as e: except EC2ResponseError as e:
module.fail_json(msg=e.message) module.fail_json(msg="Error finding route table with lookup 'tag': {0}".format(e.message),
exception=traceback.format_exc())
except RuntimeError as e: except RuntimeError as e:
module.fail_json(msg=e.args[0]) module.fail_json(msg=e.args[0], exception=traceback.format_exc())
else: else:
route_table = None route_table = None
elif lookup == 'id': elif lookup == 'id':
try: try:
route_table = get_route_table_by_id(connection, vpc_id, route_table_id) route_table = get_route_table_by_id(connection, vpc_id, route_table_id)
except EC2ResponseError as e: except EC2ResponseError as e:
module.fail_json(msg=e.message) module.fail_json(msg="Error finding route table with lookup 'id': {0}".format(e.message),
exception=traceback.format_exc())
# If no route table returned then create new route table # If no route table returned then create new route table
if route_table is None: if route_table is None:
@ -569,8 +578,8 @@ def ensure_route_table_present(connection, module):
except EC2ResponseError as e: except EC2ResponseError as e:
if e.error_code == 'DryRunOperation': if e.error_code == 'DryRunOperation':
module.exit_json(changed=True) module.exit_json(changed=True)
module.fail_json(msg="Failed to create route table: {0}".format(e.message),
module.fail_json(msg=e.message) exception=traceback.format_exc())
if routes is not None: if routes is not None:
try: try:
@ -579,7 +588,8 @@ def ensure_route_table_present(connection, module):
purge_routes) purge_routes)
changed = changed or result['changed'] changed = changed or result['changed']
except EC2ResponseError as e: except EC2ResponseError as e:
module.fail_json(msg=e.message) module.fail_json(msg="Error while updating routes: {0}".format(e.message),
exception=traceback.format_exc())
if propagating_vgw_ids is not None: if propagating_vgw_ids is not None:
result = ensure_propagation(connection, route_table, result = ensure_propagation(connection, route_table,
@ -599,8 +609,9 @@ def ensure_route_table_present(connection, module):
associated_subnets = find_subnets(connection, vpc_id, subnets) associated_subnets = find_subnets(connection, vpc_id, subnets)
except EC2ResponseError as e: except EC2ResponseError as e:
raise AnsibleRouteTableException( raise AnsibleRouteTableException(
'Unable to find subnets for route table {0}, error: {1}' message='Unable to find subnets for route table {0}, error: {1}'
.format(route_table, e) .format(route_table, e),
error_traceback=traceback.format_exc()
) )
try: try:
@ -611,8 +622,9 @@ def ensure_route_table_present(connection, module):
changed = changed or result['changed'] changed = changed or result['changed']
except EC2ResponseError as e: except EC2ResponseError as e:
raise AnsibleRouteTableException( raise AnsibleRouteTableException(
'Unable to associate subnets for route table {0}, error: {1}' message='Unable to associate subnets for route table {0}, error: {1}'
.format(route_table, e) .format(route_table, e),
error_traceback=traceback.format_exc()
) )
module.exit_json(changed=changed, route_table=get_route_table_info(route_table)) module.exit_json(changed=changed, route_table=get_route_table_info(route_table))
@ -663,7 +675,9 @@ def main():
elif state == 'absent': elif state == 'absent':
result = ensure_route_table_absent(connection, module) result = ensure_route_table_absent(connection, module)
except AnsibleRouteTableException as e: except AnsibleRouteTableException as e:
module.fail_json(msg=str(e)) if e.error_traceback:
module.fail_json(msg=e.message, exception=e.error_traceback)
module.fail_json(msg=e.message)
module.exit_json(**result) module.exit_json(**result)