From 652da5487159d5ee94fe2f8e62c34fdb0d26f4e5 Mon Sep 17 00:00:00 2001 From: spisarski Date: Wed, 2 Aug 2017 13:19:58 -0600 Subject: Refactor network retrieval API calls. Refactored neutron_utils#get_network() to also accept a NetworkSettings object for more robust queries in addition to the old network_name parameter. Also refactored neutron_utils# get_network_by_id to add in the ID to list_networks((**{'id': id) and returning the first item contains the expected ID value. JIRA: SNAPS-161 Change-Id: Ie670a442dd70633bbef7a1233e630672ebac6b0c Signed-off-by: spisarski --- snaps/openstack/create_network.py | 9 +++---- snaps/openstack/create_router.py | 5 ++-- snaps/openstack/tests/create_network_tests.py | 6 ++--- snaps/openstack/utils/neutron_utils.py | 31 ++++++++++++---------- snaps/openstack/utils/tests/neutron_utils_tests.py | 5 ++-- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/snaps/openstack/create_network.py b/snaps/openstack/create_network.py index 2f26c43..4caef12 100644 --- a/snaps/openstack/create_network.py +++ b/snaps/openstack/create_network.py @@ -53,8 +53,8 @@ class OpenStackNetwork: logger.info( 'Creating neutron network %s...' % self.network_settings.name) net_inst = neutron_utils.get_network( - self.__neutron, self.network_settings.name, - self.network_settings.get_project_id(self.__os_creds)) + self.__neutron, network_settings=self.network_settings, + project_id=self.network_settings.get_project_id(self.__os_creds)) if net_inst: self.__network = net_inst else: @@ -498,9 +498,8 @@ class PortSettings: project_id = project.id if not self.network: - self.network = neutron_utils.get_network(neutron, - self.network_name, - project_id) + self.network = neutron_utils.get_network( + neutron, network_name=self.network_name, project_id=project_id) if not self.network: raise PortSettingsError( 'Cannot locate network with name - ' + self.network_name) diff --git a/snaps/openstack/create_router.py b/snaps/openstack/create_router.py index 209f9d2..3e0d904 100644 --- a/snaps/openstack/create_router.py +++ b/snaps/openstack/create_router.py @@ -233,8 +233,6 @@ class RouterSettings: out = dict() ext_gw = dict() - project_id = None - if self.name: out['name'] = self.name if self.project_name: @@ -253,7 +251,8 @@ class RouterSettings: if self.admin_state_up is not None: out['admin_state_up'] = self.admin_state_up if self.external_gateway: - ext_net = neutron_utils.get_network(neutron, self.external_gateway) + ext_net = neutron_utils.get_network( + neutron, network_name=self.external_gateway) if ext_net: ext_gw['network_id'] = ext_net.id out['external_gateway_info'] = ext_gw diff --git a/snaps/openstack/tests/create_network_tests.py b/snaps/openstack/tests/create_network_tests.py index e941c67..936eda9 100644 --- a/snaps/openstack/tests/create_network_tests.py +++ b/snaps/openstack/tests/create_network_tests.py @@ -409,7 +409,7 @@ class CreateNetworkSuccessTests(OSIntegrationTestCase): neutron_utils.delete_network(self.neutron, self.net_creator.get_network()) self.assertIsNone(neutron_utils.get_network( - self.neutron, self.net_creator.network_settings.name)) + self.neutron, network_settings=self.net_creator.network_settings)) # This shall not throw an exception here self.net_creator.clean() @@ -479,7 +479,7 @@ class CreateNetworkSuccessTests(OSIntegrationTestCase): self.net_creator.create() retrieved_net = neutron_utils.get_network( - self.neutron, self.net_config.network_settings.name) + self.neutron, network_settings=self.net_config.network_settings) self.assertEqual(self.net_creator.get_network().id, retrieved_net.id) @@ -509,7 +509,7 @@ class CreateNetworkSuccessTests(OSIntegrationTestCase): self.net_creator.create() retrieved_net = neutron_utils.get_network( - self.neutron, self.net_config.network_settings.name) + self.neutron, network_settings=self.net_config.network_settings) self.assertEqual(self.net_creator.get_network().id, retrieved_net.id) diff --git a/snaps/openstack/utils/neutron_utils.py b/snaps/openstack/utils/neutron_utils.py index 19325a8..061bc56 100644 --- a/snaps/openstack/utils/neutron_utils.py +++ b/snaps/openstack/utils/neutron_utils.py @@ -74,42 +74,45 @@ def delete_network(neutron, network): neutron.delete_network(network.id) -def get_network(neutron, network_name, project_id=None): +def get_network(neutron, network_settings=None, network_name=None, + project_id=None): """ - Returns an object (dictionary) of the first network found with a given name - and project_id (if included) + Returns Network SNAPS-OO domain object the first network found with + either the given attributes from the network_settings object if not None, + else the query will use just the name from the network_name parameter. + When the project_id is included, that will be added to the query filter. :param neutron: the client + :param network_settings: the NetworkSettings object used to create filter :param network_name: the name of the network to retrieve :param project_id: the id of the network's project :return: a SNAPS-OO Network domain object """ net_filter = dict() - if network_name: + if network_settings: + net_filter['name'] = network_settings.name + elif network_name: net_filter['name'] = network_name + if project_id: net_filter['project_id'] = project_id networks = neutron.list_networks(**net_filter) for network, netInsts in networks.items(): for inst in netInsts: - if inst.get('name') == network_name: - return Network(**inst) - return None + return Network(**inst) def get_network_by_id(neutron, network_id): """ - Returns the network object (dictionary) with the given ID + Returns the network object (dictionary) with the given ID else None :param neutron: the client :param network_id: the id of the network to retrieve :return: a SNAPS-OO Network domain object """ networks = neutron.list_networks(**{'id': network_id}) - for network, netInsts in networks.items(): - for inst in netInsts: - if inst.get('id') == network_id: - return Network(**inst) - return None + for network in networks['networks']: + if network['id'] == network_id: + return Network(**network) def create_subnet(neutron, subnet_settings, os_creds, network=None): @@ -486,7 +489,7 @@ def create_floating_ip(neutron, ext_net_name): :return: the SNAPS FloatingIp object """ logger.info('Creating floating ip to external network - ' + ext_net_name) - ext_net = get_network(neutron, ext_net_name) + ext_net = get_network(neutron, network_name=ext_net_name) if ext_net: fip = neutron.create_floatingip( body={'floatingip': diff --git a/snaps/openstack/utils/tests/neutron_utils_tests.py b/snaps/openstack/utils/tests/neutron_utils_tests.py index 82bb42e..0b9559a 100644 --- a/snaps/openstack/utils/tests/neutron_utils_tests.py +++ b/snaps/openstack/utils/tests/neutron_utils_tests.py @@ -319,7 +319,8 @@ class NeutronUtilsRouterTests(OSComponentTestCase): validate_router(self.neutron, self.net_config.router_settings.name, True) - ext_net = neutron_utils.get_network(self.neutron, self.ext_net_name) + ext_net = neutron_utils.get_network( + self.neutron, network_name=self.ext_net_name) self.assertEqual( self.router.external_gateway_info['network_id'], ext_net.id) @@ -805,7 +806,7 @@ def validate_network(neutron, name, exists): :param exists: Whether or not the network name should exist or not :return: True/False """ - network = neutron_utils.get_network(neutron, name) + network = neutron_utils.get_network(neutron, network_name=name) if exists and network: return True if not exists and not network: -- cgit 1.2.3-korg