Add vars filtering to region-list

- Region listing is missing filtering using
variables. This patch allows users to filter
region listing using --vars='a:b', and we can
use multiple vars filters using the form

--vars='a:b' --vars='c:d'
or
--vars='a:b,c:d'

Closes-Bug: 1669457

Change-Id: I0e0cabaec8b399170902a0e3c0527d839d419913
This commit is contained in:
Sulochan Acharya 2017-03-02 14:57:40 +00:00 committed by Ian Cordasco
parent 4bd5bc647e
commit 3146f50e79
3 changed files with 44 additions and 0 deletions

View File

@ -79,9 +79,19 @@ def do_region_create(cc, args):
metavar='<marker>',
default=None,
help='ID of the region to use to resume listing regions.')
@cliutils.arg('--vars',
metavar='<vars>',
nargs='+',
action='append',
default=[],
help='Variables to use as filter in the form of '
'--vars="key:value" --vars="key2:value2"')
def do_region_list(cc, args):
"""List all regions."""
params = {}
if args.vars:
query_vars = ",".join([i[0] for i in args.vars])
params['vars'] = query_vars
if args.cloud is not None:
params['cloud_id'] = args.cloud
if args.limit is not None:

View File

@ -161,3 +161,14 @@ class TestRegionsShell(base.ShellTestCase):
formatter=mock.Mock())
regions_shell.do_region_update(client, invalid_input)
mock_update.assert_called_once_with(1, name='mock_region')
@mock.patch('cratonclient.v1.regions.RegionManager.list')
def test_region_list_with_vars_success(self, mock_list):
"""Verify --vars arguments successfully passed to Client."""
self.shell('region-list --vars a:b')
mock_list.assert_called_once_with(
vars='a:b',
marker=None,
autopaginate=False,
)
mock_list.reset_mock()

View File

@ -207,6 +207,7 @@ class TestDoRegionList(base.TestShellCommand):
kwargs.setdefault('fields', regions_shell.DEFAULT_REGION_FIELDS)
kwargs.setdefault('marker', None)
kwargs.setdefault('all', False)
kwargs.setdefault('vars', None)
return super(TestDoRegionList, self).args_for(**kwargs)
def test_with_defaults(self):
@ -227,6 +228,28 @@ class TestDoRegionList(base.TestShellCommand):
)
self.assertFieldsEqualTo(regions_shell.DEFAULT_REGION_FIELDS)
def test_with_vars(self):
"""Verify that we pass vars filters to region list."""
args = self.args_for(vars=[['a:b']])
regions_shell.do_region_list(self.craton_client, args)
self.craton_client.regions.list.assert_called_once_with(
vars='a:b',
marker=None,
autopaginate=False,
)
self.assertFieldsEqualTo(regions_shell.DEFAULT_REGION_FIELDS)
def test_with_multiple_vars(self):
"""Verify that we pass multiple vars filters to region list."""
args = self.args_for(vars=[['a:b'], ['c:d']])
regions_shell.do_region_list(self.craton_client, args)
self.craton_client.regions.list.assert_called_once_with(
vars='a:b,c:d',
marker=None,
autopaginate=False,
)
self.assertFieldsEqualTo(regions_shell.DEFAULT_REGION_FIELDS)
def test_negative_limit(self):
"""Ensure we raise an exception for negative limits."""
args = self.args_for(limit=-1)