Commit 03ec545a authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

qlang.MakeFilter: Enable use of different name field



Jobs don't have a “name” field, so we must be able to control
the field used for simple filters.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 26a72a48
......@@ -2799,7 +2799,8 @@ def _WarnUnknownFields(fdefs):
def GenericList(resource, fields, names, unit, separator, header, cl=None,
format_override=None, verbose=False, force_filter=False):
format_override=None, verbose=False, force_filter=False,
namefield=None):
"""Generic implementation for listing all items of a resource.
@param resource: One of L{constants.QR_VIA_LUXI}
......@@ -2822,12 +2823,15 @@ def GenericList(resource, fields, names, unit, separator, header, cl=None,
indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
@type verbose: boolean
@param verbose: whether to use verbose field descriptions or not
@type namefield: string
@param namefield: Name of field to use for simple filters (see
L{qlang.MakeFilter} for details)
"""
if not names:
names = None
qfilter = qlang.MakeFilter(names, force_filter)
qfilter = qlang.MakeFilter(names, force_filter, namefield=namefield)
if cl is None:
cl = GetClient()
......
......@@ -292,7 +292,7 @@ def _MakeFilterPart(namefield, text):
return [OP_EQUAL, namefield, text]
def MakeFilter(args, force_filter):
def MakeFilter(args, force_filter, namefield=None):
"""Try to make a filter from arguments to a command.
If the name could be a filter it is parsed as such. If it's just a globbing
......@@ -303,10 +303,16 @@ def MakeFilter(args, force_filter):
@param args: Arguments to command
@type force_filter: bool
@param force_filter: Whether to force treatment as a full-fledged filter
@type namefield: string
@param namefield: Name of field to use for simple filters (use L{None} for
a default of "name")
@rtype: list
@return: Query filter
"""
if namefield is None:
namefield = "name"
if (force_filter or
(args and len(args) == 1 and _CheckFilter(args[0]))):
try:
......@@ -317,7 +323,7 @@ def MakeFilter(args, force_filter):
result = ParseFilter(filter_text)
elif args:
result = [OP_OR] + map(compat.partial(_MakeFilterPart, "name"), args)
result = [OP_OR] + map(compat.partial(_MakeFilterPart, namefield), args)
else:
result = None
......
......@@ -186,6 +186,12 @@ class TestMakeFilter(unittest.TestCase):
[qlang.OP_OR, [qlang.OP_EQUAL, "name", "web1"],
[qlang.OP_EQUAL, "name", "web2"]])
def testPlainNamesOtherNamefield(self):
self.assertEqual(qlang.MakeFilter(["mailA", "mailB"], False,
namefield="id"),
[qlang.OP_OR, [qlang.OP_EQUAL, "id", "mailA"],
[qlang.OP_EQUAL, "id", "mailB"]])
def testForcedFilter(self):
for i in [None, [], ["1", "2"], ["", "", ""], ["a", "b", "c", "d"]]:
self.assertRaises(errors.OpPrereqError, qlang.MakeFilter, i, True)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment