From 866d8b4ea8131fb0b1bb36310495075ad7b0381e Mon Sep 17 00:00:00 2001 From: Joseph Lawson Date: Tue, 30 Sep 2014 16:12:23 -0400 Subject: [PATCH 1/2] allow get_filter_value to return state of ami and test * updated Ami.get_filter_value to return the state value of the AMI * updated test_ec2.test_amis.test_ami_filters to test for AMIs in the state of 'available' --- moto/ec2/models.py | 2 ++ tests/test_ec2/test_amis.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 20fe46b25..ccfa5d716 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -604,6 +604,8 @@ class Ami(TaggedEC2Instance): return getattr(self,filter_name) elif filter_name == 'image-id': return self.id + elif filter_name == 'state': + return self.state 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..1004cfb17 100644 --- a/tests/test_ec2/test_amis.py +++ b/tests/test_ec2/test_amis.py @@ -132,6 +132,9 @@ 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])) + @mock_ec2 def test_getting_missing_ami(): From 7fca88e73242b89feffaa61a053723c8864fdbf9 Mon Sep 17 00:00:00 2001 From: Joseph Lawson Date: Tue, 30 Sep 2014 16:42:52 -0400 Subject: [PATCH 2/2] Test Fixes and `tag:` filter. * fixed test_ec2.test_amis.test_ami_filters to test for AMIs in the state of 'available' * enhanced AMI's to be able to take `tag:` filter. * added tag: tests for AMIs --- moto/ec2/models.py | 4 ++++ tests/test_ec2/test_amis.py | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index ccfa5d716..2013fbf04 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -606,6 +606,10 @@ class Ami(TaggedEC2Instance): 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 1004cfb17..49221cc96 100644 --- a/tests/test_ec2/test_amis.py +++ b/tests/test_ec2/test_amis.py @@ -133,7 +133,30 @@ def test_ami_filters(): 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])) + 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