summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwhoami-rajat <rajatdhasmana@gmail.com>2023-01-10 13:37:41 +0530
committerwhoami-rajat <rajatdhasmana@gmail.com>2023-02-16 04:33:40 +0000
commite7ebf7544b7bd0b014e9dffa27d6c4c63f078f6e (patch)
treeee86dd9517b64975edabe170e8b3067d4ddc581b
parent1e0880eda83ed7d338e5d09e98b9d1982068cd0b (diff)
downloadpython-openstackclient-e7ebf7544b7bd0b014e9dffa27d6c4c63f078f6e.tar.gz
Add volume revert command
This command allows users to revert a volume to a given snapshot. Change-Id: If35ee394d654f5264558a281c835affff524ca50
-rw-r--r--doc/source/cli/command-objects/volume.rst3
-rw-r--r--doc/source/cli/data/cinder.csv2
-rw-r--r--openstackclient/tests/unit/volume/v3/test_volume.py58
-rw-r--r--openstackclient/volume/v3/volume.py33
-rw-r--r--releasenotes/notes/add-volume-revert-command-1c8f695420acbe7e.yaml5
-rw-r--r--setup.cfg1
6 files changed, 101 insertions, 1 deletions
diff --git a/doc/source/cli/command-objects/volume.rst b/doc/source/cli/command-objects/volume.rst
index 5bfa547a..9b491772 100644
--- a/doc/source/cli/command-objects/volume.rst
+++ b/doc/source/cli/command-objects/volume.rst
@@ -393,3 +393,6 @@ Block Storage v3
.. autoprogram-cliff:: openstack.volume.v3
:command: volume summary
+
+ .. autoprogram-cliff:: openstack.volume.v3
+ :command: volume revert
diff --git a/doc/source/cli/data/cinder.csv b/doc/source/cli/data/cinder.csv
index 9b0f7636..0656a6a3 100644
--- a/doc/source/cli/data/cinder.csv
+++ b/doc/source/cli/data/cinder.csv
@@ -100,7 +100,7 @@ readonly-mode-update,volume set --read-only-mode | --read-write-mode,Updates vol
rename,volume set --name,Renames a volume.
reset-state,volume set --state,Explicitly updates the volume state.
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-enable,volume service set --enable,Enables the service.
service-get-log,,(Supported by API versions 3.32 - 3.latest)
diff --git a/openstackclient/tests/unit/volume/v3/test_volume.py b/openstackclient/tests/unit/volume/v3/test_volume.py
index 783f0852..ed72bfa1 100644
--- a/openstackclient/tests/unit/volume/v3/test_volume.py
+++ b/openstackclient/tests/unit/volume/v3/test_volume.py
@@ -13,10 +13,12 @@
#
import copy
+from unittest import mock
from cinderclient import api_versions
from osc_lib.cli import format_columns
from osc_lib import exceptions
+from osc_lib import utils
from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes
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,
format_columns.DictColumn(combine_meta))
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)
diff --git a/openstackclient/volume/v3/volume.py b/openstackclient/volume/v3/volume.py
index 07bd434f..4b159688 100644
--- a/openstackclient/volume/v3/volume.py
+++ b/openstackclient/volume/v3/volume.py
@@ -79,3 +79,36 @@ class VolumeSummary(command.ShowOne):
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)
diff --git a/releasenotes/notes/add-volume-revert-command-1c8f695420acbe7e.yaml b/releasenotes/notes/add-volume-revert-command-1c8f695420acbe7e.yaml
new file mode 100644
index 00000000..2832b888
--- /dev/null
+++ b/releasenotes/notes/add-volume-revert-command-1c8f695420acbe7e.yaml
@@ -0,0 +1,5 @@
+---
+features:
+ - |
+ Added ``volume revert`` command that reverts
+ the volume to the given snapshot.
diff --git a/setup.cfg b/setup.cfg
index 6a6f8769..4828d30f 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -821,3 +821,4 @@ openstack.volume.v3 =
volume_transfer_request_show = openstackclient.volume.v2.volume_transfer_request:ShowTransferRequest
volume_summary = openstackclient.volume.v3.volume:VolumeSummary
+ volume_revert = openstackclient.volume.v3.volume:VolumeRevertToSnapshot