Add volume revert command

This command allows users to revert a volume to a given snapshot.

Change-Id: If35ee394d654f5264558a281c835affff524ca50
This commit is contained in:
whoami-rajat 2023-01-10 13:37:41 +05:30
parent 1e0880eda8
commit e7ebf7544b
6 changed files with 101 additions and 1 deletions

View File

@ -393,3 +393,6 @@ Block Storage v3
.. autoprogram-cliff:: openstack.volume.v3 .. autoprogram-cliff:: openstack.volume.v3
:command: volume summary :command: volume summary
.. autoprogram-cliff:: openstack.volume.v3
:command: volume revert

View File

@ -100,7 +100,7 @@ readonly-mode-update,volume set --read-only-mode | --read-write-mode,Updates vol
rename,volume set --name,Renames a volume. rename,volume set --name,Renames a volume.
reset-state,volume set --state,Explicitly updates the volume state. reset-state,volume set --state,Explicitly updates the volume state.
retype,volume type set --type,Changes the volume type for a volume. retype,volume type set --type,Changes the volume type for a volume.
revert-to-snapshot,,Revert a volume to the specified snapshot. (Supported by API versions 3.40 - 3.latest) revert-to-snapshot,volume revert,Revert a volume to the specified snapshot. (Supported by API versions 3.40 - 3.latest)
service-disable,volume service set --disable,Disables the service. service-disable,volume service set --disable,Disables the service.
service-enable,volume service set --enable,Enables the service. service-enable,volume service set --enable,Enables the service.
service-get-log,,(Supported by API versions 3.32 - 3.latest) service-get-log,,(Supported by API versions 3.32 - 3.latest)

1 absolute-limits limits show --absolute Lists absolute limits for a user.
100 rename volume set --name Renames a volume.
101 reset-state volume set --state Explicitly updates the volume state.
102 retype volume type set --type Changes the volume type for a volume.
103 revert-to-snapshot volume revert Revert a volume to the specified snapshot. (Supported by API versions 3.40 - 3.latest)
104 service-disable volume service set --disable Disables the service.
105 service-enable volume service set --enable Enables the service.
106 service-get-log (Supported by API versions 3.32 - 3.latest)

View File

@ -13,10 +13,12 @@
# #
import copy import copy
from unittest import mock
from cinderclient import api_versions from cinderclient import api_versions
from osc_lib.cli import format_columns from osc_lib.cli import format_columns
from osc_lib import exceptions from osc_lib import exceptions
from osc_lib import utils
from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes
from openstackclient.volume.v3 import volume from openstackclient.volume.v3 import volume
@ -119,3 +121,59 @@ class TestVolumeSummary(volume_fakes.TestVolume):
self.mock_vol_1.size + self.mock_vol_2.size, self.mock_vol_1.size + self.mock_vol_2.size,
format_columns.DictColumn(combine_meta)) format_columns.DictColumn(combine_meta))
self.assertCountEqual(datalist, tuple(data)) self.assertCountEqual(datalist, tuple(data))
class TestVolumeRevertToSnapshot(volume_fakes.TestVolume):
def setUp(self):
super().setUp()
self.volumes_mock = self.app.client_manager.volume.volumes
self.volumes_mock.reset_mock()
self.snapshots_mock = self.app.client_manager.volume.volume_snapshots
self.snapshots_mock.reset_mock()
self.mock_volume = volume_fakes.create_one_volume()
self.mock_snapshot = volume_fakes.create_one_snapshot(
attrs={'volume_id': self.volumes_mock.id})
# Get the command object to test
self.cmd = volume.VolumeRevertToSnapshot(self.app, None)
def test_volume_revert_to_snapshot_pre_340(self):
arglist = [
self.mock_snapshot.id,
]
verifylist = [
('snapshot', self.mock_snapshot.id),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exc = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn(
'--os-volume-api-version 3.40 or greater is required',
str(exc))
def test_volume_revert_to_snapshot(self):
self.app.client_manager.volume.api_version = \
api_versions.APIVersion('3.40')
arglist = [
self.mock_snapshot.id,
]
verifylist = [
('snapshot', self.mock_snapshot.id),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
find_mock_result = [self.mock_snapshot, self.mock_volume]
with mock.patch.object(utils, 'find_resource',
side_effect=find_mock_result) as find_mock:
self.cmd.take_action(parsed_args)
self.volumes_mock.revert_to_snapshot.assert_called_once_with(
volume=self.mock_volume,
snapshot=self.mock_snapshot,
)
self.assertEqual(2, find_mock.call_count)

View File

@ -79,3 +79,36 @@ class VolumeSummary(command.ShowOne):
formatters={'metadata': format_columns.DictColumn}, formatters={'metadata': format_columns.DictColumn},
), ),
) )
class VolumeRevertToSnapshot(command.Command):
_description = _("Revert a volume to a snapshot.")
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'snapshot',
metavar="<snapshot>",
help=_('Name or ID of the snapshot to restore. The snapshot must '
'be the most recent one known to cinder.'),
)
return parser
def take_action(self, parsed_args):
volume_client = self.app.client_manager.volume
if volume_client.api_version < api_versions.APIVersion('3.40'):
msg = _(
"--os-volume-api-version 3.40 or greater is required to "
"support the 'volume revert snapshot' command"
)
raise exceptions.CommandError(msg)
snapshot = utils.find_resource(
volume_client.volume_snapshots, parsed_args.snapshot)
volume = utils.find_resource(
volume_client.volumes, snapshot.volume_id)
volume_client.volumes.revert_to_snapshot(
volume=volume, snapshot=snapshot)

View File

@ -0,0 +1,5 @@
---
features:
- |
Added ``volume revert`` command that reverts
the volume to the given snapshot.

View File

@ -821,3 +821,4 @@ openstack.volume.v3 =
volume_transfer_request_show = openstackclient.volume.v2.volume_transfer_request:ShowTransferRequest volume_transfer_request_show = openstackclient.volume.v2.volume_transfer_request:ShowTransferRequest
volume_summary = openstackclient.volume.v3.volume:VolumeSummary volume_summary = openstackclient.volume.v3.volume:VolumeSummary
volume_revert = openstackclient.volume.v3.volume:VolumeRevertToSnapshot