From 17298888570a40a65ea2ce4fbfc970642a77fc7d Mon Sep 17 00:00:00 2001 From: Maari Tamm Date: Thu, 12 Dec 2019 21:33:35 +0000 Subject: [PATCH] Implement osc share set/unset commands In this patch set we add the implementation for openstack share set and openstack share unset command. These commands are used to change share properties and replace the manila update and manila metadata commands. Partially-implements: bp openstack-client-support Change-Id: Ic09802fa5dc9c7f0180341397c2740ac0cadbcd4 --- manilaclient/osc/v2/share.py | 147 ++++++++++++++++++- manilaclient/tests/unit/osc/v2/test_share.py | 146 ++++++++++++++++++ setup.cfg | 2 + 3 files changed, 294 insertions(+), 1 deletion(-) diff --git a/manilaclient/osc/v2/share.py b/manilaclient/osc/v2/share.py index b76dd0471..2fa7088c0 100644 --- a/manilaclient/osc/v2/share.py +++ b/manilaclient/osc/v2/share.py @@ -146,9 +146,11 @@ class CreateShare(command.ShowOne): ) parser.add_argument( '--public', + metavar='', default=False, help=_('Level of visibility for share. ' - 'Defines whether other tenants are able to see it or not.') + 'Defines whether other tenants are able to see it or not. ' + '(Default = False)') ) parser.add_argument( '--share-type', @@ -543,3 +545,146 @@ class ShowShare(command.ShowOne): data.pop("shares_type", None) return self.dict2columns(data) + + +class SetShare(command.Command): + """Set share properties.""" + _description = _("Set share properties") + + def get_parser(self, prog_name): + parser = super(SetShare, self).get_parser(prog_name) + parser.add_argument( + 'share', + metavar="", + help=_('Share to modify (name or ID)') + ) + # 'metadata' --> 'properties' + parser.add_argument( + "--property", + metavar="", + default={}, + action=parseractions.KeyValueAction, + help=_("Set a property to this share " + "(repeat option to set multiple properties)"), + ) + parser.add_argument( + '--name', + metavar="", + default=None, + help=_('New share name. (Default=None)') + ) + parser.add_argument( + '--description', + metavar='', + default=None, + help=_('New share description. (Default=None)') + ) + parser.add_argument( + '--public', + metavar='', + help=_('Level of visibility for share. ' + 'Defines whether other tenants are able to see it or not. ') + ) + return parser + + def take_action(self, parsed_args): + share_client = self.app.client_manager.share + share_obj = apiutils.find_resource(share_client.shares, + parsed_args.share) + result = 0 + + if parsed_args.property: + try: + share_client.shares.set_metadata( + share_obj.id, parsed_args.property) + except Exception as e: + LOG.error(_("Failed to set share properties " + "'%(properties)s': %(exception)s"), + {'properties': parsed_args.property, + 'exception': e}) + result += 1 + + kwargs = {} + if parsed_args.name is not None: + kwargs['display_name'] = parsed_args.name + if parsed_args.description is not None: + kwargs['display_description'] = parsed_args.description + if parsed_args.public is not None: + kwargs['is_public'] = parsed_args.public + if kwargs: + try: + share_client.shares.update(share_obj.id, **kwargs) + except Exception as e: + LOG.error(_("Failed to update share display name, visibility " + "or display description: %s"), e) + result += 1 + + if result > 0: + raise exceptions.CommandError(_("One or more of the " + "set operations failed")) + + +class UnsetShare(command.Command): + """Unset share properties.""" + _description = _("Unset share properties") + + def get_parser(self, prog_name): + parser = super(UnsetShare, self).get_parser(prog_name) + parser.add_argument( + 'share', + metavar="", + help=_('Share to modify (name or ID)') + ) + # 'metadata' --> 'properties' + parser.add_argument( + '--property', + metavar='', + action='append', + help=_('Remove a property from share ' + '(repeat option to remove multiple properties)'), + ) + parser.add_argument( + '--name', + action='store_true', + help=_('Unset share name.') + ) + parser.add_argument( + '--description', + action='store_true', + help=_('Unset share description.') + ) + return parser + + def take_action(self, parsed_args): + share_client = self.app.client_manager.share + share_obj = apiutils.find_resource(share_client.shares, + parsed_args.share) + result = 0 + kwargs = {} + if parsed_args.name: + kwargs['display_name'] = None + if parsed_args.description: + kwargs['display_description'] = None + if kwargs: + try: + share_client.shares.update(share_obj.id, **kwargs) + except Exception as e: + LOG.error(_("Failed to unset share display name " + "or display description"), e) + result += 1 + + if parsed_args.property: + for key in parsed_args.property: + try: + share_client.shares.delete_metadata( + share_obj.id, [key]) + except Exception as e: + LOG.error(_("Failed to unset share property " + "'%(key)s': %(e)s"), + {'key': key, 'e': e}) + result += 1 + + if result > 0: + raise exceptions.CommandError(_( + "One or more of the " + "unset operations failed")) diff --git a/manilaclient/tests/unit/osc/v2/test_share.py b/manilaclient/tests/unit/osc/v2/test_share.py index 026544af1..5c8a9f643 100644 --- a/manilaclient/tests/unit/osc/v2/test_share.py +++ b/manilaclient/tests/unit/osc/v2/test_share.py @@ -15,6 +15,7 @@ import argparse import mock +import uuid from mock import call @@ -862,3 +863,148 @@ class TestShareShow(TestShare): self.assertEqual( manila_fakes.FakeShare.get_share_data(self._share), data) + + +class TestShareSet(TestShare): + + def setUp(self): + super(TestShareSet, self).setUp() + + self._share = manila_fakes.FakeShare.create_one_share() + self.shares_mock.get.return_value = self._share + + # Get the command object to test + self.cmd = osc_shares.SetShare(self.app, None) + + def test_share_set_property(self): + arglist = [ + '--property', 'Zorilla=manila', + self._share.id, + ] + verifylist = [ + ('property', {'Zorilla': 'manila'}), + ('share', self._share.id) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + self.shares_mock.set_metadata.assert_called_with( + self._share.id, + {'Zorilla': 'manila'}) + + def test_share_set_name(self): + new_name = uuid.uuid4().hex + arglist = [ + '--name', new_name, + self._share.id, + ] + verifylist = [ + ('name', new_name), + ('share', self._share.id) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + self.shares_mock.update.assert_called_with( + self._share.id, + display_name=parsed_args.name) + + def test_share_set_description(self): + new_description = uuid.uuid4().hex + arglist = [ + '--description', new_description, + self._share.id, + ] + verifylist = [ + ('description', new_description), + ('share', self._share.id) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + self.shares_mock.update.assert_called_with( + self._share.id, + display_description=parsed_args.description) + + def test_share_set_visibility(self): + arglist = [ + '--public', 'true', + self._share.id, + ] + verifylist = [ + ('public', 'true'), + ('share', self._share.id) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + self.shares_mock.update.assert_called_with( + self._share.id, + is_public='true') + + +class TestShareUnset(TestShare): + + def setUp(self): + super(TestShareUnset, self).setUp() + + self._share = manila_fakes.FakeShare.create_one_share() + self.shares_mock.get.return_value = self._share + + # Get the command objects to test + self.cmd = osc_shares.UnsetShare(self.app, None) + + def test_share_unset_property(self): + arglist = [ + '--property', 'Manila', + self._share.id, + ] + verifylist = [ + ('property', ['Manila']), + ('share', self._share.id) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + self.shares_mock.delete_metadata.assert_called_with( + self._share.id, + parsed_args.property) + + def test_share_unset_name(self): + arglist = [ + '--name', + self._share.id, + ] + verifylist = [ + ('name', True), + ('share', self._share.id) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + self.shares_mock.update.assert_called_with( + self._share.id, + display_name=None) + + def test_share_unset_description(self): + arglist = [ + '--description', + self._share.id, + ] + verifylist = [ + ('description', True), + ('share', self._share.id) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + self.shares_mock.update.assert_called_with( + self._share.id, + display_description=None) diff --git a/setup.cfg b/setup.cfg index b9892442d..51d133657 100644 --- a/setup.cfg +++ b/setup.cfg @@ -42,6 +42,8 @@ openstack.share.v2 = share_create = manilaclient.osc.v2.share:CreateShare share_delete = manilaclient.osc.v2.share:DeleteShare share_show = manilaclient.osc.v2.share:ShowShare + share_set = manilaclient.osc.v2.share:SetShare + share_unset = manilaclient.osc.v2.share:UnsetShare [wheel] universal = 1