From a63601e4819f2e4fcd7b0c79a426cfc15c442456 Mon Sep 17 00:00:00 2001 From: Ilya Sukhanov Date: Thu, 29 Aug 2013 23:06:11 -0400 Subject: [PATCH] Implement ImageId parameter in DescribeImages --- moto/ec2/models.py | 8 ++++++-- moto/ec2/responses/amis.py | 5 +++-- moto/ec2/utils.py | 8 ++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index ccde643e9..39b3c83ff 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -215,8 +215,12 @@ class AmiBackend(object): self.amis[ami_id] = ami return ami - def describe_images(self): - return self.amis.values() + def describe_images(self, ami_ids=None): + if ami_ids: + images = [image for image in self.amis.values() if image.id in ami_ids] + else: + images = self.amis.values() + return images def deregister_image(self, ami_id): if ami_id in self.amis: diff --git a/moto/ec2/responses/amis.py b/moto/ec2/responses/amis.py index b6e856388..10936e635 100644 --- a/moto/ec2/responses/amis.py +++ b/moto/ec2/responses/amis.py @@ -1,7 +1,7 @@ from jinja2 import Template from moto.ec2.models import ec2_backend -from moto.ec2.utils import instance_ids_from_querystring +from moto.ec2.utils import instance_ids_from_querystring, image_ids_from_querystring class AmisResponse(object): @@ -33,7 +33,8 @@ class AmisResponse(object): raise NotImplementedError('AMIs.describe_image_attribute is not yet implemented') def describe_images(self): - images = ec2_backend.describe_images() + 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) diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 2710cc46d..5fcafb835 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -54,6 +54,14 @@ def instance_ids_from_querystring(querystring_dict): return instance_ids +def image_ids_from_querystring(querystring_dict): + image_ids = [] + for key, value in querystring_dict.iteritems(): + if 'ImageId' in key: + image_ids.append(value[0]) + return image_ids + + def resource_ids_from_querystring(querystring_dict): prefix = 'ResourceId' response_values = {}