Add listing security groups functionality 97/55797/6
authorManuel Buil <mbuil@suse.com>
Mon, 16 Apr 2018 08:24:34 +0000 (10:24 +0200)
committerManuel Buil <mbuil@suse.com>
Tue, 17 Apr 2018 16:43:44 +0000 (18:43 +0200)
JIRA: SNAPS-302

Be able to list the available security groups

Change-Id: I82e2daeb388f4eb3bc8cbc5fd02006b30b959c2f
Signed-off-by: Manuel Buil <mbuil@suse.com>
snaps/openstack/utils/neutron_utils.py
snaps/openstack/utils/tests/neutron_utils_tests.py

index 0d69fc8..3daf1f7 100644 (file)
@@ -681,6 +681,21 @@ def get_security_group_by_id(neutron, sec_grp_id):
     return None
 
 
+def list_security_groups(neutron):
+
+    """
+    Lists the available security groups
+    :param neutron: the neutron client
+    """
+    logger.info('Listing the available security groups')
+    sec_groups = []
+    response = neutron.list_security_groups()
+    for sg in response['security_groups']:
+        sec_groups.append(__map_os_security_group(neutron, sg))
+
+    return sec_groups
+
+
 def create_security_group_rule(neutron, keystone, sec_grp_rule_settings,
                                proj_name):
     """
index cd293ca..1c874cc 100644 (file)
@@ -1010,6 +1010,33 @@ class NeutronUtilsSecurityGroupTests(OSComponentTestCase):
         self.assertEqual(self.security_groups[0].id, sec_grp_1b.id)
         self.assertEqual(self.security_groups[1].id, sec_grp_2b.id)
 
+    def test_create_list_sec_grp_no_rules(self):
+        """
+        Tests the neutron_utils.create_security_group() and
+        list_security_groups function
+        """
+        sec_grp_settings = SecurityGroupConfig(
+            name=self.sec_grp_name + "-1", description='hello group')
+        self.security_groups.append(neutron_utils.create_security_group(
+            self.neutron, self.keystone, sec_grp_settings))
+
+        sec_grp_settings2 = SecurityGroupConfig(
+            name=self.sec_grp_name + "-2", description='hola group')
+        self.security_groups.append(neutron_utils.create_security_group(
+            self.neutron, self.keystone, sec_grp_settings2))
+
+        returned_sec_groups = neutron_utils.list_security_groups(self.neutron)
+
+        self.assertIsNotNone(returned_sec_groups)
+        worked = 0
+        for sg in returned_sec_groups:
+            if sec_grp_settings.name == sg.name:
+                worked += 1
+            elif sec_grp_settings2.name == sg.name:
+                worked += 1
+
+        self.assertEqual(worked, 2)
+
 
 class NeutronUtilsFloatingIpTests(OSComponentTestCase):
     """