From 20d8318997dfd13bd700f458848679240fa3f8e1 Mon Sep 17 00:00:00 2001 From: Hugo Lopes Tavares Date: Tue, 24 Feb 2015 17:56:26 -0500 Subject: [PATCH] Add support to tag filtering to Security Groups --- moto/ec2/models.py | 7 ++++++- tests/test_ec2/test_security_groups.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index ac0bb4ddc..1336eedb8 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -92,7 +92,9 @@ from .utils import ( filter_reservations, random_network_acl_id, random_network_acl_subnet_association_id, - random_vpn_gateway_id) + random_vpn_gateway_id, + is_tag_filter, +) def validate_resource_ids(resource_ids): @@ -1113,6 +1115,9 @@ class SecurityGroup(TaggedEC2Resource): for ingress in self.ingress_rules: if getattr(ingress, ingress_attr) in filter_value: return True + elif is_tag_filter(key): + tag_value = self.get_filter_value(key) + return tag_value in filter_value else: attr_name = to_attr(key) return getattr(self, attr_name) in filter_value diff --git a/tests/test_ec2/test_security_groups.py b/tests/test_ec2/test_security_groups.py index 861e492b8..e555bd694 100644 --- a/tests/test_ec2/test_security_groups.py +++ b/tests/test_ec2/test_security_groups.py @@ -248,3 +248,13 @@ def test_security_group_tagging(): group = conn.get_all_security_groups("test-sg")[0] group.tags.should.have.length_of(1) group.tags["Test"].should.equal("Tag") + + +@mock_ec2 +def test_security_group_tag_filtering(): + conn = boto.connect_ec2() + sg = conn.create_security_group("test-sg", "Test SG") + sg.add_tag("test-tag", "test-value") + + groups = conn.get_all_security_groups(filters={"tag:test-tag": "test-value"}) + groups.should.have.length_of(1)