Skip to content

Commit 894b32f

Browse files
tremblejillr
authored andcommitted
Cleanup - use is_boto3_error_(message|code) (ansible-collections#268)
* 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
1 parent eee7166 commit 894b32f

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

plugins/modules/ec2_instance.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,8 @@
814814
from ansible.module_utils.six.moves.urllib import parse as urlparse
815815

816816
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
817+
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
818+
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_message
817819
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
818820
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list
819821
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_tag_list
@@ -1119,15 +1121,13 @@ def discover_security_groups(group, groups, parent_vpc_id=None, subnet_id=None,
11191121
if subnet_id is not None:
11201122
try:
11211123
sub = ec2.describe_subnets(aws_retry=True, SubnetIds=[subnet_id])
1122-
except botocore.exceptions.ClientError as e:
1123-
if e.response['Error']['Code'] == 'InvalidGroup.NotFound':
1124-
module.fail_json(
1125-
"Could not find subnet {0} to associate security groups. Please check the vpc_subnet_id and security_groups parameters.".format(
1126-
subnet_id
1127-
)
1124+
except is_boto3_error_code('InvalidGroup.NotFound'):
1125+
module.fail_json(
1126+
"Could not find subnet {0} to associate security groups. Please check the vpc_subnet_id and security_groups parameters.".format(
1127+
subnet_id
11281128
)
1129-
module.fail_json_aws(e, msg="Error while searching for subnet {0} parent VPC.".format(subnet_id))
1130-
except botocore.exceptions.BotoCoreError as e:
1129+
)
1130+
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
11311131
module.fail_json_aws(e, msg="Error while searching for subnet {0} parent VPC.".format(subnet_id))
11321132
parent_vpc_id = sub['Subnets'][0]['VpcId']
11331133

@@ -1615,9 +1615,9 @@ def determine_iam_role(name_or_arn):
16151615
try:
16161616
role = iam.get_instance_profile(InstanceProfileName=name_or_arn, aws_retry=True)
16171617
return role['InstanceProfile']['Arn']
1618-
except botocore.exceptions.ClientError as e:
1619-
if e.response['Error']['Code'] == 'NoSuchEntity':
1620-
module.fail_json_aws(e, msg="Could not find instance_role {0}".format(name_or_arn))
1618+
except is_boto3_error_code('NoSuchEntity'):
1619+
module.fail_json_aws(e, msg="Could not find instance_role {0}".format(name_or_arn))
1620+
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
16211621
module.fail_json_aws(e, msg="An error occurred while searching for instance_role {0}. Please try supplying the full ARN.".format(name_or_arn))
16221622

16231623

@@ -1697,15 +1697,12 @@ def ensure_present(existing_matches, changed, ec2, state):
16971697

16981698
def run_instances(ec2, **instance_spec):
16991699
try:
1700-
return ec2.run_instances(aws_retry=True, **instance_spec)
1701-
except botocore.exceptions.ClientError as e:
1702-
if e.response['Error']['Code'] == 'InvalidParameterValue' and "Invalid IAM Instance Profile ARN" in e.response['Error']['Message']:
1703-
# If the instance profile has just been created, it takes some time to be visible by ec2
1704-
# So we wait 10 second and retry the run_instances
1705-
time.sleep(10)
1706-
return ec2.run_instances(**instance_spec)
1707-
else:
1708-
raise e
1700+
return ec2.run_instances(**instance_spec)
1701+
except is_boto3_error_message('Invalid IAM Instance Profile ARN'):
1702+
# If the instance profile has just been created, it takes some time to be visible by ec2
1703+
# So we wait 10 second and retry the run_instances
1704+
time.sleep(10)
1705+
return ec2.run_instances(**instance_spec)
17091706

17101707

17111708
def main():

0 commit comments

Comments
 (0)