[OSC] Implement Share Restore Command
This patch adds the 'openstack share restore' command, which implements same functionality as 'manila restore' and can be used to restore a share from the recycle bin Partially-implements: bp openstack-client-support Change-Id: I30f3c36c00d41f8321654285e2ca4455e5d0f8f9
This commit is contained in:
parent
992a94247a
commit
8f53399e15
@ -47,6 +47,9 @@ shares
|
||||
.. autoprogram-cliff:: openstack.share.v2
|
||||
:command: share revert
|
||||
|
||||
.. autoprogram-cliff:: openstack.share.v2
|
||||
:command: share restore
|
||||
|
||||
===============
|
||||
share instances
|
||||
===============
|
||||
|
@ -1428,3 +1428,43 @@ class ShareMigrationShow(command.ShowOne):
|
||||
parsed_args.share)
|
||||
result = share.migration_get_progress()
|
||||
return self.dict2columns(result[1])
|
||||
|
||||
|
||||
class RestoreShare(command.Command):
|
||||
"""Restore one or more shares from recycle bin"""
|
||||
|
||||
_description = _("Restores this share or more shares from the recycle bin")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(RestoreShare, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'share',
|
||||
metavar="<share>",
|
||||
nargs="+",
|
||||
help=_('Name or ID of the share(s)')
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
share_client = self.app.client_manager.share
|
||||
if share_client.api_version >= api_versions.APIVersion('2.69'):
|
||||
failure_count = 0
|
||||
|
||||
for share in parsed_args.share:
|
||||
try:
|
||||
share_client.shares.restore(share)
|
||||
except Exception as e:
|
||||
failure_count += 1
|
||||
LOG.error(_("Failed to restore share with "
|
||||
"name or ID '%(share)s': %(e)s"),
|
||||
{'share': share, 'e': e})
|
||||
if failure_count > 0:
|
||||
total = len(parsed_args.share)
|
||||
msg = (f"Failed to restore {failure_count} out of "
|
||||
f"{total} shares.")
|
||||
msg = _(msg)
|
||||
raise exceptions.CommandError(msg)
|
||||
else:
|
||||
raise exceptions.CommandError(
|
||||
"Restoring a share from the recycle bin is only "
|
||||
"available with manila API version >= 2.69")
|
||||
|
@ -182,3 +182,21 @@ class SharesCLITest(base.OSCClientTestBase):
|
||||
export_location_ids = [el['ID'] for el in share_export_locations]
|
||||
for share_export in result_export_locations:
|
||||
self.assertIn(share_export["ID"], export_location_ids)
|
||||
|
||||
def test_openstack_share_restore(self):
|
||||
share = self.create_share(name='test_share')
|
||||
result1 = self.dict_result('share', f'show {share["id"]}')
|
||||
self.assertEqual(share['id'], result1['id'])
|
||||
self.assertEqual(share['name'], result1['name'])
|
||||
|
||||
self.openstack(f'share delete {share["id"]} --soft')
|
||||
self.check_object_deleted('share', share['id'])
|
||||
shares_list_after_delete = self.listing_result('share', 'list')
|
||||
self.assertNotIn(
|
||||
share['id'], [item['ID'] for item in shares_list_after_delete])
|
||||
|
||||
self.openstack(f'share restore {share["id"]} ')
|
||||
|
||||
shares_list_after_restore = self.listing_result('share', 'list')
|
||||
self.assertIn(
|
||||
share['id'], [item['ID'] for item in shares_list_after_restore])
|
||||
|
@ -2099,3 +2099,41 @@ class TestShareMigrationShow(TestShare):
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
self.cmd.take_action(parsed_args)
|
||||
self._share.migration_get_progress.assert_called
|
||||
|
||||
|
||||
class TestShareRestore(TestShare):
|
||||
|
||||
def setUp(self):
|
||||
super(TestShareRestore, self).setUp()
|
||||
|
||||
self.share = manila_fakes.FakeShare.create_one_share(
|
||||
methods={'restore': None}
|
||||
)
|
||||
self.shares_mock.get.return_value = self.share
|
||||
self.cmd = osc_shares.RestoreShare(self.app, None)
|
||||
|
||||
def test_share_restore(self):
|
||||
arglist = [
|
||||
self.share.name
|
||||
]
|
||||
verifylist = [
|
||||
('share', [self.share.name])
|
||||
]
|
||||
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)
|
||||
self.share.restore.assert_called_with(self.share)
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_share_restore_exception(self):
|
||||
arglist = [
|
||||
self.share.name
|
||||
]
|
||||
verifylist = [
|
||||
('share', [self.share.name])
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
self.share.restore.side_effect = Exception()
|
||||
self.assertRaises(
|
||||
osc_exceptions.CommandError, self.cmd.take_action, parsed_args)
|
||||
|
@ -51,6 +51,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_restore = manilaclient.osc.v2.share:RestoreShare
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user