Merge "CLI for disable service reason"

This commit is contained in:
Zuul 2024-02-28 23:40:06 +00:00 committed by Gerrit Code Review
commit 5fd1e894d0
6 changed files with 111 additions and 16 deletions

View File

@ -27,7 +27,7 @@ from manilaclient import utils
LOG = logging.getLogger(__name__)
MAX_VERSION = '2.82'
MAX_VERSION = '2.83'
MIN_VERSION = '2.0'
DEPRECATED_VERSION = '1.0'
_VERSIONED_METHOD_MAP = {}

View File

@ -13,6 +13,7 @@ from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils as osc_utils
from manilaclient import api_versions
from manilaclient.common._i18n import _
@ -44,9 +45,20 @@ class SetShareService(command.Command):
action='store_true',
help=_('Disable share service'),
)
parser.add_argument(
"--disable-reason",
metavar="<reason>",
help=_("Reason for disabling the service "
"(should be used with --disable option)")
)
return parser
def take_action(self, parsed_args):
if parsed_args.disable_reason and not parsed_args.disable:
msg = _("Cannot specify option --disable-reason without "
"--disable specified.")
raise exceptions.CommandError(msg)
share_client = self.app.client_manager.share
if parsed_args.enable:
@ -58,9 +70,19 @@ class SetShareService(command.Command):
"Failed to enable service: %s" % e))
if parsed_args.disable:
if parsed_args.disable_reason:
if share_client.api_version < api_versions.APIVersion("2.83"):
raise exceptions.CommandError(
"Service disable reason can be specified only with "
"manila API version >= 2.83")
try:
share_client.services.disable(
parsed_args.host, parsed_args.binary)
if parsed_args.disable_reason:
share_client.services.disable(
parsed_args.host, parsed_args.binary,
disable_reason=parsed_args.disable_reason)
else:
share_client.services.disable(
parsed_args.host, parsed_args.binary)
except Exception as e:
raise exceptions.CommandError(_(
"Failed to disable service: %s" % e))
@ -127,6 +149,8 @@ class ListShareService(command.Lister):
'State',
'Updated At'
]
if share_client.api_version >= api_versions.APIVersion("2.83"):
columns.append('Disabled Reason')
data = (osc_utils.get_dict_properties(
service._info, columns) for service in services)

View File

@ -45,11 +45,12 @@ class ShareServicesTestCase(base.OSCClientTestBase):
first_service = service[0]
self.openstack(f'share service set {first_service["Host"]} '
f'{first_service["Binary"]} '
'--disable')
'--disable --disable-reason test')
result = self.listing_result('share service',
'list --status disabled')
self.assertEqual(first_service['ID'], result[0]['ID'])
self.assertEqual('disabled', result[0]['Status'])
self.assertEqual('test', result[0]['Disabled Reason'])
# enable the share service again
self.openstack(f'share service set {first_service["Host"]} '

View File

@ -10,9 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
#
import ddt
from osc_lib import exceptions
from osc_lib import utils as oscutils
from manilaclient import api_versions
from manilaclient.osc import utils
from manilaclient.osc.v2 import services as osc_services
from manilaclient.tests.unit.osc.v2 import fakes as manila_fakes
@ -96,6 +98,33 @@ class TestShareServiceSet(TestShareService):
self.share_service.binary)
self.assertIsNone(result)
def test_service_set_disable_with_reason(self):
self.app.client_manager.share.api_version = api_versions.APIVersion(
"2.83")
reason = 'earthquake'
arglist = [
'--disable',
'--disable-reason', reason,
self.share_service.host,
self.share_service.binary,
]
verifylist = [
('host', self.share_service.host),
('binary', self.share_service.binary),
('disable', True),
('disable_reason', reason),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.services_mock.disable.assert_called_with(
self.share_service.host,
self.share_service.binary,
disable_reason=reason
)
self.assertIsNone(result)
def test_share_service_set_disable_exception(self):
arglist = [
self.share_service.host,
@ -116,6 +145,7 @@ class TestShareServiceSet(TestShareService):
parsed_args)
@ddt.ddt
class TestShareServiceList(TestShareService):
columns = [
@ -127,22 +157,31 @@ class TestShareServiceList(TestShareService):
'state',
'updated_at'
]
columns_with_reason = columns + ['disabled_reason']
column_headers = utils.format_column_headers(columns)
column_headers_with_reason = utils.format_column_headers(
columns_with_reason)
def setUp(self):
super(TestShareServiceList, self).setUp()
self.services_list = (
manila_fakes.FakeShareService.create_fake_services()
manila_fakes.FakeShareService.create_fake_services(
{'disabled_reason': ''})
)
self.services_mock.list.return_value = self.services_list
self.values = (oscutils.get_dict_properties(
i._info, self.columns) for i in self.services_list)
self.values_with_reason = (oscutils.get_dict_properties(
i._info, self.columns_with_reason) for i in self.services_list)
self.cmd = osc_services.ListShareService(self.app, None)
def test_share_service_list(self):
@ddt.data('2.82', '2.83')
def test_share_service_list(self, version):
self.app.client_manager.share.api_version = api_versions.APIVersion(
version)
arglist = []
verifylist = []
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -157,10 +196,17 @@ class TestShareServiceList(TestShareService):
'state': None,
'zone': None
})
self.assertEqual(self.column_headers, columns)
self.assertEqual(list(self.values), list(data))
if api_versions.APIVersion(version) >= api_versions.APIVersion("2.83"):
self.assertEqual(self.column_headers_with_reason, columns)
self.assertEqual(list(self.values_with_reason), list(data))
else:
self.assertEqual(self.column_headers, columns)
self.assertEqual(list(self.values), list(data))
def test_share_service_list_host_status(self):
@ddt.data('2.82', '2.83')
def test_share_service_list_host_status(self, version):
self.app.client_manager.share.api_version = api_versions.APIVersion(
version)
arglist = [
'--host', self.services_list[0].host,
'--status', self.services_list[1].status
@ -181,10 +227,17 @@ class TestShareServiceList(TestShareService):
'state': None,
'zone': None
})
self.assertEqual(self.column_headers, columns)
self.assertEqual(list(self.values), list(data))
if api_versions.APIVersion(version) >= api_versions.APIVersion("2.83"):
self.assertEqual(self.column_headers_with_reason, columns)
self.assertEqual(list(self.values_with_reason), list(data))
else:
self.assertEqual(self.column_headers, columns)
self.assertEqual(list(self.values), list(data))
def test_share_service_list_binary_state_zone(self):
@ddt.data('2.82', '2.83')
def test_share_service_list_binary_state_zone(self, version):
self.app.client_manager.share.api_version = api_versions.APIVersion(
version)
arglist = [
'--binary', self.services_list[0].binary,
'--state', self.services_list[1].state,
@ -207,5 +260,9 @@ class TestShareServiceList(TestShareService):
'state': self.services_list[1].state,
'zone': self.services_list[1].zone
})
self.assertEqual(self.column_headers, columns)
self.assertEqual(list(self.values), list(data))
if api_versions.APIVersion(version) >= api_versions.APIVersion("2.83"):
self.assertEqual(self.column_headers_with_reason, columns)
self.assertEqual(list(self.values_with_reason), list(data))
else:
self.assertEqual(self.column_headers, columns)
self.assertEqual(list(self.values), list(data))

View File

@ -66,19 +66,27 @@ class ServiceManager(base.Manager):
def enable(self, host, binary): # noqa
return self._do_enable(host, binary, RESOURCE_PATH)
def _do_disable(self, host, binary, resource_path=RESOURCE_PATH):
def _do_disable(self, host, binary, resource_path=RESOURCE_PATH,
disable_reason=None):
"""Disable the service specified by hostname and binary."""
body = {"host": host, "binary": binary}
if disable_reason:
body["disabled_reason"] = disable_reason
return self._update("%s/disable" % resource_path, body)
@api_versions.wraps("1.0", "2.6")
def disable(self, host, binary):
return self._do_disable(host, binary, RESOURCE_PATH_LEGACY)
@api_versions.wraps("2.7") # noqa
@api_versions.wraps("2.7", "2.82") # noqa
def disable(self, host, binary): # noqa
return self._do_disable(host, binary, RESOURCE_PATH)
@api_versions.wraps("2.83") # noqa
def disable(self, host, binary, disable_reason=None): # noqa
return self._do_disable(host, binary, RESOURCE_PATH,
disable_reason=disable_reason)
def server_api_version(self, url_append=""):
"""Returns the API Version supported by the server.

View File

@ -0,0 +1,5 @@
---
features:
- Support add disabled reason to services command.
See `bug 2037700 <https://bugs.launchpad.net/manila/+bug/2037700>`_
for more details.