Virt: add context to attach and detach interface
Add the context parameter to the virt interfaces for attaching and detaching ports. This was suggested as part of review https://review.openstack.org/#/c/147126/ TrivialFix Change-Id: I5c461a8242c51994d12ce9c6774d5f956232f950
This commit is contained in:
parent
b7a8086ed0
commit
41591c9456
@ -5018,7 +5018,8 @@ class ComputeManager(manager.Manager):
|
||||
image_meta = objects.ImageMeta.from_instance(instance)
|
||||
|
||||
try:
|
||||
self.driver.attach_interface(instance, image_meta, network_info[0])
|
||||
self.driver.attach_interface(context, instance, image_meta,
|
||||
network_info[0])
|
||||
except exception.NovaException as ex:
|
||||
port_id = network_info[0].get('id')
|
||||
LOG.warning(_LW("attach interface failed , try to deallocate "
|
||||
@ -5050,7 +5051,7 @@ class ComputeManager(manager.Manager):
|
||||
raise exception.PortNotFound(_("Port %s is not "
|
||||
"attached") % port_id)
|
||||
try:
|
||||
self.driver.detach_interface(instance, condemned)
|
||||
self.driver.detach_interface(context, instance, condemned)
|
||||
except exception.NovaException as ex:
|
||||
LOG.warning(_LW("Detach interface failed, port_id=%(port_id)s,"
|
||||
" reason: %(msg)s"),
|
||||
|
@ -13820,10 +13820,10 @@ class LibvirtConnTestCase(test.NoDBTestCase):
|
||||
self.mox.ReplayAll()
|
||||
|
||||
if method_name == "attach_interface":
|
||||
drvr.attach_interface(instance, fake_image_meta,
|
||||
drvr.attach_interface(self.context, instance, fake_image_meta,
|
||||
network_info[0])
|
||||
elif method_name == "detach_interface":
|
||||
drvr.detach_interface(instance, network_info[0])
|
||||
drvr.detach_interface(self.context, instance, network_info[0])
|
||||
else:
|
||||
raise ValueError("Unhandled method %s" % method_name)
|
||||
|
||||
@ -16224,10 +16224,10 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
|
||||
self.mox.ReplayAll()
|
||||
if method == 'attach_interface':
|
||||
self.drvr.attach_interface(
|
||||
instance, fake_image_meta, network_info[0])
|
||||
self.context, instance, fake_image_meta, network_info[0])
|
||||
elif method == 'detach_interface':
|
||||
self.drvr.detach_interface(
|
||||
instance, network_info[0])
|
||||
self.context, instance, network_info[0])
|
||||
self.mox.VerifyAll()
|
||||
|
||||
def test_attach_interface_with_running_instance(self):
|
||||
@ -16280,7 +16280,7 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
|
||||
guest.detach_device = mock.Mock(side_effect=error)
|
||||
# mock out that get_interface_by_mac doesn't find the interface
|
||||
guest.get_interface_by_mac = mock.Mock(return_value=None)
|
||||
self.drvr.detach_interface(instance, vif)
|
||||
self.drvr.detach_interface(self.context, instance, vif)
|
||||
guest.get_interface_by_mac.assert_called_once_with(vif['address'])
|
||||
# an error shouldn't be logged, but a warning should be logged
|
||||
self.assertFalse(mock_log.error.called)
|
||||
|
@ -2178,7 +2178,8 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
|
||||
self.assertEqual(num_iface_ids, num_found)
|
||||
|
||||
def _attach_interface(self, vif):
|
||||
self.conn.attach_interface(self.instance, self.image, vif)
|
||||
self.conn.attach_interface(self.context, self.instance, self.image,
|
||||
vif)
|
||||
self._validate_interfaces(vif['id'], 1, 2)
|
||||
|
||||
def test_attach_interface(self):
|
||||
@ -2194,14 +2195,14 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
|
||||
side_effect=Exception):
|
||||
self.assertRaises(exception.InterfaceAttachFailed,
|
||||
self.conn.attach_interface,
|
||||
self.instance, self.image, vif)
|
||||
self.context, self.instance, self.image, vif)
|
||||
|
||||
@mock.patch.object(vif, 'get_network_device',
|
||||
return_value='fake_device')
|
||||
def _detach_interface(self, vif, mock_get_device):
|
||||
self._create_vm()
|
||||
self._attach_interface(vif)
|
||||
self.conn.detach_interface(self.instance, vif)
|
||||
self.conn.detach_interface(self.context, self.instance, vif)
|
||||
self._validate_interfaces('free', 1, 2)
|
||||
|
||||
def test_detach_interface(self):
|
||||
@ -2211,7 +2212,8 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
|
||||
def test_detach_interface_and_attach(self):
|
||||
vif = self._create_vif()
|
||||
self._detach_interface(vif)
|
||||
self.conn.attach_interface(self.instance, self.image, vif)
|
||||
self.conn.attach_interface(self.context, self.instance, self.image,
|
||||
vif)
|
||||
self._validate_interfaces(vif['id'], 1, 2)
|
||||
|
||||
def test_detach_interface_no_device(self):
|
||||
@ -2219,7 +2221,7 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
|
||||
vif = self._create_vif()
|
||||
self._attach_interface(vif)
|
||||
self.assertRaises(exception.NotFound, self.conn.detach_interface,
|
||||
self.instance, vif)
|
||||
self.context, self.instance, vif)
|
||||
|
||||
def test_detach_interface_no_vif_match(self):
|
||||
self._create_vm()
|
||||
@ -2227,7 +2229,7 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
|
||||
self._attach_interface(vif)
|
||||
vif['id'] = 'bad-id'
|
||||
self.assertRaises(exception.NotFound, self.conn.detach_interface,
|
||||
self.instance, vif)
|
||||
self.context, self.instance, vif)
|
||||
|
||||
@mock.patch.object(vif, 'get_network_device',
|
||||
return_value='fake_device')
|
||||
@ -2240,7 +2242,7 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
|
||||
side_effect=Exception):
|
||||
self.assertRaises(exception.InterfaceDetachFailed,
|
||||
self.conn.detach_interface,
|
||||
self.instance, vif)
|
||||
self.context, self.instance, vif)
|
||||
|
||||
def test_resize_to_smaller_disk(self):
|
||||
self._create_vm(instance_type='m1.large')
|
||||
|
@ -2527,8 +2527,8 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
|
||||
self._network_values)
|
||||
extra_specs = vm_util.ExtraSpecs()
|
||||
mock_extra_specs.return_value = extra_specs
|
||||
self._vmops.attach_interface(self._instance, self._image_meta,
|
||||
self._network_values)
|
||||
self._vmops.attach_interface(self._context, self._instance,
|
||||
self._image_meta, self._network_values)
|
||||
mock_get_vm_ref.assert_called_once_with(self._session, self._instance)
|
||||
mock_get_attach_port_index(self._session, 'fake-ref')
|
||||
mock_get_network_attach_config_spec.assert_called_once_with(
|
||||
@ -2556,7 +2556,8 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
|
||||
|
||||
with mock.patch.object(self._session, '_call_method',
|
||||
return_value='hardware-devices'):
|
||||
self._vmops.detach_interface(self._instance, self._network_values)
|
||||
self._vmops.detach_interface(self._context, self._instance,
|
||||
self._network_values)
|
||||
mock_get_vm_ref.assert_called_once_with(self._session, self._instance)
|
||||
mock_get_detach_port_index(self._session, 'fake-ref')
|
||||
mock_get_network_detach_config_spec.assert_called_once_with(
|
||||
@ -2632,7 +2633,8 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
|
||||
shares_share=40)
|
||||
extra_specs = vm_util.ExtraSpecs(vif_limits=vif_limits)
|
||||
mock_extra_specs.return_value = extra_specs
|
||||
self._vmops.attach_interface(self._instance, self._image_meta,
|
||||
self._vmops.attach_interface(self._context, self._instance,
|
||||
self._image_meta,
|
||||
self._network_values)
|
||||
mock_get_vm_ref.assert_called_once_with(self._session, self._instance)
|
||||
mock_get_attach_port_index(self._session, 'fake-ref')
|
||||
|
@ -489,11 +489,12 @@ class ComputeDriver(object):
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def attach_interface(self, instance, image_meta, vif):
|
||||
def attach_interface(self, context, instance, image_meta, vif):
|
||||
"""Use hotplug to add a network interface to a running instance.
|
||||
|
||||
The counter action to this is :func:`detach_interface`.
|
||||
|
||||
:param context: The request context.
|
||||
:param nova.objects.instance.Instance instance:
|
||||
The instance which will get an additional network interface.
|
||||
:param nova.objects.ImageMeta image_meta:
|
||||
@ -507,11 +508,12 @@ class ComputeDriver(object):
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def detach_interface(self, instance, vif):
|
||||
def detach_interface(self, context, instance, vif):
|
||||
"""Use hotunplug to remove a network interface from a running instance.
|
||||
|
||||
The counter action to this is :func:`attach_interface`.
|
||||
|
||||
:param context: The request context.
|
||||
:param nova.objects.instance.Instance instance:
|
||||
The instance which gets a network interface removed.
|
||||
:param nova.network.model.NetworkInfo vif:
|
||||
|
@ -304,13 +304,13 @@ class FakeDriver(driver.ComputeDriver):
|
||||
self._mounts[instance_name] = {}
|
||||
self._mounts[instance_name][mountpoint] = new_connection_info
|
||||
|
||||
def attach_interface(self, instance, image_meta, vif):
|
||||
def attach_interface(self, context, instance, image_meta, vif):
|
||||
if vif['id'] in self._interfaces:
|
||||
raise exception.InterfaceAttachFailed(
|
||||
instance_uuid=instance.uuid)
|
||||
self._interfaces[vif['id']] = vif
|
||||
|
||||
def detach_interface(self, instance, vif):
|
||||
def detach_interface(self, context, instance, vif):
|
||||
try:
|
||||
del self._interfaces[vif['id']]
|
||||
except KeyError:
|
||||
|
@ -344,10 +344,10 @@ class HyperVDriver(driver.ComputeDriver):
|
||||
def manage_image_cache(self, context, all_instances):
|
||||
self._imagecache.update(context, all_instances)
|
||||
|
||||
def attach_interface(self, instance, image_meta, vif):
|
||||
def attach_interface(self, context, instance, image_meta, vif):
|
||||
return self._vmops.attach_interface(instance, vif)
|
||||
|
||||
def detach_interface(self, instance, vif):
|
||||
def detach_interface(self, context, instance, vif):
|
||||
return self._vmops.detach_interface(instance, vif)
|
||||
|
||||
def rescue(self, context, instance, network_info, image_meta,
|
||||
|
@ -1311,7 +1311,7 @@ class LibvirtDriver(driver.ComputeDriver):
|
||||
|
||||
self._disconnect_volume(connection_info, disk_dev)
|
||||
|
||||
def attach_interface(self, instance, image_meta, vif):
|
||||
def attach_interface(self, context, instance, image_meta, vif):
|
||||
guest = self._host.get_guest(instance)
|
||||
|
||||
self.vif_driver.plug(instance, vif)
|
||||
@ -1331,7 +1331,7 @@ class LibvirtDriver(driver.ComputeDriver):
|
||||
raise exception.InterfaceAttachFailed(
|
||||
instance_uuid=instance.uuid)
|
||||
|
||||
def detach_interface(self, instance, vif):
|
||||
def detach_interface(self, context, instance, vif):
|
||||
guest = self._host.get_guest(instance)
|
||||
cfg = self.vif_driver.get_config(instance, vif,
|
||||
instance.image_meta,
|
||||
|
@ -492,13 +492,13 @@ class VMwareVCDriver(driver.ComputeDriver):
|
||||
"""Efficient override of base instance_exists method."""
|
||||
return self._vmops.instance_exists(instance)
|
||||
|
||||
def attach_interface(self, instance, image_meta, vif):
|
||||
def attach_interface(self, context, instance, image_meta, vif):
|
||||
"""Attach an interface to the instance."""
|
||||
self._vmops.attach_interface(instance, image_meta, vif)
|
||||
self._vmops.attach_interface(context, instance, image_meta, vif)
|
||||
|
||||
def detach_interface(self, instance, vif):
|
||||
def detach_interface(self, context, instance, vif):
|
||||
"""Detach an interface from the instance."""
|
||||
self._vmops.detach_interface(instance, vif)
|
||||
self._vmops.detach_interface(context, instance, vif)
|
||||
|
||||
|
||||
class VMwareAPISession(api.VMwareAPISession):
|
||||
|
@ -1741,7 +1741,7 @@ class VMwareVMOps(object):
|
||||
except exception.InstanceNotFound:
|
||||
return False
|
||||
|
||||
def attach_interface(self, instance, image_meta, vif):
|
||||
def attach_interface(self, context, instance, image_meta, vif):
|
||||
"""Attach an interface to the instance."""
|
||||
vif_model = image_meta.properties.get('hw_vif_model',
|
||||
constants.DEFAULT_VIF_MODEL)
|
||||
@ -1771,13 +1771,12 @@ class VMwareVMOps(object):
|
||||
raise exception.InterfaceAttachFailed(
|
||||
instance_uuid=instance.uuid)
|
||||
|
||||
context = nova_context.get_admin_context()
|
||||
self._network_api.update_instance_vnic_index(
|
||||
context, instance, vif, port_index)
|
||||
|
||||
LOG.debug("Reconfigured VM to attach interface", instance=instance)
|
||||
|
||||
def detach_interface(self, instance, vif):
|
||||
def detach_interface(self, context, instance, vif):
|
||||
"""Detach an interface from the instance."""
|
||||
vm_ref = vm_util.get_vm_ref(self._session, instance)
|
||||
# Ensure that there is not a race with the port index management
|
||||
@ -1803,7 +1802,6 @@ class VMwareVMOps(object):
|
||||
"VM") % vif['address']
|
||||
raise exception.NotFound(msg)
|
||||
|
||||
context = nova_context.get_admin_context()
|
||||
self._network_api.update_instance_vnic_index(
|
||||
context, instance, vif, None)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user