[OSC] Implement Share Revert Command

This patch adds the 'openstack share revert' command,
which implements the same functionality as 'manila revert-to-snapshot'
and can be used to revert a share to a specified snapshot.

Partially-implements bp openstack-client-support
Change-Id: I896f2bb4ae256dda637768672f833573f5c2475d
This commit is contained in:
Maari Tamm 2021-01-25 17:54:36 +00:00
parent c7bcec2969
commit 17e5d63651
4 changed files with 77 additions and 0 deletions

View File

@ -44,6 +44,8 @@ shares
.. autoprogram-cliff:: openstack.share.v2
:command: share export location list
.. autoprogram-cliff:: openstack.share.v2
:command: share revert
==================
share access rules

View File

@ -1051,3 +1051,32 @@ class ShowShareProperties(command.ShowOne):
share_properties = share_client.shares.get_metadata(share)
return self.dict2columns(share_properties._info)
class RevertShare(command.Command):
"""Revert a share to snapshot."""
_description = _("Revert a share to the specified snapshot.")
def get_parser(self, prog_name):
parser = super(RevertShare, self).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 manila.')
)
return parser
def take_action(self, parsed_args):
share_client = self.app.client_manager.share
snapshot = apiutils.find_resource(share_client.share_snapshots,
parsed_args.snapshot)
share = apiutils.find_resource(share_client.shares,
snapshot.share_id)
try:
share.revert_to_snapshot(snapshot)
except Exception as e:
raise exceptions.CommandError(_(
"Failed to revert share to snapshot: %s" % (e)))

View File

@ -1785,3 +1785,48 @@ class TestShowShareProperties(TestShare):
self.assertCountEqual(self.columns, columns)
self.assertCountEqual(self.datalist, data)
class TestShareRevert(TestShare):
def setUp(self):
super(TestShareRevert, self).setUp()
self.share = manila_fakes.FakeShare.create_one_share(
attrs={'revert_to_snapshot_support': True},
methods={'revert_to_snapshot': None}
)
self.share_snapshot = (
manila_fakes.FakeShareSnapshot.create_one_snapshot(
attrs={'share_id': self.share.id}))
self.shares_mock.get.return_value = self.share
self.snapshots_mock.get.return_value = self.share_snapshot
self.cmd = osc_shares.RevertShare(self.app, None)
def test_share_revert(self):
arglist = [
self.share_snapshot.id
]
verifylist = [
('snapshot', self.share_snapshot.id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.shares_mock.get.assert_called_with(self.share_snapshot.share_id)
self.share.revert_to_snapshot.assert_called_with(self.share_snapshot)
self.assertIsNone(result)
def test_share_revert_exception(self):
arglist = [
self.share_snapshot.id
]
verifylist = [
('snapshot', self.share_snapshot.id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.share.revert_to_snapshot.side_effect = Exception()
self.assertRaises(
osc_exceptions.CommandError, self.cmd.take_action, parsed_args)

View File

@ -47,6 +47,7 @@ openstack.share.v2 =
share_export_location_show = manilaclient.osc.v2.share:ShareExportLocationShow
share_export_location_list = manilaclient.osc.v2.share:ShareExportLocationList
share_properties_show = manilaclient.osc.v2.share:ShowShareProperties
share_revert = manilaclient.osc.v2.share:RevertShare
share_access_create = manilaclient.osc.v2.share_access_rules:ShareAccessAllow
share_access_delete = manilaclient.osc.v2.share_access_rules:ShareAccessDeny
share_access_list = manilaclient.osc.v2.share_access_rules:ListShareAccess