Skip to content

Commit

Permalink
Python 3 compatibility error handling: use to_native(e) instead of st…
Browse files Browse the repository at this point in the history
…r(e) or e.me… (ansible-collections#26)

* Py3 compat error handling: use to_native(e) instead of str(e) or e.message
* PR comment changes, use fail_json_aws and is_boto3_error_code

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections/community.aws@ffe14f9
  • Loading branch information
veloutin authored and jillr committed Aug 27, 2021
1 parent bc199cf commit 3f19de9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 11 additions & 7 deletions plugins/modules/ec2_vpc_nat_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils._text import to_native
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 get_aws_connection_info
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_conn
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict
Expand Down Expand Up @@ -698,13 +700,15 @@ def create(client, subnet_id, allocation_id, client_token=None,
'NAT gateway {0} created'.format(result['nat_gateway_id'])
)

except botocore.exceptions.ClientError as e:
if "IdempotentParameterMismatch" in e.message:
err_msg = (
'NAT Gateway does not support update and token has already been provided: ' + str(e)
)
else:
err_msg = str(e)
except is_boto3_error_code('IdempotentParameterMismatch'):
err_msg = (
'NAT Gateway does not support update and token has already been provided: ' + err_msg
)
success = False
changed = False
result = None
except botocore.exceptions.ClientError as e: # pylint: disable=duplicate-except
err_msg = to_native(e)
success = False
changed = False
result = None
Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/ec2_vpc_nat_gateway_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils._text import to_native
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_conn
Expand All @@ -105,7 +106,7 @@ def get_nat_gateways(client, module, nat_gateway_id=None):
try:
result = json.loads(json.dumps(client.describe_nat_gateways(**params), default=date_handler))
except Exception as e:
module.fail_json(msg=str(e.message))
module.fail_json(msg=to_native(e))

for gateway in result['NatGateways']:
# Turn the boto3 result into ansible_friendly_snaked_names
Expand Down

0 comments on commit 3f19de9

Please sign in to comment.