Add like filter

Add like filter support in ``shares``, ``snapshots``,
``share-networks``, ``share-groups`` list.

Implements BP like-filter
Change-Id: I5fdf6d89d0b6c7fa182ddfaac60979bc6c0fc2a4
This commit is contained in:
zhongjun 2017-06-15 11:01:00 +08:00 committed by Goutham Pacha Ravi
parent 8d887f5221
commit e0fd972bf7
7 changed files with 275 additions and 1 deletions

View File

@ -144,6 +144,10 @@ class ShareNetworksReadWriteTest(base.BaseTestCase):
filters = {'name': self.name}
self._list_share_networks_with_filters(filters)
def test_list_share_networks_filter_by_description(self):
filters = {'description': self.description}
self._list_share_networks_with_filters(filters)
def test_list_share_networks_filter_by_neutron_net_id(self):
filters = {'neutron_net_id': self.neutron_net_id}
self._list_share_networks_with_filters(filters)
@ -151,3 +155,18 @@ class ShareNetworksReadWriteTest(base.BaseTestCase):
def test_list_share_networks_filter_by_neutron_subnet_id(self):
filters = {'neutron_subnet_id': self.neutron_subnet_id}
self._list_share_networks_with_filters(filters)
@ddt.data('name', 'description')
def test_list_share_networks_filter_by_inexact(self, option):
self.create_share_network(
name=data_utils.rand_name('autotest_inexact'),
description='fake_description_inexact',
neutron_net_id='fake_neutron_net_id',
neutron_subnet_id='fake_neutron_subnet_id',
)
filters = {option + '~': 'inexact'}
share_networks = self.admin_client.list_share_networks(
filters=filters)
self.assertGreater(len(share_networks), 0)

View File

@ -47,6 +47,14 @@ class SharesListReadOnlyTest(base.BaseTestCase):
def test_shares_list_filter_by_export_location(self, role):
self.clients[role].manila('list', params='--export_location fake')
@ddt.data('admin', 'user')
def test_shares_list_filter_by_inexact_name(self, role):
self.clients[role].manila('list', params='--name~ na')
@ddt.data('admin', 'user')
def test_shares_list_filter_by_inexact_description(self, role):
self.clients[role].manila('list', params='--description~ des')
@ddt.data('admin', 'user')
def test_shares_list_filter_by_status(self, role):
self.clients[role].manila('list', params='--status status')
@ -290,3 +298,23 @@ class SharesListReadWriteTest(base.BaseTestCase):
self.admin_client.list_share_instances,
filters={'export_location': 'fake'},
microversion='2.34')
@ddt.data('name', 'description')
def test_list_shares_by_inexact_option(self, option):
shares = self.user_client.list_shares(
filters={option + '~': option})
# We know we have to have atleast three shares.
# Due to test concurrency, there can be
# more than three shares (some created by other tests).
self.assertGreaterEqual(len(shares), 3)
self.assertTrue(
any(self.private_share['id'] == s['ID'] for s in shares))
def test_list_shares_by_description(self):
shares = self.user_client.list_shares(
filters={'description': self.private_description})
self.assertEqual(1, len(shares))
self.assertTrue(
any(self.private_share['id'] == s['ID'] for s in shares))

View File

@ -291,11 +291,14 @@ class SharesTest(utils.TestCase):
search_opts = {
'fake_str': 'fake_str_value',
'fake_int': 1,
'name~': 'fake_name',
'description~': 'fake_description',
}
cs.shares.list(detailed=False, search_opts=search_opts)
cs.assert_called(
'GET',
'/shares?fake_int=1&fake_str=fake_str_value&is_public=True')
'/shares?description%7E=fake_description&fake_int=1&'
'fake_str=fake_str_value&is_public=True&name%7E=fake_name')
@ddt.data(('id', 'b4991315-eb7d-43ec-979e-5715d4399827', True),
('id', 'b4991315-eb7d-43ec-979e-5715d4399827', False),

View File

@ -245,6 +245,22 @@ class ShellTest(test_utils.TestCase):
self.assert_called(
'GET', '/shares/detail?share_type_id=' + fake_st.id)
def test_list_filter_by_inexact_name(self):
for separator in self.separators:
self.run_command('list --name~' + separator +
'fake_name')
self.assert_called(
'GET',
'/shares/detail?name%7E=fake_name')
def test_list_filter_by_inexact_description(self):
for separator in self.separators:
self.run_command('list --description~' + separator +
'fake_description')
self.assert_called(
'GET',
'/shares/detail?description%7E=fake_description')
def test_list_filter_by_share_type_not_found(self):
for separator in self.separators:
self.assertRaises(
@ -768,6 +784,22 @@ class ShellTest(test_utils.TestCase):
self.assert_called(
'GET', '/snapshots/detail?offset=50')
def test_list_snapshots_filter_by_inexact_name(self):
for separator in self.separators:
self.run_command('snapshot-list --name~' + separator +
'fake_name')
self.assert_called(
'GET',
'/snapshots/detail?name%7E=fake_name')
def test_list_snapshots_filter_by_inexact_description(self):
for separator in self.separators:
self.run_command('snapshot-list --description~' + separator +
'fake_description')
self.assert_called(
'GET',
'/snapshots/detail?description%7E=fake_description')
def test_list_snapshots_with_sort_dir_verify_keys(self):
aliases = ['--sort_dir', '--sort-dir']
for alias in aliases:
@ -1412,6 +1444,22 @@ class ShellTest(test_utils.TestCase):
mock.ANY,
fields=['id', 'name'])
def test_share_network_list_filter_by_inexact_name(self):
for separator in self.separators:
self.run_command('share-network-list --name~' + separator +
'fake_name')
self.assert_called(
'GET',
'/share-networks/detail?name%7E=fake_name')
def test_share_network_list_filter_by_inexact_description(self):
for separator in self.separators:
self.run_command('share-network-list --description~' + separator +
'fake_description')
self.assert_called(
'GET',
'/share-networks/detail?description%7E=fake_description')
def test_share_network_security_service_add(self):
self.run_command('share-network-security-service-add fake_share_nw '
'fake_security_service')
@ -1950,6 +1998,22 @@ class ShellTest(test_utils.TestCase):
cliutils.print_list.assert_called_once_with(
mock.ANY, fields=['Id', 'Name', 'Description'])
def test_share_group_list_filter_by_inexact_name(self):
for separator in self.separators:
self.run_command('share-group-list --name~' + separator +
'fake_name')
self.assert_called(
'GET',
'/share-groups/detail?name%7E=fake_name')
def test_share_group_list_filter_by_inexact_description(self):
for separator in self.separators:
self.run_command('share-group-list --description~' + separator +
'fake_description')
self.assert_called(
'GET',
'/share-groups/detail?description%7E=fake_description')
def test_share_group_show(self):
self.run_command('share-group-show 1234')
@ -2750,3 +2814,25 @@ class ShellTest(test_utils.TestCase):
self.assert_called_anytime(
'DELETE', '/messages/%s' % fake_message.id,
clear_callstack=False)
@ddt.data(('share-network-list', ' --description~',
'/share-networks/', '2.35'),
('share-network-list', ' --name~',
'/share-networks/', '2.35'),
('share-group-list', ' --description~',
'/share-groups/', '2.35'),
('share-group-list', ' --name~', '/share-groups/', '2.35'),
('list', ' --description~', '/shares/', '2.35'),
('list', ' --name~', '/shares/', '2.35'),
('snapshot-list', ' --description~', '/snapshots/', '2.35'),
('snapshot-list', ' --name~', '/snapshots/', '2.35'))
@ddt.unpack
def test_list_filter_by_inexact_version_not_support(
self, cmd, option, url, version):
for separator in self.separators:
self.assertRaises(
exceptions.CommandError,
self.run_command,
cmd + option + separator + 'fake',
version=version
)

View File

@ -224,6 +224,9 @@ class ShareNetworkManager(base.ManagerWithFind):
:rtype: list of :class:`NetworkInfo`
"""
if not search_opts:
search_opts = {}
if search_opts:
query_string = urlencode(
sorted([(k, v) for (k, v) in list(search_opts.items()) if v]))

View File

@ -1476,6 +1476,27 @@ def do_snapshot_access_list(cs, args):
type=str,
default=None,
help='Filter results by name.')
@cliutils.arg(
'--description',
metavar='<description>',
type=str,
default=None,
help='Filter results by description. '
'Available only for microversion >= 2.36.')
@cliutils.arg(
'--name~',
metavar='<name~>',
type=str,
default=None,
help='Filter results matching a share name pattern. '
'Available only for microversion >= 2.36.')
@cliutils.arg(
'--description~',
metavar='<description~>',
type=str,
default=None,
help='Filter results matching a share description pattern. '
'Available only for microversion >= 2.36.')
@cliutils.arg(
'--status',
metavar='<status>',
@ -1652,6 +1673,16 @@ def do_list(cs, args):
'project_id': args.project_id,
'is_public': args.public,
}
if cs.api_version.matches(api_versions.APIVersion("2.36"),
api_versions.APIVersion()):
search_opts['name~'] = getattr(args, 'name~')
search_opts['description~'] = getattr(args, 'description~')
search_opts['description'] = getattr(args, 'description')
elif (getattr(args, 'name~') or getattr(args, 'description~') or
getattr(args, 'description')):
raise exceptions.CommandError(
"Pattern based filtering (name~, description~ and description)"
" is only available with manila API version >= 2.36")
if cs.api_version.matches(api_versions.APIVersion("2.35"),
api_versions.APIVersion()):
@ -1857,6 +1888,13 @@ def do_share_instance_export_location_show(cs, args):
metavar='<name>',
default=None,
help='Filter results by name.')
@cliutils.arg(
'--description',
metavar='<description>',
type=str,
default=None,
help='Filter results by description. '
'Available only for microversion >= 2.36.')
@cliutils.arg(
'--status',
metavar='<status>',
@ -1918,6 +1956,20 @@ def do_share_instance_export_location_show(cs, args):
default=None,
help='Comma separated list of columns to be displayed '
'example --columns "id,name".')
@cliutils.arg(
'--name~',
metavar='<name~>',
type=str,
default=None,
help='Filter results matching a share snapshot name pattern. '
'Available only for microversion >= 2.36.')
@cliutils.arg(
'--description~',
metavar='<description~>',
type=str,
default=None,
help='Filter results matching a share snapshot description pattern. '
'Available only for microversion >= 2.36.')
def do_snapshot_list(cs, args):
"""List all the snapshots."""
all_tenants = int(os.environ.get("ALL_TENANTS", args.all_tenants))
@ -1941,6 +1993,17 @@ def do_snapshot_list(cs, args):
'share_id': share.id,
'usage': args.usage,
}
if cs.api_version.matches(api_versions.APIVersion("2.36"),
api_versions.APIVersion()):
search_opts['name~'] = getattr(args, 'name~')
search_opts['description~'] = getattr(args, 'description~')
search_opts['description'] = getattr(args, 'description')
elif (getattr(args, 'name~') or getattr(args, 'description~') or
getattr(args, 'description')):
raise exceptions.CommandError(
"Pattern based filtering (name~, description~ and description)"
" is only available with manila API version >= 2.36")
snapshots = cs.share_snapshots.list(
search_opts=search_opts,
sort_key=args.sort_key,
@ -2679,6 +2742,13 @@ def do_share_network_list(cs, args):
metavar='<name>',
default=None,
help='Filter results by name.')
@cliutils.arg(
'--description',
metavar='<description>',
type=str,
default=None,
help='Filter results by description. '
'Available only for microversion >= 2.36.')
@cliutils.arg(
'--created-since',
'--created_since', # alias
@ -2764,6 +2834,20 @@ def do_share_network_list(cs, args):
default=None,
help='Comma separated list of columns to be displayed '
'example --columns "id".')
@cliutils.arg(
'--name~',
metavar='<name~>',
type=str,
default=None,
help='Filter results matching a share network name pattern. '
'Available only for microversion >= 2.36.')
@cliutils.arg(
'--description~',
metavar='<description~>',
type=str,
default=None,
help='Filter results matching a share network description pattern. '
'Available only for microversion >= 2.36.')
def do_share_network_list(cs, args):
"""Get a list of network info."""
all_tenants = int(os.environ.get("ALL_TENANTS", args.all_tenants))
@ -2782,6 +2866,17 @@ def do_share_network_list(cs, args):
'offset': args.offset,
'limit': args.limit,
}
if cs.api_version.matches(api_versions.APIVersion("2.36"),
api_versions.APIVersion()):
search_opts['name~'] = getattr(args, 'name~')
search_opts['description~'] = getattr(args, 'description~')
search_opts['description'] = getattr(args, 'description')
elif (getattr(args, 'name~') or getattr(args, 'description~') or
getattr(args, 'description')):
raise exceptions.CommandError(
"Pattern based filtering (name~, description~ and description)"
" is only available with manila API version >= 2.36")
if args.security_service:
search_opts['security_service_id'] = _find_security_service(
cs, args.security_service).id
@ -4105,6 +4200,13 @@ def do_share_group_create(cs, args):
type=str,
default=None,
help='Filter results by name.')
@cliutils.arg(
'--description',
metavar='<description>',
type=str,
default=None,
help='Filter results by description. '
'Available only for microversion >= 2.36.')
@cliutils.arg(
'--status',
metavar='<status>',
@ -4188,6 +4290,20 @@ def do_share_group_create(cs, args):
default=None,
help='Comma separated list of columns to be displayed '
'example --columns "id,name".')
@cliutils.arg(
'--name~',
metavar='<name~>',
type=str,
default=None,
help='Filter results matching a share group name pattern. '
'Available only for microversion >= 2.36.')
@cliutils.arg(
'--description~',
metavar='<description~>',
type=str,
default=None,
help='Filter results matching a share group description pattern. '
'Available only for microversion >= 2.36.')
@cliutils.service_type('sharev2')
@api_versions.experimental_api
def do_share_group_list(cs, args):
@ -4219,6 +4335,17 @@ def do_share_group_list(cs, args):
'share_network_id': share_network.id,
'project_id': args.project_id,
}
if cs.api_version.matches(api_versions.APIVersion("2.36"),
api_versions.APIVersion()):
search_opts['name~'] = getattr(args, 'name~')
search_opts['description~'] = getattr(args, 'description~')
search_opts['description'] = getattr(args, 'description')
elif (getattr(args, 'name~') or getattr(args, 'description~') or
getattr(args, 'description')):
raise exceptions.CommandError(
"Pattern based filtering (name~, description~ and description)"
" is only available with manila API version >= 2.36")
share_groups = cs.share_groups.list(
search_opts=search_opts, sort_key=args.sort_key,
sort_dir=args.sort_dir)

View File

@ -0,0 +1,8 @@
---
features:
- Added support for matching filters (name~, description~)
to filter results of the list commands for shares, snapshots,
share-networks, and share-groups.
- Added support for ``description`` as a filter to the list
commands for shares, snapshots, share-networks, and
share-groups.