NSXV/L2GW: validate that portgroup is used only once

The NSX only allows one DLR to connect to a port group. Trying
with more than one throws an exception.

So we prevent this from happening when a L2 GW is created.

Change-Id: I9c085a9244738c09942ca6e2c7606539ab83c8b4
This commit is contained in:
Gary Kotton 2017-06-14 02:04:09 -07:00
parent 0a0fe5a6fc
commit 6f7438e992
2 changed files with 17 additions and 3 deletions

View File

@ -15,6 +15,7 @@
# under the License.
from networking_l2gw.db.l2gateway import l2gateway_db
from networking_l2gw.db.l2gateway import l2gateway_models as models
from networking_l2gw.services.l2gateway.common import constants as l2gw_const
from networking_l2gw.services.l2gateway import exceptions as l2gw_exc
from neutron_lib import exceptions as n_exc
@ -61,7 +62,14 @@ class NsxvL2GatewayDriver(l2gateway_db.L2GatewayMixin):
msg = _("Only a single device is supported for one L2 gateway")
raise n_exc.InvalidInput(error_message=msg)
def _validate_interface_list(self, interfaces):
def _get_l2gateway_interface(self, context, interface_name):
"""Get all l2gateway_interfaces_by interface_name."""
session = context.session
with session.begin():
return session.query(models.L2GatewayInterface).filter_by(
interface_name=interface_name).all()
def _validate_interface_list(self, context, interfaces):
# In NSXv, interface is mapped to a vDS VLAN port group.
# Since HA is not supported, only one interface is expected
if len(interfaces) != 1:
@ -70,6 +78,11 @@ class NsxvL2GatewayDriver(l2gateway_db.L2GatewayMixin):
if not self._nsxv.vcns.validate_network(interfaces[0]['name']):
msg = _("Configured interface not found")
raise n_exc.InvalidInput(error_message=msg)
interface = self.get_l2gateway_interfaces(context,
interfaces[0]['name'])
if interface:
msg = _("%s is already used.") % interfaces[0]['name']
raise n_exc.InvalidInput(error_message=msg)
def create_l2_gateway_precommit(self, context, l2_gateway):
pass
@ -84,7 +97,7 @@ class NsxvL2GatewayDriver(l2gateway_db.L2GatewayMixin):
devices = gw['devices']
self._validate_device_list(devices)
interfaces = devices[0]['interfaces']
self._validate_interface_list(interfaces)
self._validate_interface_list(context, interfaces)
# Create a dedicated DLR
try:
edge_id = self._create_l2_gateway_edge(context)

View File

@ -70,6 +70,7 @@ class TestL2gatewayDriver(base.BaseTestCase):
_nsxv.vcns.validate_network.return_value = False
self.assertRaises(n_exc.InvalidInput,
self.plugin._validate_interface_list,
self.context,
fake_interfaces)
@mock.patch('vmware_nsx.services.l2gateway.'
@ -130,7 +131,7 @@ class TestL2gatewayDriver(base.BaseTestCase):
self.plugin.create_l2_gateway(self.context, fake_l2gw_dict)
_admin_check.assert_called_with(self.context, 'CREATE')
val_dev.assert_called_with(fake_devices)
val_inter.assert_called_with(fake_interfaces)
val_inter.assert_called_with(self.context, fake_interfaces)
@mock.patch('networking_l2gw.db.l2gateway.l2gateway_db.'
'L2GatewayMixin._admin_check')