Add CLI error notification in case there is no share type

Currently, when there is no default share type and try to create
a manila share without specifying a share type, the creation
request is sent and no CLI error notification is received.
This patch prevent sending the create request and provide
early feedback to CLI users.

Closes-Bug: #1960422

Change-Id: I66a8bcebe35e744f9796e3db44d6cedf2ada983f
(cherry picked from commit ded2303da8)
(cherry picked from commit 05982cef88)
(cherry picked from commit ea39ef8fc1)
This commit is contained in:
lkuchlan 2022-02-09 17:16:58 +02:00 committed by Goutham Pacha Ravi
parent ab673a2783
commit eee6ca937c
5 changed files with 70 additions and 9 deletions

View File

@ -22,6 +22,7 @@ from osc_lib import exceptions
from osc_lib import utils as oscutils
from manilaclient.common._i18n import _
from manilaclient.common.apiclient import exceptions as apiclient_exceptions
from manilaclient.common.apiclient import utils as apiutils
from manilaclient.common import cliutils
from manilaclient.osc import utils
@ -178,10 +179,17 @@ class CreateShare(command.ShowOne):
# TODO(s0ru): the table shows 'Field', 'Value'
share_client = self.app.client_manager.share
share_type = None
if parsed_args.share_type:
share_type = apiutils.find_resource(share_client.share_types,
parsed_args.share_type).id
else:
try:
share_type = apiutils.find_resource(
share_client.share_types, 'default').id
except apiclient_exceptions.CommandError:
msg = ("There is no default share type available. You must "
"pick a valid share type to create a share.")
raise exceptions.CommandError(msg)
share_network = None
if parsed_args.share_network:

View File

@ -58,6 +58,11 @@ class TestShareCreate(TestShare):
self.new_share = manila_fakes.FakeShare.create_one_share()
self.shares_mock.create.return_value = self.new_share
self.share_types_mock = self.app.client_manager.share.share_types
self.share_types_mock.reset_mock()
self.share_type = manila_fakes.FakeShareType.create_one_sharetype()
self.share_types_mock.get.return_value = self.share_type
# Get the command object to test
self.cmd = osc_shares.CreateShare(self.app, None)
@ -70,10 +75,12 @@ class TestShareCreate(TestShare):
arglist = [
self.new_share.share_proto,
str(self.new_share.size),
'--share-type', self.share_type.id,
]
verifylist = [
('share_proto', self.new_share.share_proto),
('size', self.new_share.size)
('size', self.new_share.size),
('share_type', self.share_type.id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -89,7 +96,7 @@ class TestShareCreate(TestShare):
share_group_id=None,
share_network=None,
share_proto=self.new_share.share_proto,
share_type=None,
share_type=self.share_type.id,
size=self.new_share.size,
snapshot_id=None
)
@ -113,12 +120,14 @@ class TestShareCreate(TestShare):
arglist = [
self.new_share.share_proto,
str(self.new_share.size),
'--share-type', self.share_type.id,
'--property', 'Manila=zorilla',
'--property', 'Zorilla=manila'
]
verifylist = [
('share_proto', self.new_share.share_proto),
('size', self.new_share.size),
('share_type', self.share_type.id),
('property', {'Manila': 'zorilla', 'Zorilla': 'manila'}),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -134,7 +143,7 @@ class TestShareCreate(TestShare):
share_group_id=None,
share_network=None,
share_proto=self.new_share.share_proto,
share_type=None,
share_type=self.share_type.id,
size=self.new_share.size,
snapshot_id=None
)
@ -146,6 +155,24 @@ class TestShareCreate(TestShare):
# we implement snapshot support in OSC
# def test_share_create_with_snapshot(self):
def test_create_share_with_no_existing_share_type(self):
arglist = [
self.new_share.share_proto,
str(self.new_share.size),
]
verifylist = [
('share_proto', self.new_share.share_proto),
('size', self.new_share.size),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.share_types_mock.get.side_effect = osc_exceptions.CommandError()
self.assertRaises(
osc_exceptions.CommandError,
self.cmd.take_action,
parsed_args)
class TestShareDelete(TestShare):

View File

@ -1948,13 +1948,16 @@ class ShellTest(test_utils.TestCase):
def test_create_share(self):
# Use only required fields
self.run_command("create nfs 1")
self.assert_called("POST", "/shares", body=self.create_share_body)
expected = self.create_share_body.copy()
expected['share']['share_type'] = 'test_type'
self.run_command("create nfs 1 --share-type test_type")
self.assert_called("POST", "/shares", body=expected)
def test_create_public_share(self):
expected = self.create_share_body.copy()
expected['share']['is_public'] = True
self.run_command("create --public nfs 1")
expected['share']['share_type'] = 'test_type'
self.run_command("create --public nfs 1 --share-type test_type")
self.assert_called("POST", "/shares", body=expected)
def test_create_with_share_network(self):
@ -1962,19 +1965,27 @@ class ShellTest(test_utils.TestCase):
sn = "fake-share-network"
with mock.patch.object(shell_v2, "_find_share_network",
mock.Mock(return_value=sn)):
self.run_command("create nfs 1 --share-network %s" % sn)
self.run_command("create nfs 1 --share-type test_type "
"--share-network %s" % sn)
expected = self.create_share_body.copy()
expected['share']['share_network_id'] = sn
expected['share']['share_type'] = 'test_type'
self.assert_called("POST", "/shares", body=expected)
shell_v2._find_share_network.assert_called_once_with(mock.ANY, sn)
def test_create_with_metadata(self):
# Except required fields added metadata
self.run_command("create nfs 1 --metadata key1=value1 key2=value2")
self.run_command("create nfs 1 --metadata key1=value1 key2=value2 "
"--share-type test_type")
expected = self.create_share_body.copy()
expected['share']['metadata'] = {"key1": "value1", "key2": "value2"}
expected['share']['share_type'] = 'test_type'
self.assert_called("POST", "/shares", body=expected)
def test_create_share_with_no_existing_share_type(self):
self.assertRaises(
exceptions.CommandError, self.run_command, "create nfs 1")
def test_allow_access_cert(self):
self.run_command("access-allow 1234 cert client.example.com")

View File

@ -824,6 +824,15 @@ def do_create(cs, args):
share_network = None
if args.share_network:
share_network = _find_share_network(cs, args.share_network)
if not args.share_type:
try:
_find_share_type(cs, "default")
except exceptions.CommandError:
msg = ("There is no default share type available. You must pick "
"a valid share type to create a share.")
raise exceptions.CommandError(msg)
share = cs.shares.create(args.share_protocol, args.size, args.snapshot_id,
args.name, args.description,
metadata=share_metadata,

View File

@ -0,0 +1,6 @@
---
fixes:
- |
`Launchpad bug 1960422 <https://bugs.launchpad.net/python-manilaclient/+bug/1960422>`_
has been fixed by prevent sending the share creation request and provide
early feedback to CLI users.