diff --git a/vmware_nsx/dvs/dvs.py b/vmware_nsx/dvs/dvs.py index 35f29cc047..2d33fabf64 100644 --- a/vmware_nsx/dvs/dvs.py +++ b/vmware_nsx/dvs/dvs.py @@ -324,3 +324,13 @@ class DvsManager(object): instanceUuid=True) if vm_refs: return vm_refs[0].value + + def get_portgroup_info(self, pg_moref): + """Get portgroup information.""" + # Expand the properties to collect on need basis. + properties = ['name'] + pg_info = self._session.invoke_api(vim_util, + 'get_object_properties_dict', + self._session.vim, + pg_moref, properties) + return pg_info diff --git a/vmware_nsx/plugins/dvs/plugin.py b/vmware_nsx/plugins/dvs/plugin.py index 957032667a..d6d0b01fde 100644 --- a/vmware_nsx/plugins/dvs/plugin.py +++ b/vmware_nsx/plugins/dvs/plugin.py @@ -151,9 +151,10 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin, if net_data.get(pnet.NETWORK_TYPE) == c_utils.NetworkTypes.PORTGROUP: net_id = net_data.get(pnet.PHYSICAL_NETWORK) dvpg_moref = self._dvs._net_id_to_moref(net_id) - if dvpg_moref.name != net_data.get('name'): - err_msg = (_("Portgroup name %s(dvpg)s must match network " - "name %(network)s"), {'dvpg': dvpg_moref.name, + pg_info = self._dvs.get_portgroup_info(dvpg_moref) + if pg_info.get('name') != net_data.get('name'): + err_msg = (_("Portgroup name %(dvpg)s must match network " + "name %(network)s") % {'dvpg': pg_info.get('name'), 'network': net_data.get('name')}) raise n_exc.InvalidInput(error_message=err_msg) dvs_id = dvpg_moref.value diff --git a/vmware_nsx/tests/unit/dvs/test_plugin.py b/vmware_nsx/tests/unit/dvs/test_plugin.py index f37e34365c..1c3b1c1cb4 100644 --- a/vmware_nsx/tests/unit/dvs/test_plugin.py +++ b/vmware_nsx/tests/unit/dvs/test_plugin.py @@ -197,19 +197,25 @@ class NeutronSimpleDvsTest(test_plugin.NeutronDbPluginV2TestCase): def test_create_and_delete_dvs_network_flat(self): self._create_and_delete_dvs_network() + @mock.patch.object(dvs.DvsManager, 'get_portgroup_info') @mock.patch.object(dvs.DvsManager, '_net_id_to_moref') - def test_create_and_delete_dvs_network_portgroup(self, fake_get_moref): - fake_get_moref.return_value.name = 'fake-name' + def test_create_and_delete_dvs_network_portgroup(self, fake_get_moref, + fake_pg_info): + fake_pg_info.return_value = {'name': 'fake-name'} self._create_and_delete_dvs_network(network_type='portgroup') self.assertTrue(fake_get_moref.call_count) + self.assertTrue(fake_pg_info.call_count) + @mock.patch.object(dvs.DvsManager, 'get_portgroup_info') @mock.patch.object(dvs.DvsManager, '_net_id_to_moref') def test_create_and_delete_dvs_network_portgroup_vlan(self, - fake_get_moref): - fake_get_moref.return_value.name = 'fake-name' + fake_get_moref, + fake_pg_info): + fake_pg_info.return_value = {'name': 'fake-name'} self._create_and_delete_dvs_network(network_type='portgroup', vlan_tag=7) self.assertTrue(fake_get_moref.call_count) + self.assertTrue(fake_pg_info.call_count) def test_create_and_delete_dvs_port(self): params = {'provider:network_type': 'vlan', @@ -297,10 +303,12 @@ class NeutronSimpleDvsTest(test_plugin.NeutronDbPluginV2TestCase): {'network': {'port_security_enabled': True}}) self.assertEqual(True, updated_net['port_security_enabled']) + @mock.patch.object(dvs.DvsManager, 'get_portgroup_info') @mock.patch.object(dvs.DvsManager, '_net_id_to_moref') def test_create_and_delete_portgroup_network_invalid_name(self, - fake_get_moref): - fake_get_moref.return_value.name = 'fake-different-name' + fake_get_moref, + fake_pg_info): + fake_pg_info.return_value = {'name': 'fake-different-name'} data = {'network': {'provider:network_type': 'portgroup', 'name': 'fake-name', 'admin_state_up': True}}