diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 73df55897..f87853e07 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -604,6 +604,12 @@ class Ami(TaggedEC2Instance): return getattr(self,filter_name) elif filter_name == 'image-id': return self.id + elif filter_name == 'state': + return self.state + elif filter_name.startswith('tag:'): + tag_name = filter_name.replace('tag:', '', 1) + tags = dict((tag['key'], tag['value']) for tag in self.get_tags()) + return tags.get(tag_name) else: ec2_backend.raise_not_implemented_error("The filter '{0}' for DescribeImages".format(filter_name)) diff --git a/tests/test_ec2/test_amis.py b/tests/test_ec2/test_amis.py index 95fe9f920..49221cc96 100644 --- a/tests/test_ec2/test_amis.py +++ b/tests/test_ec2/test_amis.py @@ -132,6 +132,32 @@ def test_ami_filters(): amis_by_id = conn.get_all_images(filters={'image-id': imageA.id}) set([ami.id for ami in amis_by_id]).should.equal(set([imageA.id])) + amis_by_id = conn.get_all_images(filters={'state': 'available'}) + set([ami.id for ami in amis_by_id]).should.equal(set([imageA.id, imageB.id])) + + +@mock_ec2 +def test_ami_filtering_via_tag(): + conn = boto.connect_vpc('the_key', 'the_secret') + + reservationA = conn.run_instances('ami-1234abcd') + instanceA = reservationA.instances[0] + imageA_id = conn.create_image(instanceA.id, "test-ami-A", "this is a test ami") + imageA = conn.get_image(imageA_id) + imageA.add_tag("a key", "some value") + + reservationB = conn.run_instances('ami-abcd1234') + instanceB = reservationB.instances[0] + imageB_id = conn.create_image(instanceB.id, "test-ami-B", "this is a test ami") + imageB = conn.get_image(imageB_id) + imageB.add_tag("another key", "some other value") + + amis_by_tagA = conn.get_all_images(filters={'tag:a key': 'some value'}) + set([ami.id for ami in amis_by_tagA]).should.equal(set([imageA.id])) + + amis_by_tagB = conn.get_all_images(filters={'tag:another key': 'some other value'}) + set([ami.id for ami in amis_by_tagB]).should.equal(set([imageB.id])) + @mock_ec2 def test_getting_missing_ami():