From b5a4f9c396fd72ece58d09974718900c3b93edd5 Mon Sep 17 00:00:00 2001 From: Besjana Gjika Date: Mon, 8 Nov 2021 09:51:22 +0000 Subject: [PATCH] Add support for ~name and ~description filtering when listing shares. This fix addresses a TODO item for the "openstack share list" command. It adds support for ~name and ~description filtering when listing shares. Closes-Bug: #1949091 Change-Id: If6bb9e7d379672116d88012682069686f5aedb52 --- manilaclient/osc/v2/share.py | 26 +- manilaclient/tests/unit/osc/v2/test_share.py | 244 +++++-------------- 2 files changed, 88 insertions(+), 182 deletions(-) diff --git a/manilaclient/osc/v2/share.py b/manilaclient/osc/v2/share.py index 04d54a545..2c8316928 100644 --- a/manilaclient/osc/v2/share.py +++ b/manilaclient/osc/v2/share.py @@ -461,6 +461,20 @@ class ListShare(command.Lister): metavar="", help=_('The last share ID of the previous page'), ) + parser.add_argument( + "--name~", + metavar="", + default=None, + help=_("Filter results matching a share name pattern. " + "Available only for microversion >= 2.36.") + ) + parser.add_argument( + '--description~', + metavar="", + default=None, + help=_("Filter results matching a share description pattern." + "Available only for microversion >= 2.36.") + ) return parser @@ -468,8 +482,7 @@ class ListShare(command.Lister): share_client = self.app.client_manager.share identity_client = self.app.client_manager.identity - # TODO(gouthamr): Add support for ~name, ~description - # export_location filtering + # TODO(gouthamr): Add support for export_location filtering if parsed_args.long: columns = SHARE_ATTRIBUTES column_headers = SHARE_ATTRIBUTES_HEADERS @@ -563,6 +576,15 @@ class ListShare(command.Lister): # NOTE(vkmc) We implemented sorting and filtering in manilaclient # but we will use the one provided by osc + if share_client.api_version >= api_versions.APIVersion("2.36"): + search_opts['name~'] = getattr(parsed_args, 'name~') + search_opts['description~'] = getattr(parsed_args, 'description~') + elif (getattr(parsed_args, 'name~') or + getattr(parsed_args, 'description~')): + raise exceptions.CommandError( + "Pattern based filtering (name~ and description~)" + " is only available with manila API version >= 2.36") + data = share_client.shares.list(search_opts=search_opts) data = oscutils.sort_items(data, parsed_args.sort, str) diff --git a/manilaclient/tests/unit/osc/v2/test_share.py b/manilaclient/tests/unit/osc/v2/test_share.py index 57f5e9ad4..fbecfb563 100644 --- a/manilaclient/tests/unit/osc/v2/test_share.py +++ b/manilaclient/tests/unit/osc/v2/test_share.py @@ -535,6 +535,8 @@ class TestShareList(TestShare): 'project_id': None, 'user_id': None, 'offset': None, + 'name~': None, + 'description~': None, } return search_opts @@ -550,24 +552,7 @@ class TestShareList(TestShare): cmd_columns, cmd_data = self.cmd.take_action(parsed_args) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': None, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': None, - } + search_opts = self._get_search_opts() self.shares_mock.list.assert_called_once_with( search_opts=search_opts, @@ -593,24 +578,7 @@ class TestShareList(TestShare): cmd_columns, cmd_data = self.cmd.take_action(parsed_args) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': None, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': None, - } + search_opts = self._get_search_opts() search_opts['project_id'] = self.project.id search_opts['all_tenants'] = True @@ -641,24 +609,7 @@ class TestShareList(TestShare): cmd_columns, cmd_data = self.cmd.take_action(parsed_args) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': None, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': None, - } + search_opts = self._get_search_opts() search_opts['project_id'] = self.project.id search_opts['all_tenants'] = True @@ -687,24 +638,7 @@ class TestShareList(TestShare): cmd_columns, cmd_data = self.cmd.take_action(parsed_args) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': None, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': None, - } + search_opts = self._get_search_opts() search_opts['user_id'] = self.user.id @@ -733,24 +667,7 @@ class TestShareList(TestShare): cmd_columns, cmd_data = self.cmd.take_action(parsed_args) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': None, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': None, - } + search_opts = self._get_search_opts() search_opts['user_id'] = self.user.id @@ -778,24 +695,7 @@ class TestShareList(TestShare): cmd_columns, cmd_data = self.cmd.take_action(parsed_args) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': None, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': None, - } + search_opts = self._get_search_opts() search_opts['name'] = self.new_share.name @@ -823,24 +723,7 @@ class TestShareList(TestShare): cmd_columns, cmd_data = self.cmd.take_action(parsed_args) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': None, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': None, - } + search_opts = self._get_search_opts() search_opts['status'] = self.new_share.status @@ -868,24 +751,7 @@ class TestShareList(TestShare): cmd_columns, cmd_data = self.cmd.take_action(parsed_args) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': None, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': None, - } + search_opts = self._get_search_opts() search_opts['all_tenants'] = True @@ -912,24 +778,7 @@ class TestShareList(TestShare): cmd_columns, cmd_data = self.cmd.take_action(parsed_args) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': None, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': None, - } + search_opts = self._get_search_opts() self.shares_mock.list.assert_called_once_with( search_opts=search_opts, @@ -1019,24 +868,10 @@ class TestShareList(TestShare): self.assertEqual(self.columns, cmd_columns) - search_opts = { - 'all_tenants': False, - 'is_public': False, - 'metadata': {}, - 'extra_specs': {}, - 'limit': 2, - 'name': None, - 'status': None, - 'host': None, - 'share_server_id': None, - 'share_network_id': None, - 'share_type_id': None, - 'snapshot_id': None, - 'share_group_id': None, - 'project_id': None, - 'user_id': None, - 'offset': self.new_share.id - } + search_opts = self._get_search_opts() + + search_opts['limit'] = 2 + search_opts['offset'] = self.new_share.id data = self._get_data() @@ -1055,6 +890,55 @@ class TestShareList(TestShare): self.assertRaises(argparse.ArgumentTypeError, self.check_parser, self.cmd, arglist, verifylist) + def test_share_list_name_description_filter(self): + arglist = [ + '--name~', self.new_share.name, + '--description~', self.new_share.description, + ] + + verifylist = [ + ('name~', self.new_share.name), + ('description~', self.new_share.description), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + cmd_columns, cmd_data = self.cmd.take_action(parsed_args) + + search_opts = self._get_search_opts() + + search_opts['name~'] = self.new_share.name + search_opts['description~'] = self.new_share.description + + self.shares_mock.list.assert_called_once_with( + search_opts=search_opts, + ) + + self.assertEqual(self.columns, cmd_columns) + + data = self._get_data() + + self.assertEqual(data, tuple(cmd_data)) + + def test_list_share_api_version_exception(self): + self.app.client_manager.share.api_version = api_versions.APIVersion( + "2.35") + + arglist = [ + '--name~', 'Name', + '--description~', 'Description', + ] + verifylist = [ + ('name~', 'Name'), + ('description~', 'Description'), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.assertRaises( + osc_exceptions.CommandError, + self.cmd.take_action, + parsed_args) + class TestShareShow(TestShare):