From bca28631e206d2412b7ebe5e9958997e841cd2b5 Mon Sep 17 00:00:00 2001 From: liyingjun Date: Fri, 11 Dec 2015 12:44:34 +0800 Subject: [PATCH] Add optional fields for source to query cli Improve cli `openstack search query --source | [,...]`, 'all' to fetch all fields from source, and fetch fields wanted by combining fields with ','. Change-Id: I38219c13d5f31b5b963117dd8a05a0e091042088 --- searchlightclient/osc/v1/search.py | 18 +++++++++++++----- .../tests/unit/osc/v1/test_search.py | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/searchlightclient/osc/v1/search.py b/searchlightclient/osc/v1/search.py index 6959a63..cc33df1 100644 --- a/searchlightclient/osc/v1/search.py +++ b/searchlightclient/osc/v1/search.py @@ -53,10 +53,15 @@ class SearchResource(lister.Lister): ) parser.add_argument( "--source", - action='store_true', - default=False, - help="Whether to display the source details, defaults to false. " - "You can specify --max-width to make the output look better." + nargs='?', + const='all_sources', + metavar="[,...]", + help="Whether to display the json source. If not specified, " + "it will not be displayed. If specified with no argument, " + "the full source will be displayed. Otherwise, specify the " + "fields combined with ',' to return the fields you want." + "It is recommended that you use the --max-width argument " + "with this option." ) return parser @@ -72,8 +77,11 @@ class SearchResource(lister.Lister): "type": parsed_args.type, "all_projects": parsed_args.all_projects } - if parsed_args.source: + source = parsed_args.source + if source: columns = ("ID", "Score", "Type", "Source") + if source != "all_sources": + params["_source"] = source.split(",") else: columns = ("ID", "Name", "Score", "Type", "Updated") # Only return the required fields when source not specified. diff --git a/searchlightclient/tests/unit/osc/v1/test_search.py b/searchlightclient/tests/unit/osc/v1/test_search.py index 4d60057..b3fdcee 100644 --- a/searchlightclient/tests/unit/osc/v1/test_search.py +++ b/searchlightclient/tests/unit/osc/v1/test_search.py @@ -39,8 +39,12 @@ class TestSearchResource(TestSearch): parsed_args = self.check_parser(self.cmd, arglist, []) columns, data = self.cmd.take_action(parsed_args) details = False - if assertArgs.get('source'): - details = assertArgs.pop('source') + if assertArgs.get("source"): + details = True + if assertArgs.get("source") == "all_resources": + assertArgs.pop("source") + else: + assertArgs["_source"] = assertArgs.pop("source") self.search_client.search.assert_called_with(**assertArgs) if details: @@ -88,4 +92,10 @@ class TestSearchResource(TestSearch): def test_list_source(self): self._test_search(["name: fake", "--source"], query={"query_string": {"query": "name: fake"}}, - all_projects=False, source=True, type=None) + all_projects=False, source="all_resources", + type=None) + + def test_list_optional_source(self): + self._test_search(["name: fake", "--source", "f1,f2"], + query={"query_string": {"query": "name: fake"}}, + all_projects=False, source=["f1", "f2"], type=None)