Merge pull request #86 from hltbra/fix-describe-images-for-missing-images

Fix DescribeImages for missing images
This commit is contained in:
Steve Pulec 2014-01-28 15:36:12 -08:00
commit 61512aaae9
6 changed files with 26 additions and 7 deletions

View File

@ -1,4 +1,4 @@
class InvalidIdError(RuntimeError):
def __init__(self, instance_id):
def __init__(self, id_value):
super(InvalidIdError, self).__init__()
self.instance_id = instance_id
self.id = id_value

View File

@ -222,6 +222,8 @@ class AmiBackend(object):
def describe_images(self, ami_ids=None):
if ami_ids:
images = [image for image in self.amis.values() if image.id in ami_ids]
for missing_ami in set(ami_ids) - set(ami.id for ami in images):
raise InvalidIdError(missing_ami)
else:
images = self.amis.values()
return images

View File

@ -2,6 +2,7 @@ from jinja2 import Template
from moto.core.responses import BaseResponse
from moto.ec2.models import ec2_backend
from moto.ec2.exceptions import InvalidIdError
from moto.ec2.utils import instance_ids_from_querystring, image_ids_from_querystring
@ -35,9 +36,14 @@ class AmisResponse(BaseResponse):
def describe_images(self):
ami_ids = image_ids_from_querystring(self.querystring)
images = ec2_backend.describe_images(ami_ids=ami_ids)
template = Template(DESCRIBE_IMAGES_RESPONSE)
return template.render(images=images)
try:
images = ec2_backend.describe_images(ami_ids=ami_ids)
except InvalidIdError as exc:
template = Template(DESCRIBE_IMAGES_INVALID_IMAGE_ID_RESPONSE)
return template.render(image_id=exc.id), dict(status=400)
else:
template = Template(DESCRIBE_IMAGES_RESPONSE)
return template.render(images=images)
def modify_image_attribute(self):
raise NotImplementedError('AMIs.modify_image_attribute is not yet implemented')
@ -101,6 +107,11 @@ DESCRIBE_IMAGE_RESPONSE = """<DescribeImageAttributeResponse xmlns="http://ec2.a
</{{key }}>
</DescribeImageAttributeResponse>"""
DESCRIBE_IMAGES_INVALID_IMAGE_ID_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidAMIID.NotFound</Code><Message>The image id '[{{ image_id }}]' does not exist</Message></Error></Errors><RequestID>59dbff89-35bd-4eac-99ed-be587EXAMPLE</RequestID></Response>
"""
DEREGISTER_IMAGE_RESPONSE = """<DeregisterImageResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<return>{{ success }}</return>

View File

@ -15,7 +15,7 @@ class InstanceResponse(BaseResponse):
reservations = ec2_backend.get_reservations_by_instance_ids(instance_ids)
except InvalidIdError as exc:
template = Template(EC2_INVALID_INSTANCE_ID)
return template.render(instance_id=exc.instance_id), dict(status=400)
return template.render(instance_id=exc.id), dict(status=400)
else:
reservations = ec2_backend.all_reservations(make_copy=True)

View File

@ -15,5 +15,5 @@ class requires_boto_gte(object):
boto_version = version_tuple(boto.__version__)
required = version_tuple(self.version)
if boto_version >= required:
return test()
return test
raise SkipTest

View File

@ -39,3 +39,9 @@ def test_ami_pulls_attributes_from_instance():
image_id = conn.create_image(instance.id, "test-ami", "this is a test ami")
image = conn.get_image(image_id)
image.kernel_id.should.equal('test-kernel')
@mock_ec2
def test_getting_missing_ami():
conn = boto.connect_ec2('the_key', 'the_secret')
conn.get_image.when.called_with('ami-missing').should.throw(EC2ResponseError)