From baa0091d539060d6cf253fb0b23d8aaa7be84e81 Mon Sep 17 00:00:00 2001 From: Carlos Eduardo Date: Mon, 22 Jul 2024 15:57:42 -0300 Subject: [PATCH] Add ensure shares command Added a new command to trigger the ensure shares API. Change-Id: If40328c83ec4c87d727aceac89a1a14b9fd8a25d Signed-off-by: Carlos Eduardo --- manilaclient/osc/v2/services.py | 32 ++++++++++ .../tests/unit/osc/v2/test_services.py | 58 +++++++++++++++++++ manilaclient/tests/unit/v2/test_services.py | 10 ++++ manilaclient/v2/services.py | 6 ++ ...nsure-shares-command-6cd854408de1979b.yaml | 6 ++ setup.cfg | 1 + 6 files changed, 113 insertions(+) create mode 100644 releasenotes/notes/add-ensure-shares-command-6cd854408de1979b.yaml diff --git a/manilaclient/osc/v2/services.py b/manilaclient/osc/v2/services.py index 6fd67b0aa..68e55cee0 100644 --- a/manilaclient/osc/v2/services.py +++ b/manilaclient/osc/v2/services.py @@ -151,8 +151,40 @@ class ListShareService(command.Lister): ] if share_client.api_version >= api_versions.APIVersion("2.83"): columns.append('Disabled Reason') + if share_client.api_version >= api_versions.APIVersion("2.86"): + columns.append('Ensuring') data = (osc_utils.get_dict_properties( service._info, columns) for service in services) return (columns, data) + + +class EnsureShareService(command.Command): + """Run ensure shares in a back end (Admin only).""" + _description = _("Run ensure shares in a back end (Admin only).") + + def get_parser(self, prog_name): + parser = super(EnsureShareService, self).get_parser(prog_name) + parser.add_argument( + 'host', + metavar='', + help=_("Host to run ensure shares. " + "'example_host@example_backend'.") + ) + return parser + + def take_action(self, parsed_args): + share_client = self.app.client_manager.share + + if share_client.api_version < api_versions.APIVersion("2.86"): + raise exceptions.CommandError( + "Ensure shares API is only available in " + "manila API version >= 2.86") + + try: + share_client.services.ensure_shares(parsed_args.host) + except Exception as e: + raise exceptions.CommandError( + _("Failed to run ensure shares: %s" % e) + ) diff --git a/manilaclient/tests/unit/osc/v2/test_services.py b/manilaclient/tests/unit/osc/v2/test_services.py index 32c2712c8..f1525b9be 100644 --- a/manilaclient/tests/unit/osc/v2/test_services.py +++ b/manilaclient/tests/unit/osc/v2/test_services.py @@ -266,3 +266,61 @@ class TestShareServiceList(TestShareService): else: self.assertEqual(self.column_headers, columns) self.assertEqual(list(self.values), list(data)) + + +@ddt.ddt +class TestShareServiceEnsureShares(TestShareService): + + def setUp(self): + super(TestShareServiceEnsureShares, self).setUp() + + self.cmd = osc_services.EnsureShareService(self.app, None) + + def test_ensure_shares(self): + self.app.client_manager.share.api_version = api_versions.APIVersion( + '2.86') + fake_host = 'fake_host@fakebackend' + arglist = [ + fake_host, + ] + verifylist = [ + ('host', fake_host), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + self.services_mock.ensure_shares.assert_called_with(fake_host) + + def test_ensure_shares_invalid_version(self): + self.app.client_manager.share.api_version = api_versions.APIVersion( + '2.85') + fake_host = 'fake_host@fakebackend' + arglist = [ + fake_host, + ] + verifylist = [ + ('host', fake_host), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.assertRaises(exceptions.CommandError, + self.cmd.take_action, + parsed_args) + + def test_ensure_shares_command_error(self): + self.app.client_manager.share.api_version = api_versions.APIVersion( + '2.86') + self.services_mock.ensure_shares.side_effect = Exception() + fake_host = 'fake_host@fakebackend' + arglist = [ + fake_host, + ] + verifylist = [ + ('host', fake_host), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.assertRaises(exceptions.CommandError, + self.cmd.take_action, + parsed_args) diff --git a/manilaclient/tests/unit/v2/test_services.py b/manilaclient/tests/unit/v2/test_services.py index ba64ae7ac..93518fb09 100644 --- a/manilaclient/tests/unit/v2/test_services.py +++ b/manilaclient/tests/unit/v2/test_services.py @@ -98,3 +98,13 @@ class ServicesTest(utils.TestCase): self._get_resource_path(microversion) + '/disable', {"host": host, "binary": binary}, ) + + def test_ensure_shares(self): + microversion = '2.86' + manager = self._get_manager(microversion) + manager.api.client.post = mock.Mock(return_value='fake') + host = 'fake_host' + + result = manager.ensure_shares(host) + + self.assertEqual(result, 'fake') diff --git a/manilaclient/v2/services.py b/manilaclient/v2/services.py index e10d3f011..f87fff6a1 100644 --- a/manilaclient/v2/services.py +++ b/manilaclient/v2/services.py @@ -87,6 +87,12 @@ class ServiceManager(base.Manager): return self._do_disable(host, binary, RESOURCE_PATH, disable_reason=disable_reason) + @api_versions.wraps("2.86") + def ensure_shares(self, host): # noqa + resource_path = f'{RESOURCE_PATH}/ensure_shares' + body = {"host": host} + return self.api.client.post(resource_path, body=body) + def server_api_version(self, url_append=""): """Returns the API Version supported by the server. diff --git a/releasenotes/notes/add-ensure-shares-command-6cd854408de1979b.yaml b/releasenotes/notes/add-ensure-shares-command-6cd854408de1979b.yaml new file mode 100644 index 000000000..23cf84e33 --- /dev/null +++ b/releasenotes/notes/add-ensure-shares-command-6cd854408de1979b.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Starting from API version 2.86, it is possible to run ensure shares + on a given manila-share host, in order to update the shares + information. diff --git a/setup.cfg b/setup.cfg index a22325ae3..d26b16d4d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -115,6 +115,7 @@ openstack.share.v2 = share_availability_zone_list = manilaclient.osc.v2.availability_zones:ShareAvailabilityZoneList share_service_set = manilaclient.osc.v2.services:SetShareService share_service_list = manilaclient.osc.v2.services:ListShareService + share_service_ensure_shares = manilaclient.osc.v2.services:EnsureShareService share_security_service_create = manilaclient.osc.v2.security_services:CreateShareSecurityService share_security_service_delete = manilaclient.osc.v2.security_services:DeleteShareSecurityService share_security_service_show = manilaclient.osc.v2.security_services:ShowShareSecurityService