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 abikouo committed Oct 16, 2024
1 parent 7d3e487 commit 7fc8983
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions plugins/modules/ec2_vpc_nacl_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,17 @@
'''

try:
from botocore.exceptions import ClientError, BotoCoreError
import botocore
except ImportError:
pass # caught by 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,
ansible_dict_to_boto3_filter_list,
camel_dict_to_snake_dict,
boto3_tag_list_to_ansible_dict,
)
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


# VPC-supported IANA protocol numbers
Expand All @@ -132,11 +133,9 @@ def list_ec2_vpc_nacls(connection, module):

try:
nacls = connection.describe_network_acls(aws_retry=True, NetworkAclIds=nacl_ids, Filters=filters)
except ClientError as e:
if e.response['Error']['Code'] == 'InvalidNetworkAclID.NotFound':
module.fail_json(msg='Unable to describe ACL. NetworkAcl does not exist')
module.fail_json_aws(e, msg="Unable to describe network ACLs {0}".format(nacl_ids))
except BotoCoreError as e:
except is_boto3_error_code('InvalidNetworkAclID.NotFound'):
module.fail_json(msg='Unable to describe ACL. NetworkAcl does not exist')
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Unable to describe network ACLs {0}".format(nacl_ids))

# Turn the boto3 result in to ansible_friendly_snaked_names
Expand Down

0 comments on commit 7fc8983

Please sign in to comment.