diff --git a/moto/ec2/models.py b/moto/ec2/models.py index effad6047..a9e9f0ad7 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -84,6 +84,15 @@ class TaggedEC2Instance(object): tags = ec2_backend.describe_tags(self.id) return tags + def get_filter_value(self, filter_name): + tags = self.get_tags() + + if filter_name.startswith('tag:'): + tagname = filter_name.split('tag:')[1] + for tag in tags: + if tag['key'] == tagname: + return tag['value'] + class NetworkInterface(object): def __init__(self, subnet, private_ip_address, device_index=0, public_ip_auto_assign=True, group_ids=None): @@ -1142,10 +1151,15 @@ class VPC(TaggedEC2Instance): return self.dhcp_options.id - msg = "The filter '{0}' for DescribeVPCs has not been" \ - " implemented in Moto yet. Feel free to open an issue at" \ - " https://github.com/spulec/moto/issues".format(filter_name) - raise NotImplementedError(msg) + filter_value = super(VPC, self).get_filter_value(filter_name) + + if not filter_value: + msg = "The filter '{0}' for DescribeVPCs has not been" \ + " implemented in Moto yet. Feel free to open an issue at" \ + " https://github.com/spulec/moto/issues".format(filter_name) + raise NotImplementedError(msg) + + return filter_value class VPCBackend(object): diff --git a/tests/test_ec2/test_vpcs.py b/tests/test_ec2/test_vpcs.py index b7eed9bc0..66af6adac 100644 --- a/tests/test_ec2/test_vpcs.py +++ b/tests/test_ec2/test_vpcs.py @@ -110,4 +110,22 @@ def test_vpc_get_by_dhcp_options_id(): vpcs.should.have.length_of(2) vpc_ids = map(lambda v: v.id, vpcs) vpc1.id.should.be.within(vpc_ids) + vpc2.id.should.be.within(vpc_ids) + + +@mock_ec2 +def test_vpc_get_by_tag(): + conn = boto.connect_vpc() + vpc1 = conn.create_vpc("10.0.0.0/16") + vpc2 = conn.create_vpc("10.0.0.0/16") + vpc3 = conn.create_vpc("10.0.0.0/24") + + vpc1.add_tag('Name', 'TestVPC') + vpc2.add_tag('Name', 'TestVPC') + vpc3.add_tag('Name', 'TestVPC2') + + vpcs = conn.get_all_vpcs(filters={'tag:Name': 'TestVPC'}) + vpcs.should.have.length_of(2) + vpc_ids = map(lambda v: v.id, vpcs) + vpc1.id.should.be.within(vpc_ids) vpc2.id.should.be.within(vpc_ids) \ No newline at end of file