Using 'INTERNAL-TENANT-ID' as the network owner

If a network does not belong to any tenant (such as the HA router network),
instead of relying on the tenant_id in the plugin context, use a special tenant
id called 'INTERNAL-TENANT-ID' as the tenant_id in the pluin context can be
different during creation and deletion depending on the tenant performing the
operation.

Closes-Bug: 1494302
Change-Id: I139c8a4754fcf75f9549a0c2b67697ad311fa4c8
changes/76/229576/3
Shashank Hegde 7 years ago
parent b80dcf83dd
commit 3ac3b2e354

@ -33,6 +33,7 @@ from networking_arista.ml2 import arista_ml2
LOG = logging.getLogger(__name__)
EOS_UNREACHABLE_MSG = _('Unable to reach EOS')
INTERNAL_TENANT_ID = 'INTERNAL-TENANT-ID'
class AristaDriver(driver_api.MechanismDriver):
@ -75,9 +76,7 @@ class AristaDriver(driver_api.MechanismDriver):
# If network type is not VLAN, do nothing
return
network_id = network['id']
tenant_id = network['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = network['tenant_id'] or INTERNAL_TENANT_ID
segmentation_id = segments[0]['segmentation_id']
with self.eos_sync_lock:
db_lib.remember_tenant(tenant_id)
@ -91,9 +90,7 @@ class AristaDriver(driver_api.MechanismDriver):
network = context.current
network_id = network['id']
network_name = network['name']
tenant_id = network['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = network['tenant_id'] or INTERNAL_TENANT_ID
segments = context.network_segments
vlan_id = segments[0]['segmentation_id']
shared_net = network['shared']
@ -137,9 +134,7 @@ class AristaDriver(driver_api.MechanismDriver):
(new_network['shared'] != orig_network['shared'])):
network_id = new_network['id']
network_name = new_network['name']
tenant_id = new_network['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = new_network['tenant_id'] or INTERNAL_TENANT_ID
vlan_id = new_network['provider:segmentation_id']
shared_net = new_network['shared']
with self.eos_sync_lock:
@ -162,9 +157,7 @@ class AristaDriver(driver_api.MechanismDriver):
"""Delete the network infromation from the DB."""
network = context.current
network_id = network['id']
tenant_id = network['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = network['tenant_id'] or INTERNAL_TENANT_ID
with self.eos_sync_lock:
if db_lib.is_network_provisioned(tenant_id, network_id):
db_lib.forget_network(tenant_id, network_id)
@ -177,9 +170,7 @@ class AristaDriver(driver_api.MechanismDriver):
# If networtk type is not VLAN, do nothing
return
network_id = network['id']
tenant_id = network['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = network['tenant_id'] or INTERNAL_TENANT_ID
with self.eos_sync_lock:
# Succeed deleting network in case EOS is not accessible.
@ -209,9 +200,7 @@ class AristaDriver(driver_api.MechanismDriver):
if host and is_vm_boot:
port_id = port['id']
network_id = port['network_id']
tenant_id = port['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = port['tenant_id'] or INTERNAL_TENANT_ID
with self.eos_sync_lock:
# If network does not exist under this tenant,
# it may be a shared network. Get shared network owner Id
@ -243,9 +232,7 @@ class AristaDriver(driver_api.MechanismDriver):
port_id = port['id']
port_name = port['name']
network_id = port['network_id']
tenant_id = port['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = port['tenant_id'] or INTERNAL_TENANT_ID
with self.eos_sync_lock:
hostname = self._host_name(host)
vm_provisioned = db_lib.is_vm_provisioned(device_id,
@ -297,9 +284,7 @@ class AristaDriver(driver_api.MechanismDriver):
if host and host != orig_port['binding:host_id'] and is_vm_boot:
port_id = new_port['id']
network_id = new_port['network_id']
tenant_id = new_port['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = new_port['tenant_id'] or INTERNAL_TENANT_ID
with self.eos_sync_lock:
db_lib.update_vm_host(device_id, host, port_id,
network_id, tenant_id)
@ -322,9 +307,7 @@ class AristaDriver(driver_api.MechanismDriver):
port_id = port['id']
port_name = port['name']
network_id = port['network_id']
tenant_id = port['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = port['tenant_id'] or INTERNAL_TENANT_ID
with self.eos_sync_lock:
hostname = self._host_name(host)
segmentation_id = db_lib.get_segmentation_id(tenant_id,
@ -369,9 +352,7 @@ class AristaDriver(driver_api.MechanismDriver):
host_id = context.host
device_id = port['device_id']
tenant_id = port['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = port['tenant_id'] or INTERNAL_TENANT_ID
network_id = port['network_id']
port_id = port['id']
with self.eos_sync_lock:
@ -388,9 +369,7 @@ class AristaDriver(driver_api.MechanismDriver):
"""
port = context.current
host = context.host
tenant_id = port['tenant_id']
if not tenant_id:
tenant_id = context._plugin_context.tenant_id
tenant_id = port['tenant_id'] or INTERNAL_TENANT_ID
with self.eos_sync_lock:
self._delete_port(port, host, tenant_id)

@ -20,6 +20,8 @@ from neutron.tests.unit import testlib_api
from networking_arista.ml2 import mechanism_arista
INTERNAL_TENANT_ID = 'INTERNAL-TENANT-ID'
class AristaDriverTestCase(testlib_api.SqlTestCase):
"""Main test cases for Arista Mechanism driver.
@ -72,8 +74,8 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
self.drv.create_network_precommit(network_context)
expected_calls += [
mock.call.remember_tenant(tenant_id),
mock.call.remember_network(tenant_id,
mock.call.remember_tenant(INTERNAL_TENANT_ID),
mock.call.remember_network(INTERNAL_TENANT_ID,
network_id,
segmentation_id)
]
@ -130,8 +132,8 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
self.drv.create_network_postcommit(network_context)
expected_calls += [
mock.call.is_network_provisioned(tenant_id, network_id),
mock.call.create_network(tenant_id, net_dict),
mock.call.is_network_provisioned(INTERNAL_TENANT_ID, network_id),
mock.call.create_network(INTERNAL_TENANT_ID, net_dict),
]
mechanism_arista.db_lib.assert_has_calls(expected_calls)
@ -174,8 +176,8 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
self.drv.delete_network_precommit(network_context)
expected_calls += [
mock.call.is_network_provisioned(tenant_id, network_id),
mock.call.forget_network(tenant_id, network_id),
mock.call.is_network_provisioned(INTERNAL_TENANT_ID, network_id),
mock.call.forget_network(INTERNAL_TENANT_ID, network_id),
]
mechanism_arista.db_lib.assert_has_calls(expected_calls)
@ -217,11 +219,11 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
self.drv.delete_network_postcommit(network_context)
expected_calls += [
mock.call.delete_network(tenant_id, network_id),
mock.call.num_nets_provisioned(tenant_id),
mock.call.num_vms_provisioned(tenant_id),
mock.call.forget_tenant(tenant_id),
mock.call.delete_tenant(tenant_id),
mock.call.delete_network(INTERNAL_TENANT_ID, network_id),
mock.call.num_nets_provisioned(INTERNAL_TENANT_ID),
mock.call.num_vms_provisioned(INTERNAL_TENANT_ID),
mock.call.forget_tenant(INTERNAL_TENANT_ID),
mock.call.delete_tenant(INTERNAL_TENANT_ID),
]
mechanism_arista.db_lib.assert_has_calls(expected_calls)
@ -266,22 +268,21 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
network_id,
segmentation_id,
False)
network_context.current['tenant_id'] = ''
port_context = self._get_port_context(tenant_id,
network_id,
vm_id,
network_context)
port_context.current['tenant_id'] = ''
mechanism_arista.db_lib.is_network_provisioned.return_value = True
host_id = port_context.current['binding:host_id']
port_id = port_context.current['id']
self.drv.create_port_precommit(port_context)
expected_calls += [
mock.call.is_network_provisioned(tenant_id, network_id),
mock.call.remember_tenant(tenant_id),
mock.call.is_network_provisioned(INTERNAL_TENANT_ID, network_id),
mock.call.remember_tenant(INTERNAL_TENANT_ID),
mock.call.remember_vm(vm_id, host_id, port_id,
network_id, tenant_id)
network_id, INTERNAL_TENANT_ID)
]
mechanism_arista.db_lib.assert_has_calls(expected_calls)
@ -334,11 +335,11 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
network_id,
segmentation_id,
False)
network_context.current['tenant_id'] = ''
port_context = self._get_port_context(tenant_id,
network_id,
vm_id,
network_context)
port_context.current['tenant_id'] = ''
mechanism_arista.db_lib.is_vm_provisioned.return_value = True
mechanism_arista.db_lib.is_network_provisioned.return_value = True
mechanism_arista.db_lib.get_shared_network_owner_id.return_value = 1
@ -354,10 +355,10 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
expected_calls += [
mock.call.is_vm_provisioned(device_id, host_id, port_id,
network_id, tenant_id),
mock.call.is_network_provisioned(tenant_id, network_id),
network_id, INTERNAL_TENANT_ID),
mock.call.is_network_provisioned(INTERNAL_TENANT_ID, network_id),
mock.call.plug_port_into_network(device_id, host_id, port_id,
network_id, tenant_id,
network_id, INTERNAL_TENANT_ID,
port_name, device_owner)
]
@ -406,12 +407,11 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
network_id,
segmentation_id,
False)
network_context.current['tenant_id'] = ''
port_context = self._get_port_context(tenant_id,
network_id,
vm_id,
network_context)
port_context.current['tenant_id'] = ''
mechanism_arista.db_lib.is_vm_provisioned.return_value = True
mechanism_arista.db_lib.num_nets_provisioned.return_value = 0
mechanism_arista.db_lib.num_vms_provisioned.return_value = 0
@ -421,9 +421,9 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
port_id = port_context.current['id']
expected_calls += [
mock.call.is_vm_provisioned(vm_id, host_id, port_id,
network_id, tenant_id),
network_id, INTERNAL_TENANT_ID),
mock.call.forget_vm(vm_id, host_id, port_id,
network_id, tenant_id),
network_id, INTERNAL_TENANT_ID),
]
mechanism_arista.db_lib.assert_has_calls(expected_calls)
@ -475,11 +475,11 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
network_id,
segmentation_id,
False)
network_context.current['tenant_id'] = ''
port_context = self._get_port_context(tenant_id,
network_id,
vm_id,
network_context)
port_context.current['tenant_id'] = ''
port = port_context.current
device_id = port['device_id']
host_id = port['binding:host_id']
@ -488,13 +488,13 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
self.drv.delete_port_postcommit(port_context)
expected_calls += [
mock.call.is_network_provisioned(tenant_id, network_id),
mock.call.is_network_provisioned(INTERNAL_TENANT_ID, network_id),
mock.call.unplug_host_from_network(device_id, host_id, port_id,
network_id, tenant_id),
mock.call.num_nets_provisioned(tenant_id),
mock.call.num_vms_provisioned(tenant_id),
mock.call.forget_tenant(tenant_id),
mock.call.delete_tenant(tenant_id),
network_id, INTERNAL_TENANT_ID),
mock.call.num_nets_provisioned(INTERNAL_TENANT_ID),
mock.call.num_vms_provisioned(INTERNAL_TENANT_ID),
mock.call.forget_tenant(INTERNAL_TENANT_ID),
mock.call.delete_tenant(INTERNAL_TENANT_ID),
]
mechanism_arista.db_lib.assert_has_calls(expected_calls)
@ -537,12 +537,11 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
network_id,
segmentation_id,
False)
network_context.current['tenant_id'] = ''
port_context = self._get_port_context(tenant_id,
network_id,
vm_id,
network_context)
port_context.current['tenant_id'] = ''
host_id = port_context.current['binding:host_id']
port_context.original['binding:host_id'] = 'ubuntu0'
port_id = port_context.current['id']
@ -550,7 +549,7 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
expected_calls += [
mock.call.update_vm_host(vm_id, host_id, port_id,
network_id, tenant_id)
network_id, INTERNAL_TENANT_ID)
]
mechanism_arista.db_lib.assert_has_calls(expected_calls)
@ -618,11 +617,11 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
network_id,
segmentation_id,
False)
network_context.current['tenant_id'] = ''
port_context = self._get_port_context(tenant_id,
network_id,
vm_id,
network_context)
port_context.current['tenant_id'] = ''
mechanism_arista.db_lib.is_vm_provisioned.return_value = True
mechanism_arista.db_lib.is_network_provisioned.return_value = True
@ -643,18 +642,19 @@ class AristaDriverTestCase(testlib_api.SqlTestCase):
self.drv.update_port_postcommit(port_context)
expected_calls += [
mock.call.get_segmentation_id(tenant_id, network_id),
mock.call.get_segmentation_id(INTERNAL_TENANT_ID, network_id),
mock.call.is_vm_provisioned(device_id, host_id, port_id,
network_id, tenant_id),
mock.call.is_network_provisioned(tenant_id, network_id,
network_id, INTERNAL_TENANT_ID),
mock.call.is_network_provisioned(INTERNAL_TENANT_ID, network_id,
segmentation_id),
mock.call.is_network_provisioned(tenant_id, network_id),
mock.call.is_network_provisioned(INTERNAL_TENANT_ID, network_id),
mock.call.unplug_host_from_network(device_id, orig_host_id,
port_id, network_id, tenant_id),
mock.call.num_nets_provisioned(tenant_id),
mock.call.num_vms_provisioned(tenant_id),
port_id, network_id,
INTERNAL_TENANT_ID),
mock.call.num_nets_provisioned(INTERNAL_TENANT_ID),
mock.call.num_vms_provisioned(INTERNAL_TENANT_ID),
mock.call.plug_port_into_network(device_id, host_id, port_id,
network_id, tenant_id,
network_id, INTERNAL_TENANT_ID,
port_name, device_owner)
]

Loading…
Cancel
Save