1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """Module for a simple query language"""
23
24 from ganeti import errors
25
26
27 OP_OR = "|"
28 OP_EQUAL = "="
29
30
32 """Function extracting wanted names from restricted filter.
33
34 This should only be used until proper filtering is implemented. The filter
35 must either be empty or of the format C{["|", ["=", field, "name1"], ["=",
36 field, "name2"], ...]}.
37
38 """
39 if filter_ is None:
40 return []
41
42 if not isinstance(filter_, list):
43 raise errors.ParameterError("Filter should be list")
44
45 if not filter_ or filter_[0] != OP_OR:
46 raise errors.ParameterError("Filter should start with OR operator")
47
48 if len(filter_) < 2:
49 raise errors.ParameterError("Invalid filter, OR operator should have"
50 " operands")
51
52 result = []
53
54 for idx, item in enumerate(filter_[1:]):
55 if not isinstance(item, list):
56 raise errors.ParameterError("Invalid OR operator, operand %s not a"
57 " list" % idx)
58
59 if len(item) != 3 or item[0] != OP_EQUAL:
60 raise errors.ParameterError("Invalid OR operator, operand %s is not an"
61 " equality filter" % idx)
62
63 (_, name, value) = item
64
65 if not isinstance(value, basestring):
66 raise errors.ParameterError("Operand %s for OR should compare against a"
67 " string" % idx)
68
69 if name != namefield:
70 raise errors.ParameterError("Operand %s for OR should filter field '%s',"
71 " not '%s'" % (idx, namefield, name))
72
73 result.append(value)
74
75 return result
76
77
79 """Builds a filter for use with L{ReadSimpleFilter}.
80
81 @param namefield: Name of field containing item name
82 @param values: List of names
83
84 """
85 if values:
86 return [OP_OR] + [[OP_EQUAL, namefield, i] for i in values]
87
88 return None
89