summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils
diff options
context:
space:
mode:
Diffstat (limited to 'snaps/openstack/utils')
-rw-r--r--snaps/openstack/utils/launch_utils.py2
-rw-r--r--snaps/openstack/utils/neutron_utils.py39
-rw-r--r--snaps/openstack/utils/tests/neutron_utils_tests.py63
-rw-r--r--snaps/openstack/utils/tests/nova_utils_tests.py6
-rw-r--r--snaps/openstack/utils/tests/settings_utils_tests.py1
5 files changed, 81 insertions, 30 deletions
diff --git a/snaps/openstack/utils/launch_utils.py b/snaps/openstack/utils/launch_utils.py
index fd78bd2..49d41e7 100644
--- a/snaps/openstack/utils/launch_utils.py
+++ b/snaps/openstack/utils/launch_utils.py
@@ -307,6 +307,8 @@ def __create_instances(os_creds_dict, creator_class, config_class, config,
creator.create()
out[inst_config['name']] = creator
+ else:
+ raise Exception('Unable to instantiate creator')
logger.info('Initialized configured %ss', config_key)
diff --git a/snaps/openstack/utils/neutron_utils.py b/snaps/openstack/utils/neutron_utils.py
index 3daf1f7..8783690 100644
--- a/snaps/openstack/utils/neutron_utils.py
+++ b/snaps/openstack/utils/neutron_utils.py
@@ -210,16 +210,17 @@ def delete_subnet(neutron, subnet):
neutron.delete_subnet(subnet.id)
-def get_subnet(neutron, subnet_settings=None, subnet_name=None):
+def get_subnet(neutron, network, subnet_settings=None, subnet_name=None):
"""
Returns the first subnet object that fits the query else None including
if subnet_settings or subnet_name parameters are None.
:param neutron: the client
+ :param network: the associated SNAPS-OO Network domain object
:param subnet_settings: the subnet settings of the object to retrieve
:param subnet_name: the name of the subnet to retrieve
:return: a SNAPS-OO Subnet domain object or None
"""
- sub_filter = dict()
+ sub_filter = {'network_id': network.id}
if subnet_settings:
sub_filter['name'] = subnet_settings.name
sub_filter['cidr'] = subnet_settings.cidr
@@ -250,6 +251,33 @@ def get_subnet(neutron, subnet_settings=None, subnet_name=None):
return Subnet(**subnet)
+def get_subnet_by_name(neutron, keystone, subnet_name, project_name=None):
+ """
+ Returns the first subnet object that fits the query else None including
+ if subnet_settings or subnet_name parameters are None.
+ :param neutron: the Neutron client
+ :param keystone: the Keystone client
+ :param subnet_name: the name of the subnet to retrieve
+ :param project_name: the name of the associated project to the subnet to
+ retrieve
+ :return: a SNAPS-OO Subnet domain object or None
+ """
+ project = None
+ if project_name:
+ project = keystone_utils.get_project(
+ keystone, project_name=project_name)
+ if project:
+ sub_filter = {'name': subnet_name, 'project_id': project.id}
+ subnets = neutron.list_subnets(**sub_filter)
+ for subnet in subnets['subnets']:
+ return Subnet(**subnet)
+ else:
+ sub_filter = {'name': subnet_name}
+ subnets = neutron.list_subnets(**sub_filter)
+ for subnet in subnets['subnets']:
+ return Subnet(**subnet)
+
+
def get_subnet_by_id(neutron, subnet_id):
"""
Returns a SNAPS-OO Subnet domain object for a given ID
@@ -635,7 +663,6 @@ def get_security_group(neutron, keystone, sec_grp_settings=None,
return None
groups = neutron.list_security_groups(**sec_grp_filter)
- group = None
for group in groups['security_groups']:
if project_name:
if 'project_id' in group.keys():
@@ -645,11 +672,9 @@ def get_security_group(neutron, keystone, sec_grp_settings=None,
project = keystone_utils.get_project_by_id(
keystone, group['tenant_id'])
if project and project_name == project.name:
- break
+ return __map_os_security_group(neutron, group)
else:
- break
- if group:
- return __map_os_security_group(neutron, group)
+ return __map_os_security_group(neutron, group)
def __map_os_security_group(neutron, os_sec_grp):
diff --git a/snaps/openstack/utils/tests/neutron_utils_tests.py b/snaps/openstack/utils/tests/neutron_utils_tests.py
index 1c874cc..d43b969 100644
--- a/snaps/openstack/utils/tests/neutron_utils_tests.py
+++ b/snaps/openstack/utils/tests/neutron_utils_tests.py
@@ -94,6 +94,7 @@ class NeutronUtilsNetworkTests(OSComponentTestCase):
self.os_creds, self.os_session)
self.network = None
self.net_config = openstack_tests.get_pub_net_config(
+ project_name=self.os_creds.project_name,
net_name=guid + '-pub-net')
def tearDown(self):
@@ -155,6 +156,7 @@ class NeutronUtilsSubnetTests(OSComponentTestCase):
self.os_creds, self.os_session)
self.network = None
self.net_config = openstack_tests.get_pub_net_config(
+ project_name=self.os_creds.project_name,
net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet',
external_net=self.ext_net_name)
@@ -185,10 +187,11 @@ class NeutronUtilsSubnetTests(OSComponentTestCase):
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.assertTrue(validate_subnet(
- self.neutron, subnet_setting.name, subnet_setting.cidr, True))
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
subnet_query1 = neutron_utils.get_subnet(
- self.neutron, subnet_name=subnet_setting.name)
+ self.neutron, self.network, subnet_name=subnet_setting.name)
self.assertEqual(self.network.subnets[0], subnet_query1)
subnet_query2 = neutron_utils.get_subnets_by_network(self.neutron,
@@ -197,6 +200,12 @@ class NeutronUtilsSubnetTests(OSComponentTestCase):
self.assertEqual(1, len(subnet_query2))
self.assertEqual(self.network.subnets[0], subnet_query2[0])
+ subnet_query3 = neutron_utils.get_subnet_by_name(
+ self.neutron, self.keystone, subnet_setting.name,
+ self.os_creds.project_name)
+ self.assertIsNotNone(subnet_query3)
+ self.assertEqual(self.network.subnets[0], subnet_query3)
+
def test_create_subnet_null_name(self):
"""
Tests the neutron_utils.create_neutron_subnet() function for an
@@ -230,16 +239,17 @@ class NeutronUtilsSubnetTests(OSComponentTestCase):
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.assertTrue(validate_subnet(
- self.neutron, subnet_setting.name, subnet_setting.cidr, True))
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
self.assertFalse(validate_subnet(
- self.neutron, '', subnet_setting.cidr, True))
+ self.neutron, self.network, '', subnet_setting.cidr, True))
subnet_query1 = neutron_utils.get_subnet(
- self.neutron, subnet_name=subnet_setting.name)
+ self.neutron, self.network, subnet_name=subnet_setting.name)
self.assertEqual(self.network.subnets[0], subnet_query1)
- subnet_query2 = neutron_utils.get_subnets_by_network(self.neutron,
- self.network)
+ subnet_query2 = neutron_utils.get_subnets_by_network(
+ self.neutron, self.network)
self.assertIsNotNone(subnet_query2)
self.assertEqual(1, len(subnet_query2))
self.assertEqual(self.network.subnets[0], subnet_query2[0])
@@ -517,6 +527,7 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
self.router = None
self.interface_router = None
self.net_config = openstack_tests.get_pub_net_config(
+ project_name=self.os_creds.project_name,
net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet',
router_name=guid + '-pub-router', external_net=self.ext_net_name)
@@ -564,9 +575,9 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
"""
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.net_config = openstack_tests.OSNetworkConfig(
- self.net_config.network_settings.name, subnet_setting.name,
- subnet_setting.cidr, self.net_config.router_settings.name,
- self.ext_net_name)
+ self.os_creds.project_name, self.net_config.network_settings.name,
+ subnet_setting.name, subnet_setting.cidr,
+ self.net_config.router_settings.name, self.ext_net_name)
self.router = neutron_utils.create_router(
self.neutron, self.os_creds, self.net_config.router_settings)
validate_router(
@@ -592,7 +603,8 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.assertTrue(validate_subnet(
- self.neutron, subnet_setting.name, subnet_setting.cidr, True))
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
self.router = neutron_utils.create_router(
self.neutron, self.os_creds, self.net_config.router_settings)
@@ -621,7 +633,8 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.assertTrue(validate_subnet(
- self.neutron, subnet_setting.name, subnet_setting.cidr, True))
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
with self.assertRaises(NeutronException):
self.interface_router = neutron_utils.add_interface_router(
@@ -693,7 +706,8 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.assertTrue(validate_subnet(
- self.neutron, subnet_setting.name, subnet_setting.cidr, True))
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
self.port = neutron_utils.create_port(
self.neutron, self.os_creds, PortConfig(
@@ -718,8 +732,9 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
self.os_creds.project_name))
subnet_setting = self.net_config.network_settings.subnet_settings[0]
- self.assertTrue(validate_subnet(self.neutron, subnet_setting.name,
- subnet_setting.cidr, True))
+ self.assertTrue(validate_subnet(
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
self.port = neutron_utils.create_port(
self.neutron, self.os_creds, PortConfig(
@@ -745,7 +760,8 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.assertTrue(validate_subnet(
- self.neutron, subnet_setting.name, subnet_setting.cidr, True))
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
self.port = neutron_utils.create_port(
self.neutron, self.os_creds,
@@ -791,7 +807,8 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.assertTrue(validate_subnet(
- self.neutron, subnet_setting.name, subnet_setting.cidr, True))
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
with self.assertRaises(Exception):
self.port = neutron_utils.create_port(
@@ -819,7 +836,8 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.assertTrue(validate_subnet(
- self.neutron, subnet_setting.name, subnet_setting.cidr, True))
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
with self.assertRaises(Exception):
self.port = neutron_utils.create_port(
@@ -847,7 +865,8 @@ class NeutronUtilsRouterTests(OSComponentTestCase):
subnet_setting = self.net_config.network_settings.subnet_settings[0]
self.assertTrue(validate_subnet(
- self.neutron, subnet_setting.name, subnet_setting.cidr, True))
+ self.neutron, self.network, subnet_setting.name,
+ subnet_setting.cidr, True))
with self.assertRaises(Exception):
self.port = neutron_utils.create_port(
@@ -1110,18 +1129,20 @@ def validate_network(neutron, keystone, name, exists, project_name):
return False
-def validate_subnet(neutron, name, cidr, exists):
+def validate_subnet(neutron, network, name, cidr, exists):
"""
Returns true if a subnet for a given name DOES NOT exist if the exists
parameter is false conversely true. Returns false if a subnet for a given
name DOES exist if the exists parameter is true conversely false.
:param neutron: The neutron client
+ :param network: The SNAPS-OO Network domain object
:param name: The expected subnet name
:param cidr: The expected CIDR value
:param exists: Whether or not the network name should exist or not
:return: True/False
"""
- subnet = neutron_utils.get_subnet(neutron, subnet_name=name)
+ subnet = neutron_utils.get_subnet(
+ neutron, network, subnet_name=name)
if exists and subnet and subnet.name == name:
return subnet.cidr == cidr
if not exists and not subnet:
diff --git a/snaps/openstack/utils/tests/nova_utils_tests.py b/snaps/openstack/utils/tests/nova_utils_tests.py
index 5c2f0ba..1f60b9b 100644
--- a/snaps/openstack/utils/tests/nova_utils_tests.py
+++ b/snaps/openstack/utils/tests/nova_utils_tests.py
@@ -269,7 +269,8 @@ class NovaUtilsInstanceTests(OSComponentTestCase):
self.image_creator.create()
network_settings = openstack_tests.get_priv_net_config(
- guid + '-net', guid + '-subnet').network_settings
+ self.os_creds.project_name, guid + '-net',
+ guid + '-subnet').network_settings
self.network_creator = OpenStackNetwork(
self.os_creds, network_settings)
self.network_creator.create()
@@ -393,7 +394,8 @@ class NovaUtilsInstanceVolumeTests(OSComponentTestCase):
self.image_creator.create()
network_settings = openstack_tests.get_priv_net_config(
- guid + '-net', guid + '-subnet').network_settings
+ self.os_creds.project_name, guid + '-net',
+ guid + '-subnet').network_settings
self.network_creator = OpenStackNetwork(
self.os_creds, network_settings)
self.network_creator.create()
diff --git a/snaps/openstack/utils/tests/settings_utils_tests.py b/snaps/openstack/utils/tests/settings_utils_tests.py
index 14af990..3d080d4 100644
--- a/snaps/openstack/utils/tests/settings_utils_tests.py
+++ b/snaps/openstack/utils/tests/settings_utils_tests.py
@@ -196,6 +196,7 @@ class SettingsUtilsVmInstTests(OSComponentTestCase):
# First network is public
self.pub_net_config = openstack_tests.get_pub_net_config(
+ project_name=self.os_creds.project_name,
net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet',
router_name=guid + '-pub-router',
external_net=self.ext_net_name)