Skip to content

Commit

Permalink
Make use of is_boto3_error_message
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Oct 20, 2020
1 parent 808a6e6 commit 0a27968
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
10 changes: 4 additions & 6 deletions plugins/modules/aws_elasticbeanstalk_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
pass # handled by AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_message


def describe_app(ebs, app_name, module):
Expand Down Expand Up @@ -208,13 +209,10 @@ def main():
else:
ebs.delete_application(ApplicationName=app_name)
changed = True
except BotoCoreError as e:
except is_boto3_error_message('It is currently pending deletion'):
changed = False
except (ClientError, BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Cannot terminate app")
except ClientError as e:
if 'It is currently pending deletion.' not in e.response['Error']['Message']:
module.fail_json_aws(e, msg="Cannot terminate app")
else:
changed = False

result = dict(changed=changed, app=app)

Expand Down
16 changes: 7 additions & 9 deletions plugins/modules/cloudfront_invalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
pass # caught by imported AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_message
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import snake_dict_to_camel_dict
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict
from ansible_collections.amazon.aws.plugins.module_utils.cloudfront_facts import CloudFrontFactsServiceManager
Expand All @@ -167,16 +168,13 @@ def create_invalidation(self, distribution_id, invalidation_batch):
return response, False
else:
return response, True
except BotoCoreError as e:
except is_boto3_error_message('Your request contains a caller reference that was used for a previous invalidation '
'batch for the same distribution.'):
self.module.warn("InvalidationBatch target paths are not modifiable. "
"To make a new invalidation please update caller_reference.")
return current_invalidation_response, False
except (ClientError, BotoCoreError) as e: # pylint: disable=duplicate-except
self.module.fail_json_aws(e, msg="Error creating CloudFront invalidations.")
except ClientError as e:
if ('Your request contains a caller reference that was used for a previous invalidation batch '
'for the same distribution.' in e.response['Error']['Message']):
self.module.warn("InvalidationBatch target paths are not modifiable. "
"To make a new invalidation please update caller_reference.")
return current_invalidation_response, False
else:
self.module.fail_json_aws(e, msg="Error creating CloudFront invalidations.")

def get_invalidation(self, distribution_id, caller_reference):
current_invalidation = {}
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/ec2_asg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ def create_autoscaling_group(connection):
try:
attach_load_balancers(connection, group_name, list(elbs_to_attach))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to attach load balancers %s" % (elbs_to_attach)
module.fail_json_aws(e, msg="Failed to attach load balancers %s" % (elbs_to_attach))

# Handle target group attachments/detachments
# Attach target groups if they are specified but none currently exist
Expand Down
14 changes: 6 additions & 8 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@

import ansible_collections.amazon.aws.plugins.module_utils.ec2 as ec2_utils
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_message
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 compare_aws_tags
Expand Down Expand Up @@ -1694,14 +1695,11 @@ def ensure_present(existing_matches, changed, ec2, state):
def run_instances(ec2, **instance_spec):
try:
return ec2.run_instances(**instance_spec)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'InvalidParameterValue' and "Invalid IAM Instance Profile ARN" in e.response['Error']['Message']:
# If the instance profile has just been created, it takes some time to be visible by ec2
# So we wait 10 second and retry the run_instances
time.sleep(10)
return ec2.run_instances(**instance_spec)
else:
raise e
except is_boto3_error_message('Invalid IAM Instance Profile ARN'):
# If the instance profile has just been created, it takes some time to be visible by ec2
# So we wait 10 second and retry the run_instances
time.sleep(10)
return ec2.run_instances(**instance_spec)


def main():
Expand Down
8 changes: 3 additions & 5 deletions plugins/modules/rds_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@

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.core import is_boto3_error_message
from ansible_collections.amazon.aws.plugins.module_utils.core import get_boto3_client_method_parameters
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_tag_list
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
Expand Down Expand Up @@ -1040,11 +1041,8 @@ def promote_replication_instance(client, module, instance, read_replica):
try:
call_method(client, module, method_name='promote_read_replica', parameters={'DBInstanceIdentifier': instance['DBInstanceIdentifier']})
changed = True
except is_boto3_error_code('InvalidDBInstanceState') as e:
if 'DB Instance is not a read replica' in e.response['Error']['Message']:
pass
else:
raise e
except is_boto3_error_message('DB Instance is not a read replica'):
pass
return changed


Expand Down

0 comments on commit 0a27968

Please sign in to comment.