Merge "Add optional fields for source to query cli"

This commit is contained in:
Jenkins
2016-01-28 21:20:45 +00:00
committed by Gerrit Code Review
2 changed files with 26 additions and 8 deletions

View File

@@ -53,10 +53,15 @@ class SearchResource(lister.Lister):
) )
parser.add_argument( parser.add_argument(
"--source", "--source",
action='store_true', nargs='?',
default=False, const='all_sources',
help="Whether to display the source details, defaults to false. " metavar="[<field>,...]",
"You can specify --max-width to make the output look better." 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 return parser
@@ -72,8 +77,11 @@ class SearchResource(lister.Lister):
"type": parsed_args.type, "type": parsed_args.type,
"all_projects": parsed_args.all_projects "all_projects": parsed_args.all_projects
} }
if parsed_args.source: source = parsed_args.source
if source:
columns = ("ID", "Score", "Type", "Source") columns = ("ID", "Score", "Type", "Source")
if source != "all_sources":
params["_source"] = source.split(",")
else: else:
columns = ("ID", "Name", "Score", "Type", "Updated") columns = ("ID", "Name", "Score", "Type", "Updated")
# Only return the required fields when source not specified. # Only return the required fields when source not specified.

View File

@@ -39,8 +39,12 @@ class TestSearchResource(TestSearch):
parsed_args = self.check_parser(self.cmd, arglist, []) parsed_args = self.check_parser(self.cmd, arglist, [])
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
details = False details = False
if assertArgs.get('source'): if assertArgs.get("source"):
details = assertArgs.pop('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) self.search_client.search.assert_called_with(**assertArgs)
if details: if details:
@@ -88,4 +92,10 @@ class TestSearchResource(TestSearch):
def test_list_source(self): def test_list_source(self):
self._test_search(["name: fake", "--source"], self._test_search(["name: fake", "--source"],
query={"query_string": {"query": "name: fake"}}, 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)