From 7149a817b023b5f731a79901af8de4ca671a0247 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Wed, 13 Mar 2013 17:16:20 +1100 Subject: [PATCH] Fix security groups (need to be accessed as attributes) bug 1134193 Thanks Lukas Barton for the patch in the bug. Signed-off-by: Angus Salkeld Change-Id: I448ba76fcf47d4e02775ff8eca38897eb399a3cc --- heat/engine/resources/security_group.py | 8 +-- heat/tests/test_security_group.py | 67 ++++++++++++++----------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/heat/engine/resources/security_group.py b/heat/engine/resources/security_group.py index 0c55e7a9..c62dffad 100644 --- a/heat/engine/resources/security_group.py +++ b/heat/engine/resources/security_group.py @@ -38,7 +38,7 @@ class SecurityGroup(resource.Resource): groups = self.nova().security_groups.list() for group in groups: - if group['name'] == self.physical_resource_name(): + if group.name == self.physical_resource_name(): sec = group break @@ -47,12 +47,12 @@ class SecurityGroup(resource.Resource): self.physical_resource_name(), self.properties['GroupDescription']) - self.resource_id_set(sec['id']) + self.resource_id_set(sec.id) if self.properties['SecurityGroupIngress']: rules_client = self.nova().security_group_rules for i in self.properties['SecurityGroupIngress']: try: - rule = rules_client.create(sec['id'], + rule = rules_client.create(sec.id, i['IpProtocol'], i['FromPort'], i['ToPort'], @@ -75,7 +75,7 @@ class SecurityGroup(resource.Resource): except clients.novaclient.exceptions.NotFound: pass else: - for rule in sec['rules']: + for rule in sec.rules: try: self.nova().security_group_rules.delete(rule['id']) except clients.novaclient.exceptions.NotFound: diff --git a/heat/tests/test_security_group.py b/heat/tests/test_security_group.py index 0364e8c9..bfa30dd4 100644 --- a/heat/tests/test_security_group.py +++ b/heat/tests/test_security_group.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. - +import collections import unittest import mox @@ -29,6 +29,15 @@ from novaclient.v1_1 import security_groups as nova_sg from novaclient.v1_1 import security_group_rules as nova_sgr +NovaSG = collections.namedtuple('NovaSG', + ' '.join([ + 'name', + 'id', + 'rules', + 'description', + ])) + + @attr(tag=['unit', 'resource']) @attr(speed='fast') class SecurityGroupTest(unittest.TestCase): @@ -93,19 +102,20 @@ Resources: def test_security_group_nova(self): #create script clients.OpenStackClients.nova('compute').AndReturn(self.fc) - nova_sg.SecurityGroupManager.list().AndReturn([{ - 'id': 1, - 'name': 'test', - 'description': 'FAKE_SECURITY_GROUP' - }]) + nova_sg.SecurityGroupManager.list().AndReturn([NovaSG( + id=1, + name='test', + description='FAKE_SECURITY_GROUP', + rules=[], + )]) clients.OpenStackClients.nova('compute').AndReturn(self.fc) nova_sg.SecurityGroupManager.create( 'test_stack.the_sg', - 'HTTP and SSH access').AndReturn({ - 'id': 2, - 'name': 'test_stack.the_sg', - 'description': 'HTTP and SSH access' - }) + 'HTTP and SSH access').AndReturn(NovaSG( + id=2, + name='test_stack.the_sg', + description='HTTP and SSH access', + rules=[])) clients.OpenStackClients.nova('compute').AndReturn(self.fc) nova_sgr.SecurityGroupRuleManager.create( @@ -115,11 +125,11 @@ Resources: # delete script clients.OpenStackClients.nova('compute').AndReturn(self.fc) - nova_sg.SecurityGroupManager.get(2).AndReturn({ - 'id': 2, - 'name': 'test_stack.the_sg', - 'description': 'HTTP and SSH access', - "rules": [{ + nova_sg.SecurityGroupManager.get(2).AndReturn(NovaSG( + id=2, + name='test_stack.the_sg', + description='HTTP and SSH access', + rules=[{ "from_port": 22, "group": {}, "ip_protocol": "tcp", @@ -140,7 +150,7 @@ Resources: }, "id": 131 }] - }) + )) clients.OpenStackClients.nova('compute').AndReturn(self.fc) nova_sgr.SecurityGroupRuleManager.delete(130).AndReturn(None) clients.OpenStackClients.nova('compute').AndReturn(self.fc) @@ -162,11 +172,12 @@ Resources: def test_security_group_nova_exception(self): #create script clients.OpenStackClients.nova('compute').AndReturn(self.fc) - nova_sg.SecurityGroupManager.list().AndReturn([{ - 'id': 2, - 'name': 'test_stack.the_sg', - 'description': 'HTTP and SSH access' - }]) + nova_sg.SecurityGroupManager.list().AndReturn([NovaSG( + id=2, + name='test_stack.the_sg', + description='HTTP and SSH access', + rules=[], + )]) clients.OpenStackClients.nova('compute').AndReturn(self.fc) nova_sgr.SecurityGroupRuleManager.create( @@ -180,11 +191,11 @@ Resources: # delete script clients.OpenStackClients.nova('compute').AndReturn(self.fc) - nova_sg.SecurityGroupManager.get(2).AndReturn({ - 'id': 2, - 'name': 'test_stack.the_sg', - 'description': 'HTTP and SSH access', - "rules": [{ + nova_sg.SecurityGroupManager.get(2).AndReturn(NovaSG( + id=2, + name='test_stack.the_sg', + description='HTTP and SSH access', + rules=[{ "from_port": 22, "group": {}, "ip_protocol": "tcp", @@ -205,7 +216,7 @@ Resources: }, "id": 131 }] - }) + )) clients.OpenStackClients.nova('compute').AndReturn(self.fc) nova_sgr.SecurityGroupRuleManager.delete(130).AndRaise( clients.novaclient.exceptions.NotFound('goneburger')) -- 2.45.2