From 03ec545a4f3fea0008a8ff5dec2bd41d8cae7508 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Wed, 28 Mar 2012 14:25:48 +0200 Subject: [PATCH] qlang.MakeFilter: Enable use of different name field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Jobs don't have a βnameβ field, so we must be able to control the field used for simple filters. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/cli.py | 8 ++++++-- lib/qlang.py | 10 ++++++++-- test/ganeti.qlang_unittest.py | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/cli.py b/lib/cli.py index 7947cc65c..56245f54d 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -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() diff --git a/lib/qlang.py b/lib/qlang.py index bd2a3fd5f..839a615ba 100644 --- a/lib/qlang.py +++ b/lib/qlang.py @@ -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 diff --git a/test/ganeti.qlang_unittest.py b/test/ganeti.qlang_unittest.py index b3cf4f7a3..02b953406 100755 --- a/test/ganeti.qlang_unittest.py +++ b/test/ganeti.qlang_unittest.py @@ -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) -- GitLab