Centralize volume create code during boot from volume

This centralizes the code that creates a volume during
boot from volume and the BDM source_type is one of blank,
image or snapshot.

In a subsequent change [1] the central _create_volume method
will be enhanced to create a "new style" volume attachment.

[1] I30a49c9e085cffc071d0ba332db67945e116bc80

Change-Id: I4205c00311f389907dcc390869414687ac03b7f5
Related-Bug: #1747693
This commit is contained in:
Matt Riedemann 2019-09-16 10:45:33 -04:00
parent 1a0d80dda2
commit aae96dc1ac
2 changed files with 32 additions and 27 deletions

View File

@ -903,8 +903,8 @@ class TestDriverBlockDevice(test.NoDBTestCase):
self.volume_api.get_snapshot.assert_called_once_with(
self.context, 'fake-snapshot-id-1')
self.volume_api.create.assert_called_once_with(
self.context, 3, '', '', snapshot, availability_zone=None,
volume_type=None)
self.context, 3, '', '', availability_zone=None,
snapshot=snapshot, volume_type=None)
wait_func.assert_called_once_with(self.context, 'fake-volume-id-2')
def test_snapshot_attach_no_volume_cinder_cross_az_attach_false(self):
@ -936,8 +936,8 @@ class TestDriverBlockDevice(test.NoDBTestCase):
self.volume_api.get_snapshot.assert_called_once_with(
self.context, 'fake-snapshot-id-1')
self.volume_api.create.assert_called_once_with(
self.context, 3, '', '', snapshot, availability_zone='test-az',
volume_type=None)
self.context, 3, '', '', availability_zone='test-az',
snapshot=snapshot, volume_type=None)
wait_func.assert_called_once_with(self.context, 'fake-volume-id-2')
def test_snapshot_attach_fail_volume(self):
@ -976,8 +976,8 @@ class TestDriverBlockDevice(test.NoDBTestCase):
vol_get_snap.assert_called_once_with(
self.context, 'fake-snapshot-id-1')
vol_create.assert_called_once_with(
self.context, 3, '', '', snapshot, availability_zone=None,
volume_type=None)
self.context, 3, '', '', availability_zone=None,
snapshot=snapshot, volume_type=None)
vol_delete.assert_called_once_with(self.context, volume['id'])
def test_snapshot_attach_volume(self):
@ -1486,8 +1486,9 @@ class TestDriverBlockDevice(test.NoDBTestCase):
self.virt_driver)
vol_create.assert_called_once_with(
self.context, test_bdm.volume_size, '', '', snapshot,
volume_type=expected_volume_type, availability_zone='test-az')
self.context, test_bdm.volume_size, '', '',
availability_zone='test-az', snapshot=snapshot,
volume_type=expected_volume_type)
vol_attach.assert_called_once_with(
self.context, instance, self.volume_api, self.virt_driver)
self.assertEqual('fake-volume-id-2', test_bdm.volume_id)

View File

@ -354,6 +354,20 @@ class DriverVolumeBlockDevice(DriverBlockDevice):
except exception.CinderAPIVersionNotAvailable:
return volume_api.get(context, volume_id)
def _create_volume(self, context, instance, volume_api, size,
wait_func=None, **create_kwargs):
av_zone = _get_volume_create_az_value(instance)
name = create_kwargs.pop('name', '')
description = create_kwargs.pop('description', '')
vol = volume_api.create(
context, size, name, description, volume_type=self.volume_type,
availability_zone=av_zone, **create_kwargs)
if wait_func:
self._call_wait_func(context, wait_func, volume_api, vol['id'])
return vol
def _do_detach(self, context, instance, volume_api, virt_driver,
attachment_id=None, destroy_bdm=False):
"""Private method that actually does the detach.
@ -719,14 +733,11 @@ class DriverVolSnapshotBlockDevice(DriverVolumeBlockDevice):
virt_driver, wait_func=None):
if not self.volume_id:
av_zone = _get_volume_create_az_value(instance)
snapshot = volume_api.get_snapshot(context,
self.snapshot_id)
vol = volume_api.create(context, self.volume_size, '', '',
snapshot, volume_type=self.volume_type,
availability_zone=av_zone)
if wait_func:
self._call_wait_func(context, wait_func, volume_api, vol['id'])
vol = self._create_volume(
context, instance, volume_api, self.volume_size,
wait_func=wait_func, snapshot=snapshot)
self.volume_id = vol['id']
@ -746,13 +757,9 @@ class DriverVolImageBlockDevice(DriverVolumeBlockDevice):
def attach(self, context, instance, volume_api,
virt_driver, wait_func=None):
if not self.volume_id:
av_zone = _get_volume_create_az_value(instance)
vol = volume_api.create(context, self.volume_size,
'', '', image_id=self.image_id,
volume_type=self.volume_type,
availability_zone=av_zone)
if wait_func:
self._call_wait_func(context, wait_func, volume_api, vol['id'])
vol = self._create_volume(
context, instance, volume_api, self.volume_size,
wait_func=wait_func, image_id=self.image_id)
self.volume_id = vol['id']
@ -772,12 +779,9 @@ class DriverVolBlankBlockDevice(DriverVolumeBlockDevice):
virt_driver, wait_func=None):
if not self.volume_id:
vol_name = instance.uuid + '-blank-vol'
av_zone = _get_volume_create_az_value(instance)
vol = volume_api.create(context, self.volume_size, vol_name, '',
volume_type=self.volume_type,
availability_zone=av_zone)
if wait_func:
self._call_wait_func(context, wait_func, volume_api, vol['id'])
vol = self._create_volume(
context, instance, volume_api, self.volume_size,
wait_func=wait_func, name=vol_name)
self.volume_id = vol['id']