[dvs] Validate network name for portgroup networks

Add a validation to ensure the network name given for
provider network of type 'portgroup' matches the dvportgroup
name. This is make the behavior consistent with nsxv plugin.

Change-Id: I05d8b1108e045cfcd095d6a695254052b92595d7
This commit is contained in:
Giridhar Jayavelu 2016-08-21 16:25:11 -07:00
parent bae2327e6d
commit af8cd3f2ca
2 changed files with 21 additions and 2 deletions

View File

@ -149,7 +149,13 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin,
net_id = None
if net_data.get(pnet.NETWORK_TYPE) == c_utils.NetworkTypes.PORTGROUP:
net_id = net_data.get(pnet.PHYSICAL_NETWORK)
dvs_id = self._dvs._net_id_to_moref(net_id).value
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,
'network': net_data.get('name')})
raise n_exc.InvalidInput(error_message=err_msg)
dvs_id = dvpg_moref.value
else:
dvs_id = self._dvs_get_id(net_data)
self._dvs.add_port_group(dvs_id, vlan_tag)

View File

@ -156,7 +156,8 @@ class NeutronSimpleDvsTest(test_plugin.NeutronDbPluginV2TestCase):
def _create_and_delete_dvs_network(self, network_type='flat', vlan_tag=0):
params = {'provider:network_type': network_type,
'provider:physical_network': 'fake-moid'}
'provider:physical_network': 'fake-moid',
'name': 'fake-name'}
if network_type == 'vlan':
params['provider:segmentation_id'] = vlan_tag
params['arg_list'] = tuple(params.keys())
@ -198,12 +199,14 @@ class NeutronSimpleDvsTest(test_plugin.NeutronDbPluginV2TestCase):
@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'
self._create_and_delete_dvs_network(network_type='portgroup')
self.assertTrue(fake_get_moref.call_count)
@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'
self._create_and_delete_dvs_network(network_type='portgroup',
vlan_tag=7)
self.assertTrue(fake_get_moref.call_count)
@ -293,3 +296,13 @@ class NeutronSimpleDvsTest(test_plugin.NeutronDbPluginV2TestCase):
ctx, id,
{'network': {'port_security_enabled': True}})
self.assertEqual(True, updated_net['port_security_enabled'])
@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'
data = {'network': {'provider:network_type': 'portgroup',
'name': 'fake-name',
'admin_state_up': True}}
self.assertRaises(exp.BadRequest, self._plugin.create_network,
context.get_admin_context(), data)