Merge "volume: Migrate 'volume group snapshot' commands to SDK"

This commit is contained in:
Zuul 2023-11-04 00:10:08 +00:00 committed by Gerrit Code Review
commit ce63fd88da
3 changed files with 120 additions and 89 deletions

View File

@ -21,7 +21,6 @@ from openstack.block_storage.v3 import extension as _extension
from openstack.block_storage.v3 import resource_filter as _filters from openstack.block_storage.v3 import resource_filter as _filters
from openstack.block_storage.v3 import volume as _volume from openstack.block_storage.v3 import volume as _volume
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
from openstackclient.tests.unit import fakes from openstackclient.tests.unit import fakes
from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes
from openstackclient.tests.unit import utils from openstackclient.tests.unit import utils
@ -91,6 +90,11 @@ class TestVolume(FakeClientMixin, utils.TestCommand):
endpoint=fakes.AUTH_URL, token=fakes.AUTH_TOKEN endpoint=fakes.AUTH_URL, token=fakes.AUTH_TOKEN
) )
# avoid circular imports
from openstackclient.tests.unit.compute.v2 import (
fakes as compute_fakes,
)
self.app.client_manager.compute = compute_fakes.FakeComputev2Client( self.app.client_manager.compute = compute_fakes.FakeComputev2Client(
endpoint=fakes.AUTH_URL, endpoint=fakes.AUTH_URL,
token=fakes.AUTH_TOKEN, token=fakes.AUTH_TOKEN,

View File

@ -10,28 +10,32 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from cinderclient import api_versions from unittest import mock
from keystoneauth1 import discover
from openstack.block_storage.v3 import group as _group
from openstack.block_storage.v3 import group_snapshot as _group_snapshot
from openstack.test import fakes as sdk_fakes
from openstack import utils as sdk_utils
from osc_lib import exceptions from osc_lib import exceptions
from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes
from openstackclient.volume.v3 import volume_group_snapshot from openstackclient.volume.v3 import volume_group_snapshot
class TestVolumeGroupSnapshot(volume_fakes.TestVolume): def fake_supports_microversion(mocked_version):
def setUp(self): def supports_microversion(adapter, microversion, raise_exception=False):
super().setUp() required = discover.normalize_version_number(microversion)
candidate = discover.normalize_version_number(mocked_version)
return discover.version_match(required, candidate)
self.volume_groups_mock = self.volume_client.groups return supports_microversion
self.volume_groups_mock.reset_mock()
self.volume_group_snapshots_mock = self.volume_client.group_snapshots
self.volume_group_snapshots_mock.reset_mock()
class TestVolumeGroupSnapshotCreate(TestVolumeGroupSnapshot): class TestVolumeGroupSnapshotCreate(volume_fakes.TestVolume):
fake_volume_group = volume_fakes.create_one_volume_group() fake_volume_group = sdk_fakes.generate_fake_resource(_group.Group)
fake_volume_group_snapshot = ( fake_volume_group_snapshot = sdk_fakes.generate_fake_resource(
volume_fakes.create_one_volume_group_snapshot() _group_snapshot.GroupSnapshot,
) )
columns = ( columns = (
@ -54,11 +58,11 @@ class TestVolumeGroupSnapshotCreate(TestVolumeGroupSnapshot):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.volume_groups_mock.get.return_value = self.fake_volume_group self.volume_sdk_client.find_group.return_value = self.fake_volume_group
self.volume_group_snapshots_mock.create.return_value = ( self.volume_sdk_client.create_group_snapshot.return_value = (
self.fake_volume_group_snapshot self.fake_volume_group_snapshot
) )
self.volume_group_snapshots_mock.get.return_value = ( self.volume_sdk_client.find_group_snapshot.return_value = (
self.fake_volume_group_snapshot self.fake_volume_group_snapshot
) )
@ -66,8 +70,9 @@ class TestVolumeGroupSnapshotCreate(TestVolumeGroupSnapshot):
self.app, None self.app, None
) )
def test_volume_group_snapshot_create(self): @mock.patch.object(sdk_utils, 'supports_microversion')
self.volume_client.api_version = api_versions.APIVersion('3.14') def test_volume_group_snapshot_create(self, mock_mv):
mock_mv.side_effect = fake_supports_microversion('3.14')
arglist = [ arglist = [
self.fake_volume_group.id, self.fake_volume_group.id,
@ -81,19 +86,22 @@ class TestVolumeGroupSnapshotCreate(TestVolumeGroupSnapshot):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.volume_groups_mock.get.assert_called_once_with( self.volume_sdk_client.find_group.assert_called_once_with(
self.fake_volume_group.id
)
self.volume_group_snapshots_mock.create.assert_called_once_with(
self.fake_volume_group.id, self.fake_volume_group.id,
None, ignore_missing=False,
None, details=False,
)
self.volume_sdk_client.create_group_snapshot.assert_called_once_with(
group_id=self.fake_volume_group.id,
name=None,
description=None,
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertCountEqual(self.data, data) self.assertCountEqual(self.data, data)
def test_volume_group_snapshot_create_with_options(self): @mock.patch.object(sdk_utils, 'supports_microversion')
self.volume_client.api_version = api_versions.APIVersion('3.14') def test_volume_group_snapshot_create_with_options(self, mock_mv):
mock_mv.side_effect = fake_supports_microversion('3.14')
arglist = [ arglist = [
self.fake_volume_group.id, self.fake_volume_group.id,
@ -111,19 +119,22 @@ class TestVolumeGroupSnapshotCreate(TestVolumeGroupSnapshot):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.volume_groups_mock.get.assert_called_once_with( self.volume_sdk_client.find_group.assert_called_once_with(
self.fake_volume_group.id
)
self.volume_group_snapshots_mock.create.assert_called_once_with(
self.fake_volume_group.id, self.fake_volume_group.id,
'foo', ignore_missing=False,
'hello, world', details=False,
)
self.volume_sdk_client.create_group_snapshot.assert_called_once_with(
group_id=self.fake_volume_group.id,
name='foo',
description='hello, world',
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertCountEqual(self.data, data) self.assertCountEqual(self.data, data)
def test_volume_group_snapshot_create_pre_v314(self): @mock.patch.object(sdk_utils, 'supports_microversion')
self.volume_client.api_version = api_versions.APIVersion('3.13') def test_volume_group_snapshot_create_pre_v314(self, mock_mv):
mock_mv.side_effect = fake_supports_microversion('3.13')
arglist = [ arglist = [
self.fake_volume_group.id, self.fake_volume_group.id,
@ -136,32 +147,36 @@ class TestVolumeGroupSnapshotCreate(TestVolumeGroupSnapshot):
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exc = self.assertRaises( exc = self.assertRaises(
exceptions.CommandError, self.cmd.take_action, parsed_args exceptions.CommandError,
self.cmd.take_action,
parsed_args,
) )
self.assertIn( self.assertIn(
'--os-volume-api-version 3.14 or greater is required', str(exc) '--os-volume-api-version 3.14 or greater is required',
str(exc),
) )
class TestVolumeGroupSnapshotDelete(TestVolumeGroupSnapshot): class TestVolumeGroupSnapshotDelete(volume_fakes.TestVolume):
fake_volume_group_snapshot = ( fake_volume_group_snapshot = sdk_fakes.generate_fake_resource(
volume_fakes.create_one_volume_group_snapshot() _group_snapshot.GroupSnapshot,
) )
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.volume_group_snapshots_mock.get.return_value = ( self.volume_sdk_client.find_group_snapshot.return_value = (
self.fake_volume_group_snapshot self.fake_volume_group_snapshot
) )
self.volume_group_snapshots_mock.delete.return_value = None self.volume_sdk_client.delete_group_snapshot.return_value = None
self.cmd = volume_group_snapshot.DeleteVolumeGroupSnapshot( self.cmd = volume_group_snapshot.DeleteVolumeGroupSnapshot(
self.app, None self.app, None
) )
def test_volume_group_snapshot_delete(self): @mock.patch.object(sdk_utils, 'supports_microversion')
self.volume_client.api_version = api_versions.APIVersion('3.14') def test_volume_group_snapshot_delete(self, mock_mv):
mock_mv.side_effect = fake_supports_microversion('3.14')
arglist = [ arglist = [
self.fake_volume_group_snapshot.id, self.fake_volume_group_snapshot.id,
@ -173,13 +188,14 @@ class TestVolumeGroupSnapshotDelete(TestVolumeGroupSnapshot):
result = self.cmd.take_action(parsed_args) result = self.cmd.take_action(parsed_args)
self.volume_group_snapshots_mock.delete.assert_called_once_with( self.volume_sdk_client.delete_group_snapshot.assert_called_once_with(
self.fake_volume_group_snapshot.id, self.fake_volume_group_snapshot.id,
) )
self.assertIsNone(result) self.assertIsNone(result)
def test_volume_group_snapshot_delete_pre_v314(self): @mock.patch.object(sdk_utils, 'supports_microversion')
self.volume_client.api_version = api_versions.APIVersion('3.13') def test_volume_group_snapshot_delete_pre_v314(self, mock_mv):
mock_mv.side_effect = fake_supports_microversion('3.13')
arglist = [ arglist = [
self.fake_volume_group_snapshot.id, self.fake_volume_group_snapshot.id,
@ -190,15 +206,23 @@ class TestVolumeGroupSnapshotDelete(TestVolumeGroupSnapshot):
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exc = self.assertRaises( exc = self.assertRaises(
exceptions.CommandError, self.cmd.take_action, parsed_args exceptions.CommandError,
self.cmd.take_action,
parsed_args,
) )
self.assertIn( self.assertIn(
'--os-volume-api-version 3.14 or greater is required', str(exc) '--os-volume-api-version 3.14 or greater is required',
str(exc),
) )
class TestVolumeGroupSnapshotList(TestVolumeGroupSnapshot): class TestVolumeGroupSnapshotList(volume_fakes.TestVolume):
fake_volume_group_snapshots = volume_fakes.create_volume_group_snapshots() fake_volume_group_snapshots = list(
sdk_fakes.generate_fake_resources(
_group_snapshot.GroupSnapshot,
count=3,
)
)
columns = ( columns = (
'ID', 'ID',
@ -217,7 +241,7 @@ class TestVolumeGroupSnapshotList(TestVolumeGroupSnapshot):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.volume_group_snapshots_mock.list.return_value = ( self.volume_sdk_client.group_snapshots.return_value = (
self.fake_volume_group_snapshots self.fake_volume_group_snapshots
) )
@ -225,8 +249,9 @@ class TestVolumeGroupSnapshotList(TestVolumeGroupSnapshot):
self.app, None self.app, None
) )
def test_volume_group_snapshot_list(self): @mock.patch.object(sdk_utils, 'supports_microversion')
self.volume_client.api_version = api_versions.APIVersion('3.14') def test_volume_group_snapshot_list(self, mock_mv):
mock_mv.side_effect = fake_supports_microversion('3.14')
arglist = [ arglist = [
'--all-projects', '--all-projects',
@ -238,16 +263,15 @@ class TestVolumeGroupSnapshotList(TestVolumeGroupSnapshot):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.volume_group_snapshots_mock.list.assert_called_once_with( self.volume_sdk_client.group_snapshots.assert_called_once_with(
search_opts={ all_projects=True,
'all_tenants': True,
},
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertCountEqual(tuple(self.data), data) self.assertCountEqual(tuple(self.data), data)
def test_volume_group_snapshot_list_pre_v314(self): @mock.patch.object(sdk_utils, 'supports_microversion')
self.volume_client.api_version = api_versions.APIVersion('3.13') def test_volume_group_snapshot_list_pre_v314(self, mock_mv):
mock_mv.side_effect = fake_supports_microversion('3.13')
arglist = [] arglist = []
verifylist = [ verifylist = [
@ -256,8 +280,11 @@ class TestVolumeGroupSnapshotList(TestVolumeGroupSnapshot):
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exc = self.assertRaises( exc = self.assertRaises(
exceptions.CommandError, self.cmd.take_action, parsed_args exceptions.CommandError,
self.cmd.take_action,
parsed_args,
) )
self.assertIn( self.assertIn(
'--os-volume-api-version 3.14 or greater is required', str(exc) '--os-volume-api-version 3.14 or greater is required',
str(exc),
) )

View File

@ -12,7 +12,7 @@
import logging import logging
from cinderclient import api_versions from openstack import utils as sdk_utils
from osc_lib.command import command from osc_lib.command import command
from osc_lib import exceptions from osc_lib import exceptions
from osc_lib import utils from osc_lib import utils
@ -75,22 +75,25 @@ class CreateVolumeGroupSnapshot(command.ShowOne):
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
volume_client = self.app.client_manager.volume volume_client = self.app.client_manager.sdk_connection.volume
if volume_client.api_version < api_versions.APIVersion('3.14'): if not sdk_utils.supports_microversion(volume_client, '3.14'):
msg = _( msg = _(
"--os-volume-api-version 3.14 or greater is required to " "--os-volume-api-version 3.14 or greater is required to "
"support the 'volume group snapshot create' command" "support the 'volume group snapshot create' command"
) )
raise exceptions.CommandError(msg) raise exceptions.CommandError(msg)
volume_group = utils.find_resource( group = volume_client.find_group(
volume_client.groups,
parsed_args.volume_group, parsed_args.volume_group,
ignore_missing=False,
details=False,
) )
snapshot = volume_client.group_snapshots.create( snapshot = volume_client.create_group_snapshot(
volume_group.id, parsed_args.name, parsed_args.description group_id=group.id,
name=parsed_args.name,
description=parsed_args.description,
) )
return _format_group_snapshot(snapshot) return _format_group_snapshot(snapshot)
@ -112,21 +115,22 @@ class DeleteVolumeGroupSnapshot(command.Command):
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
volume_client = self.app.client_manager.volume volume_client = self.app.client_manager.sdk_connection.volume
if volume_client.api_version < api_versions.APIVersion('3.14'): if not sdk_utils.supports_microversion(volume_client, '3.14'):
msg = _( msg = _(
"--os-volume-api-version 3.14 or greater is required to " "--os-volume-api-version 3.14 or greater is required to "
"support the 'volume group snapshot delete' command" "support the 'volume group snapshot delete' command"
) )
raise exceptions.CommandError(msg) raise exceptions.CommandError(msg)
snapshot = utils.find_resource( group_snapshot = volume_client.find_group_snapshot(
volume_client.group_snapshots,
parsed_args.snapshot, parsed_args.snapshot,
ignore_missing=False,
details=False,
) )
volume_client.group_snapshots.delete(snapshot.id) volume_client.delete_group_snapshot(group_snapshot.id)
class ListVolumeGroupSnapshot(command.Lister): class ListVolumeGroupSnapshot(command.Lister):
@ -161,20 +165,18 @@ class ListVolumeGroupSnapshot(command.Lister):
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
volume_client = self.app.client_manager.volume volume_client = self.app.client_manager.sdk_connection.volume
if volume_client.api_version < api_versions.APIVersion('3.14'): if not sdk_utils.supports_microversion(volume_client, '3.14'):
msg = _( msg = _(
"--os-volume-api-version 3.14 or greater is required to " "--os-volume-api-version 3.14 or greater is required to "
"support the 'volume group snapshot list' command" "support the 'volume group snapshot list' command"
) )
raise exceptions.CommandError(msg) raise exceptions.CommandError(msg)
search_opts = { groups = volume_client.group_snapshots(
'all_tenants': parsed_args.all_projects, all_projects=parsed_args.all_projects,
} )
groups = volume_client.group_snapshots.list(search_opts=search_opts)
column_headers = ( column_headers = (
'ID', 'ID',
@ -209,21 +211,19 @@ class ShowVolumeGroupSnapshot(command.ShowOne):
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
volume_client = self.app.client_manager.volume volume_client = self.app.client_manager.sdk_connection.volume
if volume_client.api_version < api_versions.APIVersion('3.14'): if not sdk_utils.supports_microversion(volume_client, '3.14'):
msg = _( msg = _(
"--os-volume-api-version 3.14 or greater is required to " "--os-volume-api-version 3.14 or greater is required to "
"support the 'volume group snapshot show' command" "support the 'volume group snapshot show' command"
) )
raise exceptions.CommandError(msg) raise exceptions.CommandError(msg)
snapshot = utils.find_resource( group_snapshot = volume_client.find_group_snapshot(
volume_client.group_snapshots,
parsed_args.snapshot, parsed_args.snapshot,
ignore_missing=False,
details=True,
) )
# TODO(stephenfin): Do we need this? return _format_group_snapshot(group_snapshot)
snapshot = volume_client.groups.show(snapshot.id)
return _format_group_snapshot(snapshot)