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
This commit is contained in:
Maari Tamm
2019-12-12 21:33:35 +00:00
parent 6bccfc0d6e
commit 1729888857
3 changed files with 294 additions and 1 deletions

View File

@@ -146,9 +146,11 @@ class CreateShare(command.ShowOne):
)
parser.add_argument(
'--public',
metavar='<public>',
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="<share>",
help=_('Share to modify (name or ID)')
)
# 'metadata' --> 'properties'
parser.add_argument(
"--property",
metavar="<key=value>",
default={},
action=parseractions.KeyValueAction,
help=_("Set a property to this share "
"(repeat option to set multiple properties)"),
)
parser.add_argument(
'--name',
metavar="<name>",
default=None,
help=_('New share name. (Default=None)')
)
parser.add_argument(
'--description',
metavar='<description>',
default=None,
help=_('New share description. (Default=None)')
)
parser.add_argument(
'--public',
metavar='<public>',
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="<share>",
help=_('Share to modify (name or ID)')
)
# 'metadata' --> 'properties'
parser.add_argument(
'--property',
metavar='<key>',
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"))

View File

@@ -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)

View File

@@ -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