Fix DescribeImages to throw error if AMI requested does not exist

This commit is contained in:
Hugo Lopes Tavares 2014-01-28 18:14:00 -05:00
commit 90aa87d53e
5 changed files with 25 additions and 6 deletions

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>