Commit 60cba7f8 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

qlang: Add function to build simple filter



This will be used in clients to build the filters for query2.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarRené Nussbaumer <rn@google.com>
parent 145bea54
......@@ -73,3 +73,16 @@ def ReadSimpleFilter(namefield, filter_):
result.append(value)
return result
def MakeSimpleFilter(namefield, values):
"""Builds a filter for use with L{ReadSimpleFilter}.
@param namefield: Name of field containing item name
@param values: List of names
"""
if values:
return [OP_OR] + [[OP_EQUAL, namefield, i] for i in values]
return None
......@@ -56,5 +56,23 @@ class TestReadSimpleFilter(unittest.TestCase):
"name", i)
class TestMakeSimpleFilter(unittest.TestCase):
def _Test(self, field, names, expected, parse_exp=None):
if parse_exp is None:
parse_exp = names
filter_ = qlang.MakeSimpleFilter(field, names)
self.assertEqual(filter_, expected)
self.assertEqual(qlang.ReadSimpleFilter(field, filter_), parse_exp)
def test(self):
self._Test("name", None, None, parse_exp=[])
self._Test("name", [], None)
self._Test("name", ["node1.example.com"],
["|", ["=", "name", "node1.example.com"]])
self._Test("xyz", ["a", "b", "c"],
["|", ["=", "xyz", "a"], ["=", "xyz", "b"], ["=", "xyz", "c"]])
if __name__ == "__main__":
testutils.GanetiTestProgram()
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