qlang.MakeFilter: Enable use of different name field
authorMichael Hanselmann <hansmi@google.com>
Wed, 28 Mar 2012 12:25:48 +0000 (14:25 +0200)
committerMichael Hanselmann <hansmi@google.com>
Fri, 30 Mar 2012 12:02:49 +0000 (14:02 +0200)
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
lib/qlang.py
test/ganeti.qlang_unittest.py

index 7947cc6..56245f5 100644 (file)
@@ -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()
index bd2a3fd..839a615 100644 (file)
@@ -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
 
index b3cf4f7..02b9534 100755 (executable)
@@ -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)