Skip to content

Commit

Permalink
Cleanup - use is_boto3_error_(message|code) (ansible-collections#268)
Browse files Browse the repository at this point in the history
* Reorder imports
* Make use of is_boto3_error_message
* Mass-migration over to is_boto3_error_code
* Remove unused imports
* unused vars in exception
* Improve consistency around catching BotoCoreError and ClientError
* Remove unused imports
* Remove unused 'PolicyError' from iam_policy_info
* Avoid catching botocore.exceptions.ClientError when we only want some error codes
* Import camel_dict_to_snake_dict/snake_dict_to_camel_dict from ansible.module_utils.common.dict_transformations

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections/community.aws@4cf52ef
  • Loading branch information
tremble authored and mandar242 committed Oct 24, 2024
1 parent 54e51bb commit fd480de
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions plugins/modules/ec2_transit_gateway_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,17 @@
'''

try:
from botocore.exceptions import BotoCoreError, ClientError
import botocore
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.ec2 import (
AWSRetry,
boto3_tag_list_to_ansible_dict,
camel_dict_to_snake_dict,
ansible_dict_to_boto3_filter_list
)
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 ansible_dict_to_boto3_filter_list
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict


class AnsibleEc2TgwInfo(object):
Expand Down Expand Up @@ -208,11 +208,9 @@ def describe_transit_gateways(self):
try:
response = self._connection.describe_transit_gateways(
TransitGatewayIds=transit_gateway_ids, Filters=filters)
except ClientError as e:
if e.response['Error']['Code'] == 'InvalidTransitGatewayID.NotFound':
self._results['transit_gateways'] = []
return
raise
except is_boto3_error_code('InvalidTransitGatewayID.NotFound'):
self._results['transit_gateways'] = []
return

for transit_gateway in response['TransitGateways']:
transit_gateway_info.append(camel_dict_to_snake_dict(transit_gateway, ignore_list=['Tags']))
Expand Down Expand Up @@ -253,7 +251,7 @@ def main():
tgwf_manager = AnsibleEc2TgwInfo(module=module, results=results)
try:
tgwf_manager.describe_transit_gateways()
except (BotoCoreError, ClientError) as e:
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e)

module.exit_json(**results)
Expand Down

0 comments on commit fd480de

Please sign in to comment.