From 3146f50e79ef1f600f77087aeb30477a2172b692 Mon Sep 17 00:00:00 2001 From: Sulochan Acharya Date: Thu, 2 Mar 2017 14:57:40 +0000 Subject: [PATCH] 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 --- cratonclient/shell/v1/regions_shell.py | 10 ++++++++ .../shell/v1/test_regions_shell.py | 11 +++++++++ .../tests/unit/shell/v1/test_regions_shell.py | 23 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/cratonclient/shell/v1/regions_shell.py b/cratonclient/shell/v1/regions_shell.py index 9f55e59..8ee172f 100644 --- a/cratonclient/shell/v1/regions_shell.py +++ b/cratonclient/shell/v1/regions_shell.py @@ -79,9 +79,19 @@ def do_region_create(cc, args): metavar='', default=None, help='ID of the region to use to resume listing regions.') +@cliutils.arg('--vars', + metavar='', + 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: diff --git a/cratonclient/tests/integration/shell/v1/test_regions_shell.py b/cratonclient/tests/integration/shell/v1/test_regions_shell.py index e2963a2..d8689a1 100644 --- a/cratonclient/tests/integration/shell/v1/test_regions_shell.py +++ b/cratonclient/tests/integration/shell/v1/test_regions_shell.py @@ -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() diff --git a/cratonclient/tests/unit/shell/v1/test_regions_shell.py b/cratonclient/tests/unit/shell/v1/test_regions_shell.py index 86654cf..6078bde 100644 --- a/cratonclient/tests/unit/shell/v1/test_regions_shell.py +++ b/cratonclient/tests/unit/shell/v1/test_regions_shell.py @@ -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)