Improve ec2_ami tests (#38987)

* Improve ec2_ami tests

Ensure that ec2_ami_image_id fact gets set immediately after AMI
creation so that they get torn down even if tests fail

Use YAML anchor to simplify AWS credential passing

Use aws_connection_info to reduce AWS credential boilerplate

Improve exception handling when updating image attributes

Error messages weren't correctly formatted to show image ids.
This commit is contained in:
Will Thames 2018-04-23 21:54:49 +10:00 committed by Sloane Hertel
parent 176ebfd471
commit 412373ce8e
2 changed files with 48 additions and 133 deletions

View file

@ -406,15 +406,6 @@ def create_image(module, connection):
'Description': description
}
images = connection.describe_images(
Filters=[
{
'Name': 'name',
'Values': [name]
}
]
).get('Images')
block_device_mapping = None
if device_mapping:
@ -572,7 +563,7 @@ def update_image(module, connection, image_id):
LaunchPermission=dict(Add=to_add, Remove=to_remove))
changed = True
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg="Error updating launch permissions")
module.fail_json_aws(e, msg="Error updating launch permissions of image %s" % image_id)
desired_tags = module.params.get('tags')
if desired_tags is not None:
@ -626,9 +617,9 @@ def get_image_by_id(module, connection, image_id):
result['ProductCodes'] = connection.describe_image_attribute(Attribute='productCodes', ImageId=image_id)['ProductCodes']
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] != 'InvalidAMIID.Unavailable':
module.fail_json_aws(e, msg="Error retrieving image attributes %s" % image_id)
module.fail_json_aws(e, msg="Error retrieving image attributes for image %s" % image_id)
except botocore.exceptions.BotoCoreError as e:
module.fail_json_aws(e, msg="Error retrieving image attributes %s" % image_id)
module.fail_json_aws(e, msg="Error retrieving image attributes for image %s" % image_id)
return result
module.fail_json(msg="Invalid number of instances (%s) found for image_id: %s." % (str(len(images)), image_id))
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: