Merge "compute: Validate a BDMs disk_bus when provided" into stable/ussuri

This commit is contained in:
Zuul 2020-08-26 18:02:55 +00:00 committed by Gerrit Code Review
commit 5267bdb61e
7 changed files with 38 additions and 4 deletions

View File

@ -2515,9 +2515,10 @@ disk_available_least_total:
disk_bus: disk_bus:
description: | description: |
Disk bus type, some hypervisors (currently only libvirt) support Disk bus type, some hypervisors (currently only libvirt) support
specify this parameter. Some example disk_bus values can be: `ide`, specify this parameter. Some example disk_bus values can be: ``fdc``,
`usb`, `virtio`, `scsi`. This is not an exhaustive list as it depends ``ide``, ``sata``, ``scsi``, ``usb``, ``virtio``, ``xen``, ``lxc``
on the virtualization driver, and may change as more support is added. and ``uml``. Support for each bus type depends on the virtualization driver
and underlying hypervisor.
in: body in: body
required: false required: false
type: string type: string

View File

@ -748,6 +748,7 @@ class ServersController(wsgi.Controller):
exception.InvalidBDMEphemeralSize, exception.InvalidBDMEphemeralSize,
exception.InvalidBDMFormat, exception.InvalidBDMFormat,
exception.InvalidBDMSwapSize, exception.InvalidBDMSwapSize,
exception.InvalidBDMDiskBus,
exception.VolumeTypeNotFound, exception.VolumeTypeNotFound,
exception.AutoDiskConfigDisabledByImage, exception.AutoDiskConfigDisabledByImage,
exception.InstanceGroupNotFound, exception.InstanceGroupNotFound,

View File

@ -1742,6 +1742,13 @@ class API(base.Base):
"(source: 'blank', dest: 'volume') need to have non-zero " "(source: 'blank', dest: 'volume') need to have non-zero "
"size")) "size"))
# NOTE(lyarwood): Ensure the disk_bus is at least known to Nova.
# The virt driver may reject this later but for now just ensure
# it's listed as an acceptable value of the DiskBus field class.
disk_bus = bdm.disk_bus if 'disk_bus' in bdm else None
if disk_bus and disk_bus not in fields_obj.DiskBus.ALL:
raise exception.InvalidBDMDiskBus(disk_bus=disk_bus)
ephemeral_size = sum(bdm.volume_size or instance_type['ephemeral_gb'] ephemeral_size = sum(bdm.volume_size or instance_type['ephemeral_gb']
for bdm in block_device_mappings for bdm in block_device_mappings
if block_device.new_format_is_ephemeral(bdm)) if block_device.new_format_is_ephemeral(bdm))

View File

@ -251,6 +251,11 @@ class TooManyDiskDevices(InvalidBDM):
code = 403 code = 403
class InvalidBDMDiskBus(InvalidBDM):
msg_fmr = _("Block Device Mapping is invalid: The provided disk bus "
"%(disk_bus)s is not valid.")
class InvalidAttribute(Invalid): class InvalidAttribute(Invalid):
msg_fmt = _("Attribute not supported: %(attr)s") msg_fmt = _("Attribute not supported: %(attr)s")

View File

@ -340,6 +340,8 @@ class DiskBus(BaseNovaEnum):
# NOTE(aspiers): If you change this, don't forget to update the # NOTE(aspiers): If you change this, don't forget to update the
# docs and metadata for hw_*_bus in glance. # docs and metadata for hw_*_bus in glance.
# NOTE(lyarwood): Also update the possible values in the api-ref for the
# block_device_mapping_v2.disk_bus parameter.
FDC = "fdc" FDC = "fdc"
IDE = "ide" IDE = "ide"
SATA = "sata" SATA = "sata"

View File

@ -5059,7 +5059,8 @@ class ServersControllerCreateTest(test.TestCase):
(exception.InvalidBDMVolume, {'id': 'fake'}), (exception.InvalidBDMVolume, {'id': 'fake'}),
(exception.InvalidBDMImage, {'id': 'fake'}), (exception.InvalidBDMImage, {'id': 'fake'}),
(exception.InvalidBDMBootSequence, {}), (exception.InvalidBDMBootSequence, {}),
(exception.InvalidBDMLocalsLimit, {})) (exception.InvalidBDMLocalsLimit, {}),
(exception.InvalidBDMDiskBus, {'disk_bus': 'foo'}))
ex_iter = iter(bdm_exceptions) ex_iter = iter(bdm_exceptions)

View File

@ -4662,6 +4662,23 @@ class _ComputeAPIUnitTestMixIn(object):
bdms, image_cache, volumes) bdms, image_cache, volumes)
mock_get_image.assert_called_once_with(self.context, image_id) mock_get_image.assert_called_once_with(self.context, image_id)
@mock.patch('nova.compute.api.API._get_image')
def test_validate_bdm_disk_bus(self, mock_get_image):
"""Tests that _validate_bdm fail if an invalid disk_bus is provided
"""
instance = self._create_instance_obj()
bdms = objects.BlockDeviceMappingList(objects=[
objects.BlockDeviceMapping(
boot_index=0, image_id=instance.image_ref,
source_type='image', destination_type='volume',
volume_type=None, snapshot_id=None, volume_id=None,
volume_size=1, disk_bus='virtio-scsi')])
image_cache = volumes = {}
self.assertRaises(exception.InvalidBDMDiskBus,
self.compute_api._validate_bdm,
self.context, instance, objects.Flavor(),
bdms, image_cache, volumes)
def test_the_specified_volume_type_id_assignment_to_name(self): def test_the_specified_volume_type_id_assignment_to_name(self):
"""Test _check_requested_volume_type method is called, if the user """Test _check_requested_volume_type method is called, if the user
is using the volume type ID, assign volume_type to volume type name. is using the volume type ID, assign volume_type to volume type name.