From f65dbaef830fe7121173fdb83e5e4dde09b11a8a Mon Sep 17 00:00:00 2001 From: spisarski Date: Tue, 17 Apr 2018 08:24:18 -0600 Subject: Fixed bug with regards to subnet lookups. Neutron returns all subnets regardless of visibility which cause problems within routers if there is another subnet with the same name attached to a different network. JIRA: SNAPS-304 In addition, this patch contains two other minor fixes. launch_utils.py - raise an exception when the creator is not properly instantiated network.py - allow fixed IPs to be none. Change-Id: Ib343074d925be4592a713727a03d5b531890eada Signed-off-by: spisarski --- .../complex-network/deploy-complex-network.yaml | 20 +++++-- examples/external-network/deploy-ext-net.yaml | 5 +- examples/inst-w-volume/deploy-vm-with-volume.yaml | 5 +- examples/simple/deploy-simple.yaml | 5 +- examples/two-network/deploy-two-net-centos.yaml | 5 +- examples/two-network/deploy-two-net-ubuntu.yaml | 5 +- snaps/config/network.py | 29 +++++----- snaps/config/router.py | 19 ++++++- snaps/config/tests/router_tests.py | 54 +++++++++++++++++-- snaps/openstack/create_instance.py | 13 +++-- snaps/openstack/create_router.py | 37 +++++++++---- snaps/openstack/tests/create_instance_tests.py | 20 ++++++- snaps/openstack/tests/create_network_tests.py | 17 +++--- snaps/openstack/tests/create_stack_tests.py | 2 +- snaps/openstack/tests/openstack_tests.py | 27 +++++----- snaps/openstack/utils/launch_utils.py | 2 + snaps/openstack/utils/neutron_utils.py | 39 +++++++++++--- snaps/openstack/utils/tests/neutron_utils_tests.py | 63 ++++++++++++++-------- snaps/openstack/utils/tests/nova_utils_tests.py | 6 ++- .../openstack/utils/tests/settings_utils_tests.py | 1 + snaps/provisioning/tests/ansible_utils_tests.py | 1 + 21 files changed, 282 insertions(+), 93 deletions(-) diff --git a/examples/complex-network/deploy-complex-network.yaml b/examples/complex-network/deploy-complex-network.yaml index 7915c38..be38794 100644 --- a/examples/complex-network/deploy-complex-network.yaml +++ b/examples/complex-network/deploy-complex-network.yaml @@ -70,8 +70,14 @@ openstack: name: mgr-router external_gateway: external internal_subnets: - - mgr-subnet - - mgr-subnet-2 + - subnet: + project_name: admin + network_name: mgr-net + subnet_name: mgr-subnet + - subnet: + project_name: admin + network_name: mgr-net + subnet_name: mgr-subnet-2 interfaces: - port: name: mgr-router-to-site1 @@ -83,12 +89,18 @@ openstack: name: site1-router external_gateway: external internal_subnets: - - site1-subnet + - subnet: + project_name: admin + network_name: site1-net + subnet_name: site1-subnet - router: name: site2-router external_gateway: external internal_subnets: - - site2-subnet + - subnet: + project_name: admin + network_name: site2-net + subnet_name: site2-subnet - router: name: site-to-site-router interfaces: diff --git a/examples/external-network/deploy-ext-net.yaml b/examples/external-network/deploy-ext-net.yaml index ac5e214..b5f00b1 100644 --- a/examples/external-network/deploy-ext-net.yaml +++ b/examples/external-network/deploy-ext-net.yaml @@ -52,7 +52,10 @@ openstack: name: ext-net-router external_gateway: ext-net internal_subnets: - - internal-subnet + - subnet: + project_name: admin + network_name: internal-net + subnet_name: internal-subnet keypairs: - keypair: name: ext-net-kp diff --git a/examples/inst-w-volume/deploy-vm-with-volume.yaml b/examples/inst-w-volume/deploy-vm-with-volume.yaml index 30dbcc0..b15f655 100644 --- a/examples/inst-w-volume/deploy-vm-with-volume.yaml +++ b/examples/inst-w-volume/deploy-vm-with-volume.yaml @@ -95,7 +95,10 @@ openstack: name: {{ router_name }} external_gateway: {{ ext_net }} internal_subnets: - - {{ subnet_name }} + - subnet: + project_name: {{ admin_proj }} + network_name: {{ net_name }} + subnet_name: {{ subnet_name }} keypairs: - keypair: os_user: diff --git a/examples/simple/deploy-simple.yaml b/examples/simple/deploy-simple.yaml index 3124210..fa4c8b2 100644 --- a/examples/simple/deploy-simple.yaml +++ b/examples/simple/deploy-simple.yaml @@ -41,7 +41,10 @@ openstack: name: simple-router external_gateway: external internal_subnets: - - simple-subnet + - subnet: + project_name: admin + network_name: simple-net + subnet_name: simple-subnet keypairs: - keypair: name: simple-kp diff --git a/examples/two-network/deploy-two-net-centos.yaml b/examples/two-network/deploy-two-net-centos.yaml index 4b5ba89..d23fff9 100644 --- a/examples/two-network/deploy-two-net-centos.yaml +++ b/examples/two-network/deploy-two-net-centos.yaml @@ -48,7 +48,10 @@ openstack: name: router-1 external_gateway: external internal_subnets: - - subnet-1 + - subnet: + project_name: admin + network_name: net-1 + subnet_name: subnet-1 keypairs: - keypair: name: two-net diff --git a/examples/two-network/deploy-two-net-ubuntu.yaml b/examples/two-network/deploy-two-net-ubuntu.yaml index 0ad471c..0381bff 100644 --- a/examples/two-network/deploy-two-net-ubuntu.yaml +++ b/examples/two-network/deploy-two-net-ubuntu.yaml @@ -48,7 +48,10 @@ openstack: name: router-1 external_gateway: external internal_subnets: - - subnet-1 + - subnet: + project_name: admin + network_name: net-1 + subnet_name: subnet-1 keypairs: - keypair: name: simple diff --git a/snaps/config/network.py b/snaps/config/network.py index 8148c61..eca8734 100644 --- a/snaps/config/network.py +++ b/snaps/config/network.py @@ -412,22 +412,23 @@ class PortConfig(object): raise PortConfigError( 'The attribute network_name is required') - def __get_fixed_ips(self, neutron): + def __get_fixed_ips(self, neutron, network): """ Sets the self.fixed_ips value :param neutron: the Neutron client + :param network: the SNAPS-OO network domain object :return: None """ - fixed_ips = list() if self.ip_addrs: for ip_addr_dict in self.ip_addrs: subnet = neutron_utils.get_subnet( - neutron, subnet_name=ip_addr_dict['subnet_name']) - if subnet and 'ip' in ip_addr_dict: - fixed_ips.append({'ip_address': ip_addr_dict['ip'], - 'subnet_id': subnet.id}) + neutron, network, subnet_name=ip_addr_dict['subnet_name']) + if subnet: + if 'ip' in ip_addr_dict: + fixed_ips.append({'ip_address': ip_addr_dict['ip'], + 'subnet_id': subnet.id}) else: raise PortConfigError( 'Invalid port configuration, subnet does not exist ' @@ -443,25 +444,29 @@ class PortConfig(object): TODO - expand automated testing to exercise all parameters :param neutron: the Neutron client + :param keystone: the Keystone client :param os_creds: the OpenStack credentials :return: the dictionary object """ - out = dict() - session = keystone_utils.keystone_session(os_creds) keystone = keystone_utils.keystone_client(os_creds, session) + + project_name = os_creds.project_name + if self.project_name: + project_name = project_name try: network = neutron_utils.get_network( neutron, keystone, network_name=self.network_name, - project_name=self.project_name) + project_name=project_name) finally: - keystone_utils.close_session(session) + if session: + keystone_utils.close_session(session) if not network: raise PortConfigError( 'Cannot locate network with name - ' + self.network_name - + ' in project - ' + str(self.project_name)) + + ' in project - ' + str(project_name)) out['network_id'] = network.id @@ -484,7 +489,7 @@ class PortConfig(object): if self.mac_address: out['mac_address'] = self.mac_address - fixed_ips = self.__get_fixed_ips(neutron) + fixed_ips = self.__get_fixed_ips(neutron, network) if fixed_ips and len(fixed_ips) > 0: out['fixed_ips'] = fixed_ips diff --git a/snaps/config/router.py b/snaps/config/router.py index 6b03ad2..2a0b6a4 100644 --- a/snaps/config/router.py +++ b/snaps/config/router.py @@ -33,7 +33,11 @@ class RouterConfig(object): :param admin_state_up: The administrative status of the router. True = up / False = down (default True) :param internal_subnets: List of subnet names to which to connect this - router for Floating IP purposes + router (this way is deprecated). + *** NEW WAY *** + List of dict where the key is 'subnet' that + contains members with the following keys: + project_name, network_name, and subnet_name :param port_settings: List of PortConfig objects :return: """ @@ -45,6 +49,19 @@ class RouterConfig(object): self.enable_snat = kwargs.get('enable_snat') if kwargs.get('internal_subnets'): self.internal_subnets = kwargs['internal_subnets'] + if isinstance(self.internal_subnets, dict): + if 'subnet' not in self.internal_subnets: + raise RouterConfigError( + 'subnet is a required key to internal_subnets') + if 'project_name' not in self.internal_subnets['subnet']: + raise RouterConfigError( + 'subnet.project is a required key to subnet') + if 'network_name' not in self.internal_subnets['subnet']: + raise RouterConfigError( + 'network_name is a required key to subnet') + if 'subnet_name' not in self.internal_subnets['subnet']: + raise RouterConfigError( + 'subnet_name is a required key to subnet') else: self.internal_subnets = list() diff --git a/snaps/config/tests/router_tests.py b/snaps/config/tests/router_tests.py index 2c8f91f..1397f23 100644 --- a/snaps/config/tests/router_tests.py +++ b/snaps/config/tests/router_tests.py @@ -31,6 +31,25 @@ class RouterConfigUnitTests(unittest.TestCase): with self.assertRaises(RouterConfigError): RouterConfig(**dict()) + def test_bad_internal_subnets_bad_key(self): + with self.assertRaises(RouterConfigError): + RouterConfig(name='foo', internal_subnets={'foo': 'bar'}) + + def test_bad_internal_subnets_no_project(self): + with self.assertRaises(RouterConfigError): + RouterConfig(name='foo', internal_subnets={ + 'subnet': {'subnet_name': 'bar', 'network_name': 'foo'}}) + + def test_bad_internal_subnets_no_network(self): + with self.assertRaises(RouterConfigError): + RouterConfig(name='foo', internal_subnets={ + 'subnet': {'subnet_name': 'bar', 'project_name': 'foo'}}) + + def test_bad_internal_subnets_no_subnet(self): + with self.assertRaises(RouterConfigError): + RouterConfig(name='foo', internal_subnets={ + 'subnet': {'project_name': 'bar', 'network_name': 'foo'}}) + def test_name_only(self): settings = RouterConfig(name='foo') self.assertEqual('foo', settings.name) @@ -59,7 +78,7 @@ class RouterConfigUnitTests(unittest.TestCase): self.assertTrue(isinstance(settings.port_settings, list)) self.assertEqual(0, len(settings.port_settings)) - def test_all(self): + def test_all_internal_subnets_str(self): port_settings = PortConfig(name='foo', network_name='bar') settings = RouterConfig( name='foo', project_name='bar', external_gateway='foo_gateway', @@ -76,11 +95,36 @@ class RouterConfigUnitTests(unittest.TestCase): self.assertEqual(['10.0.0.1/24'], settings.internal_subnets) self.assertEqual([port_settings], settings.port_settings) - def test_config_all(self): + def test_all_internal_subnets_dict(self): + port_settings = PortConfig(name='foo', network_name='bar') + int_subs = {'subnet': { + 'project_name': 'proj_a', 'network_name': 'net_name', + 'subnet_name': 'sub_name'}} + settings = RouterConfig( + name='foo', project_name='bar', external_gateway='foo_gateway', + admin_state_up=True, enable_snat=False, + internal_subnets=int_subs, + interfaces=[port_settings]) + self.assertEqual('foo', settings.name) + self.assertEqual('bar', settings.project_name) + self.assertEqual('foo_gateway', settings.external_gateway) + self.assertTrue(settings.admin_state_up) + self.assertFalse(settings.enable_snat) + self.assertIsNotNone(settings.internal_subnets) + self.assertTrue(isinstance(settings.internal_subnets, dict)) + self.assertEqual(1, len(settings.internal_subnets)) + self.assertEqual(int_subs, settings.internal_subnets) + self.assertEqual([port_settings], settings.port_settings) + + def test_config_all_internal_subnets_str(self): + int_subs = {'subnet': { + 'project_name': 'proj_a', 'network_name': 'net_name', + 'subnet_name': 'sub_name'}} settings = RouterConfig( **{'name': 'foo', 'project_name': 'bar', 'external_gateway': 'foo_gateway', 'admin_state_up': True, - 'enable_snat': False, 'internal_subnets': ['10.0.0.1/24'], + 'enable_snat': False, + 'internal_subnets': int_subs, 'interfaces': [{'port': {'name': 'foo-port', 'network_name': 'bar-net'}}]}) @@ -90,9 +134,9 @@ class RouterConfigUnitTests(unittest.TestCase): self.assertTrue(settings.admin_state_up) self.assertFalse(settings.enable_snat) self.assertIsNotNone(settings.internal_subnets) - self.assertTrue(isinstance(settings.internal_subnets, list)) + self.assertTrue(isinstance(settings.internal_subnets, dict)) self.assertEqual(1, len(settings.internal_subnets)) - self.assertEqual(['10.0.0.1/24'], settings.internal_subnets) + self.assertEqual(int_subs, settings.internal_subnets) self.assertEqual([PortConfig(**{'name': 'foo-port', 'network_name': 'bar-net'})], settings.port_settings) diff --git a/snaps/openstack/create_instance.py b/snaps/openstack/create_instance.py index 45666d5..16bd0ce 100644 --- a/snaps/openstack/create_instance.py +++ b/snaps/openstack/create_instance.py @@ -96,7 +96,7 @@ class OpenStackVmInstance(OpenStackComputeObject): """ self.initialize() - if len(self.__ports) == 0: + if len(self.__ports) != len(self.instance_settings.port_settings): self.__ports = self.__create_ports( self.instance_settings.port_settings) if not self.__vm: @@ -346,12 +346,13 @@ class OpenStackVmInstance(OpenStackComputeObject): for port_setting in port_settings: port = neutron_utils.get_port( - self.__neutron, self.__keystone, port_settings=port_setting) + self.__neutron, self.__keystone, port_settings=port_setting, + project_name=self._os_creds.project_name) if not port: port = neutron_utils.create_port( self.__neutron, self._os_creds, port_setting) - if port: - ports.append((port_setting.name, port)) + if port: + ports.append((port_setting.name, port)) return ports @@ -390,8 +391,10 @@ class OpenStackVmInstance(OpenStackComputeObject): port = self.get_port_by_name(port_name) if port: if subnet_name: + network = neutron_utils.get_network_by_id( + self.__neutron, port.network_id) subnet = neutron_utils.get_subnet( - self.__neutron, subnet_name=subnet_name) + self.__neutron, network, subnet_name=subnet_name) if not subnet: logger.warning('Cannot retrieve port IP as subnet could ' 'not be located with name - %s', diff --git a/snaps/openstack/create_router.py b/snaps/openstack/create_router.py index 4395059..260fc03 100644 --- a/snaps/openstack/create_router.py +++ b/snaps/openstack/create_router.py @@ -18,7 +18,7 @@ from neutronclient.common.exceptions import NotFound, Unauthorized from snaps.config.router import RouterConfig from snaps.openstack.openstack_creator import OpenStackNetworkObject -from snaps.openstack.utils import neutron_utils, keystone_utils +from snaps.openstack.utils import neutron_utils __author__ = 'spisarski' @@ -71,14 +71,13 @@ class OpenStackRouter(OpenStackNetworkObject): self.router_settings.name, e) if self.__router: - for internal_subnet_name in self.router_settings.internal_subnets: - internal_subnet = neutron_utils.get_subnet( - self._neutron, subnet_name=internal_subnet_name) + for sub_config in self.router_settings.internal_subnets: + internal_subnet = self.__get_internal_subnet(sub_config) if internal_subnet: self.__internal_subnets.append(internal_subnet) else: raise RouterCreationError( - 'Subnet not found with name ' + internal_subnet_name) + 'Subnet not found with name ' + internal_subnet.name) for port_setting in self.router_settings.port_settings: port = neutron_utils.get_port( @@ -100,9 +99,8 @@ class OpenStackRouter(OpenStackNetworkObject): self.__router = neutron_utils.create_router( self._neutron, self._os_creds, self.router_settings) - for internal_subnet_name in self.router_settings.internal_subnets: - internal_subnet = neutron_utils.get_subnet( - self._neutron, subnet_name=internal_subnet_name) + for sub_config in self.router_settings.internal_subnets: + internal_subnet = self.__get_internal_subnet(sub_config) if internal_subnet: self.__internal_subnets.append(internal_subnet) if internal_subnet: @@ -113,7 +111,7 @@ class OpenStackRouter(OpenStackNetworkObject): self.__internal_router_interface = router_intf else: raise RouterCreationError( - 'Subnet not found with name ' + internal_subnet_name) + 'Subnet not found with name ' + internal_subnet.name) for port_setting in self.router_settings.port_settings: port = neutron_utils.get_port( @@ -145,6 +143,27 @@ class OpenStackRouter(OpenStackNetworkObject): self._neutron, self.__router.id) return self.__router + def __get_internal_subnet(self, sub_config): + """ + returns the Subnet domain object from the subnet configurator + :param sub_config: + :return: + """ + if isinstance(sub_config, str): + return neutron_utils.get_subnet_by_name( + self._neutron, self._keystone, + subnet_name=sub_config) + if isinstance(sub_config, dict): + sub_dict = sub_config['subnet'] + network = neutron_utils.get_network( + self._neutron, self._keystone, + network_name=sub_dict['network_name'], + project_name=sub_dict['project_name']) + if network: + return neutron_utils.get_subnet( + self._neutron, network, + subnet_name=sub_dict['subnet_name']) + def clean(self): """ Removes and deletes all items created in reverse order. diff --git a/snaps/openstack/tests/create_instance_tests.py b/snaps/openstack/tests/create_instance_tests.py index ce58a01..0ef155b 100644 --- a/snaps/openstack/tests/create_instance_tests.py +++ b/snaps/openstack/tests/create_instance_tests.py @@ -306,6 +306,7 @@ class SimpleHealthCheck(OSIntegrationTestCase): self.inst_creator = None self.priv_net_config = openstack_tests.get_priv_net_config( + project_name=self.os_creds.project_name, net_name=guid + '-priv-net', subnet_name=guid + '-priv-subnet', netconf_override=self.netconf_override) @@ -490,6 +491,7 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): """ # Create Network net_config = openstack_tests.get_priv_net_config( + project_name=self.os_creds.project_name, net_name=self.guid + '-pub-net', subnet_name=self.guid + '-pub-subnet', router_name=self.guid + '-pub-router', @@ -539,6 +541,7 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): """ # Create Network net_config = openstack_tests.get_priv_net_config( + project_name=self.os_creds.project_name, net_name=self.guid + '-pub-net', subnet_name=self.guid + '-pub-subnet', router_name=self.guid + '-pub-router', @@ -711,6 +714,7 @@ class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): self.inst_creators = list() 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, netconf_override=self.netconf_override) @@ -1245,7 +1249,10 @@ class CreateInstanceIPv6NetworkTests(OSIntegrationTestCase): name=self.guid + '-net', subnet_settings=[subnet_settings]) router_settings = RouterConfig( name=self.guid + '-router', external_gateway=self.ext_net_name, - internal_subnets=[subnet_settings.name]) + internal_subnets=[{'subnet': { + 'project_name': self.os_creds.project_name, + 'network_name': network_settings.name, + 'subnet_name': subnet_settings.name}}]) # Create Network self.network_creator = OpenStackNetwork( @@ -1293,7 +1300,10 @@ class CreateInstanceIPv6NetworkTests(OSIntegrationTestCase): subnet_settings=[subnet4_settings, subnet6_settings]) router_settings = RouterConfig( name=self.guid + '-router', external_gateway=self.ext_net_name, - internal_subnets=[subnet4_settings.name]) + internal_subnets=[{'subnet': { + 'project_name': self.os_creds.project_name, + 'network_name': network_settings.name, + 'subnet_name': subnet4_settings.name}}]) # Create Network self.network_creator = OpenStackNetwork( @@ -1354,6 +1364,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): self.inst_creator = None self.net_config = openstack_tests.get_priv_net_config( + project_name=self.os_creds.project_name, net_name=self.guid + '-pub-net', subnet_name=self.guid + '-pub-subnet', router_name=self.guid + '-pub-router', @@ -1735,6 +1746,7 @@ class CreateInstanceOnComputeHost(OSIntegrationTestCase): self.inst_creators = list() self.priv_net_config = openstack_tests.get_priv_net_config( + project_name=self.os_creds.project_name, net_name=guid + '-priv-net', subnet_name=guid + '-priv-subnet', netconf_override=self.netconf_override) @@ -1870,6 +1882,7 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): self.floating_ip_name = self.guid + 'fip1' net_config = openstack_tests.get_priv_net_config( + project_name=self.os_creds.project_name, net_name=self.guid + '-pub-net', subnet_name=self.guid + '-pub-subnet', router_name=self.guid + '-pub-router', @@ -2194,6 +2207,7 @@ class CreateInstanceFromThreePartImage(OSIntegrationTestCase): self.nova = nova_utils.nova_client(self.os_creds, self.os_session) net_config = openstack_tests.get_priv_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, netconf_override=self.netconf_override) @@ -2336,6 +2350,7 @@ class CreateInstanceMockOfflineTests(OSComponentTestCase): self.inst_creator = None self.priv_net_config = openstack_tests.get_priv_net_config( + project_name=self.os_creds.project_name, net_name=self.guid + '-priv-net', subnet_name=self.guid + '-priv-subnet') self.port_settings = PortConfig( @@ -3058,6 +3073,7 @@ class CreateInstanceVolumeTests(OSIntegrationTestCase): name=guid + '-image', image_metadata=self.image_metadata) net_config = openstack_tests.get_priv_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, netconf_override=self.netconf_override) diff --git a/snaps/openstack/tests/create_network_tests.py b/snaps/openstack/tests/create_network_tests.py index d326889..5ba5cef 100644 --- a/snaps/openstack/tests/create_network_tests.py +++ b/snaps/openstack/tests/create_network_tests.py @@ -365,6 +365,7 @@ class CreateNetworkSuccessTests(OSIntegrationTestCase): guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) 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, netconf_override=self.netconf_override) @@ -395,7 +396,7 @@ class CreateNetworkSuccessTests(OSIntegrationTestCase): # Create Network self.net_creator = OpenStackNetwork( self.os_creds, self.net_config.network_settings) - self.net_creator.create() + network = self.net_creator.create() # Validate network was created self.assertTrue(neutron_utils_tests.validate_network( @@ -405,7 +406,7 @@ class CreateNetworkSuccessTests(OSIntegrationTestCase): # Validate subnets self.assertTrue(neutron_utils_tests.validate_subnet( - self.neutron, + self.neutron, network, self.net_creator.network_settings.subnet_settings[0].name, self.net_creator.network_settings.subnet_settings[0].cidr, True)) @@ -441,7 +442,7 @@ class CreateNetworkSuccessTests(OSIntegrationTestCase): # Create Network self.net_creator = OpenStackNetwork( self.os_creds, self.net_config.network_settings) - self.net_creator.create() + network = self.net_creator.create() # Create Router self.router_creator = create_router.OpenStackRouter( @@ -456,7 +457,7 @@ class CreateNetworkSuccessTests(OSIntegrationTestCase): # Validate subnets self.assertTrue(neutron_utils_tests.validate_subnet( - self.neutron, + self.neutron, network, self.net_creator.network_settings.subnet_settings[0].name, self.net_creator.network_settings.subnet_settings[0].cidr, True)) @@ -616,7 +617,7 @@ class CreateNetworkGatewayTests(OSIntegrationTestCase): # Validate subnets self.assertTrue(neutron_utils_tests.validate_subnet( - self.neutron, + self.neutron, out_net, self.net_creator.network_settings.subnet_settings[0].name, self.net_creator.network_settings.subnet_settings[0].cidr, True)) @@ -645,7 +646,7 @@ class CreateNetworkGatewayTests(OSIntegrationTestCase): # Validate subnets self.assertTrue(neutron_utils_tests.validate_subnet( - self.neutron, + self.neutron, out_net, self.net_creator.network_settings.subnet_settings[0].name, self.net_creator.network_settings.subnet_settings[0].cidr, True)) @@ -674,7 +675,7 @@ class CreateNetworkGatewayTests(OSIntegrationTestCase): # Validate subnets self.assertTrue(neutron_utils_tests.validate_subnet( - self.neutron, + self.neutron, out_net, self.net_creator.network_settings.subnet_settings[0].name, self.net_creator.network_settings.subnet_settings[0].cidr, True)) @@ -812,6 +813,7 @@ class CreateNetworkTypeTests(OSComponentTestCase): """ guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) 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') self.neutron = neutron_utils.neutron_client( @@ -959,6 +961,7 @@ class CreateMultipleNetworkTests(OSIntegrationTestCase): guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) 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') self.neutron = neutron_utils.neutron_client( diff --git a/snaps/openstack/tests/create_stack_tests.py b/snaps/openstack/tests/create_stack_tests.py index 6325b7a..17acede 100644 --- a/snaps/openstack/tests/create_stack_tests.py +++ b/snaps/openstack/tests/create_stack_tests.py @@ -371,7 +371,7 @@ class CreateStackSuccessTests(OSIntegrationTestCase): self.assertEqual(1, len(net_creators[0].get_network().subnets)) subnet = net_creators[0].get_network().subnets[0] subnet_by_name = neutron_utils.get_subnet( - neutron, subnet_name=subnet.name) + neutron, net_creators[0].get_network(), subnet_name=subnet.name) self.assertEqual(subnet, subnet_by_name) subnet_by_id = neutron_utils.get_subnet_by_id(neutron, subnet.id) diff --git a/snaps/openstack/tests/openstack_tests.py b/snaps/openstack/tests/openstack_tests.py index 3c32eb7..516d24d 100644 --- a/snaps/openstack/tests/openstack_tests.py +++ b/snaps/openstack/tests/openstack_tests.py @@ -312,19 +312,19 @@ def ubuntu_image_settings(name, url=None, image_metadata=None, public=public) -def get_priv_net_config(net_name, subnet_name, router_name=None, +def get_priv_net_config(project_name, net_name, subnet_name, router_name=None, cidr='10.55.0.0/24', external_net=None, netconf_override=None): - return OSNetworkConfig(net_name, subnet_name, cidr, router_name, - external_gateway=external_net, - netconf_override=netconf_override) + return OSNetworkConfig( + project_name, net_name, subnet_name, cidr, router_name, + external_gateway=external_net, netconf_override=netconf_override) -def get_pub_net_config(net_name, subnet_name=None, router_name=None, - cidr='10.55.1.0/24', external_net=None, - netconf_override=None): - return OSNetworkConfig(net_name, subnet_name, cidr, router_name, - external_gateway=external_net, +def get_pub_net_config( + project_name, net_name, subnet_name=None, router_name=None, + cidr='10.55.1.0/24', external_net=None, netconf_override=None): + return OSNetworkConfig(project_name, net_name, subnet_name, cidr, + router_name, external_gateway=external_net, netconf_override=netconf_override) @@ -335,8 +335,8 @@ class OSNetworkConfig: physical_network and segmentation_id """ - def __init__(self, net_name, subnet_name=None, subnet_cidr=None, - router_name=None, external_gateway=None, + def __init__(self, project_name, net_name, subnet_name=None, + subnet_cidr=None, router_name=None, external_gateway=None, netconf_override=None): """ :param netconf_override: dict() containing the reconfigured @@ -361,7 +361,10 @@ class OSNetworkConfig: if subnet_name: self.router_settings = RouterConfig( name=router_name, external_gateway=external_gateway, - internal_subnets=[subnet_name]) + internal_subnets=[{'subnet': { + 'project_name': project_name, + 'network_name': net_name, + 'subnet_name': subnet_name}}]) else: self.router_settings = RouterConfig( name=router_name, external_gateway=external_gateway) 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) diff --git a/snaps/provisioning/tests/ansible_utils_tests.py b/snaps/provisioning/tests/ansible_utils_tests.py index 14fe8e9..142e8ad 100644 --- a/snaps/provisioning/tests/ansible_utils_tests.py +++ b/snaps/provisioning/tests/ansible_utils_tests.py @@ -90,6 +90,7 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): # 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) -- cgit 1.2.3-korg