Implement OSC share resize command

In this patch we add the 'openstack share resize' command.
This command combines 'manila shrink' and 'manila extend' commands
into one and can be used to change the size of a share.

usage: openstack share resize <share> <new-size>

Change-Id: I052977f689ad4c1e9fb5f63d773519cd880c5b6c
Partially-implements: bp openstack-client-support
This commit is contained in:
Maari Tamm 2020-06-02 19:31:46 +00:00 committed by Victoria Martinez de la Cruz
parent d65d7f05a7
commit 126c82a2ae
3 changed files with 158 additions and 0 deletions

View File

@ -687,3 +687,49 @@ class UnsetShare(command.Command):
raise exceptions.CommandError(_( raise exceptions.CommandError(_(
"One or more of the " "One or more of the "
"unset operations failed")) "unset operations failed"))
class ResizeShare(command.Command):
"""Resize a share"""
_description = _("Resize a share")
def get_parser(self, prog_name):
parser = super(ResizeShare, self).get_parser(prog_name)
parser.add_argument(
'share',
metavar="<share>",
help=_('Name or ID of share to resize')
)
parser.add_argument(
'new_size',
metavar="<new-size>",
type=int,
help=_('New size of share, in GiBs')
)
return parser
def take_action(self, parsed_args):
share_client = self.app.client_manager.share
share = apiutils.find_resource(share_client.shares,
parsed_args.share)
share_size = share._info['size']
new_size = parsed_args.new_size
if share_size > new_size:
try:
share_client.shares.shrink(share, new_size)
except Exception as e:
raise exceptions.CommandError(_(
"Share resize failed: %s" % e
))
elif share_size < new_size:
try:
share_client.shares.extend(share, new_size)
except Exception as e:
raise exceptions.CommandError(_(
"Share resize failed: %s" % e
))
else:
raise exceptions.CommandError(_(
"Share size is already at %s GiBs" % new_size
))

View File

@ -20,6 +20,7 @@ import uuid
from mock import call from mock import call
from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes
from osc_lib import exceptions
from manilaclient.common import cliutils from manilaclient.common import cliutils
from manilaclient.osc.v2 import share as osc_shares from manilaclient.osc.v2 import share as osc_shares
@ -1008,3 +1009,113 @@ class TestShareUnset(TestShare):
self.shares_mock.update.assert_called_with( self.shares_mock.update.assert_called_with(
self._share.id, self._share.id,
display_description=None) display_description=None)
class TestResizeShare(TestShare):
def setUp(self):
super(TestResizeShare, self).setUp()
self._share = manila_fakes.FakeShare.create_one_share(
attrs={'size': 2})
self.shares_mock.get.return_value = self._share
# Get the command objects to test
self.cmd = osc_shares.ResizeShare(self.app, None)
def test_share_shrink(self):
arglist = [
self._share.id,
'1'
]
verifylist = [
('share', self._share.id),
('new_size', 1)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.shares_mock.shrink.assert_called_with(
self._share,
1
)
def test_share_shrink_exception(self):
arglist = [
self._share.id,
'1'
]
verifylist = [
('share', self._share.id),
('new_size', 1)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.shares_mock.shrink.side_effect = Exception()
self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
def test_share_extend(self):
arglist = [
self._share.id,
'3'
]
verifylist = [
('share', self._share.id),
('new_size', 3)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.shares_mock.extend.assert_called_with(
self._share,
3
)
def test_share_extend_exception(self):
arglist = [
self._share.id,
'3'
]
verifylist = [
('share', self._share.id),
('new_size', 3)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.shares_mock.extend.side_effect = Exception()
self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
def test_share_resize_already_required_size(self):
arglist = [
self._share.id,
'2'
]
verifylist = [
('share', self._share.id),
('new_size', 2)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args
)
def test_share_missing_args(self):
arglist = [
self._share.id
]
verifylist = [
('share', self._share.id)
]
self.assertRaises(osc_utils.ParserException,
self.check_parser, self.cmd, arglist, verifylist)

View File

@ -41,6 +41,7 @@ openstack.share.v2 =
share_show = manilaclient.osc.v2.share:ShowShare share_show = manilaclient.osc.v2.share:ShowShare
share_set = manilaclient.osc.v2.share:SetShare share_set = manilaclient.osc.v2.share:SetShare
share_unset = manilaclient.osc.v2.share:UnsetShare share_unset = manilaclient.osc.v2.share:UnsetShare
share_resize = manilaclient.osc.v2.share:ResizeShare
share_access_create = manilaclient.osc.v2.share_access_rules:ShareAccessAllow share_access_create = manilaclient.osc.v2.share_access_rules:ShareAccessAllow
share_access_delete = manilaclient.osc.v2.share_access_rules:ShareAccessDeny share_access_delete = manilaclient.osc.v2.share_access_rules:ShareAccessDeny
share_access_list = manilaclient.osc.v2.share_access_rules:ListShareAccess share_access_list = manilaclient.osc.v2.share_access_rules:ListShareAccess