Revision 03ec545a
b/lib/cli.py | ||
---|---|---|
2799 | 2799 |
|
2800 | 2800 |
|
2801 | 2801 |
def GenericList(resource, fields, names, unit, separator, header, cl=None, |
2802 |
format_override=None, verbose=False, force_filter=False): |
|
2802 |
format_override=None, verbose=False, force_filter=False, |
|
2803 |
namefield=None): |
|
2803 | 2804 |
"""Generic implementation for listing all items of a resource. |
2804 | 2805 |
|
2805 | 2806 |
@param resource: One of L{constants.QR_VIA_LUXI} |
... | ... | |
2822 | 2823 |
indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY} |
2823 | 2824 |
@type verbose: boolean |
2824 | 2825 |
@param verbose: whether to use verbose field descriptions or not |
2826 |
@type namefield: string |
|
2827 |
@param namefield: Name of field to use for simple filters (see |
|
2828 |
L{qlang.MakeFilter} for details) |
|
2825 | 2829 |
|
2826 | 2830 |
""" |
2827 | 2831 |
if not names: |
2828 | 2832 |
names = None |
2829 | 2833 |
|
2830 |
qfilter = qlang.MakeFilter(names, force_filter) |
|
2834 |
qfilter = qlang.MakeFilter(names, force_filter, namefield=namefield)
|
|
2831 | 2835 |
|
2832 | 2836 |
if cl is None: |
2833 | 2837 |
cl = GetClient() |
b/lib/qlang.py | ||
---|---|---|
292 | 292 |
return [OP_EQUAL, namefield, text] |
293 | 293 |
|
294 | 294 |
|
295 |
def MakeFilter(args, force_filter): |
|
295 |
def MakeFilter(args, force_filter, namefield=None):
|
|
296 | 296 |
"""Try to make a filter from arguments to a command. |
297 | 297 |
|
298 | 298 |
If the name could be a filter it is parsed as such. If it's just a globbing |
... | ... | |
303 | 303 |
@param args: Arguments to command |
304 | 304 |
@type force_filter: bool |
305 | 305 |
@param force_filter: Whether to force treatment as a full-fledged filter |
306 |
@type namefield: string |
|
307 |
@param namefield: Name of field to use for simple filters (use L{None} for |
|
308 |
a default of "name") |
|
306 | 309 |
@rtype: list |
307 | 310 |
@return: Query filter |
308 | 311 |
|
309 | 312 |
""" |
313 |
if namefield is None: |
|
314 |
namefield = "name" |
|
315 |
|
|
310 | 316 |
if (force_filter or |
311 | 317 |
(args and len(args) == 1 and _CheckFilter(args[0]))): |
312 | 318 |
try: |
... | ... | |
317 | 323 |
|
318 | 324 |
result = ParseFilter(filter_text) |
319 | 325 |
elif args: |
320 |
result = [OP_OR] + map(compat.partial(_MakeFilterPart, "name"), args)
|
|
326 |
result = [OP_OR] + map(compat.partial(_MakeFilterPart, namefield), args)
|
|
321 | 327 |
else: |
322 | 328 |
result = None |
323 | 329 |
|
b/test/ganeti.qlang_unittest.py | ||
---|---|---|
186 | 186 |
[qlang.OP_OR, [qlang.OP_EQUAL, "name", "web1"], |
187 | 187 |
[qlang.OP_EQUAL, "name", "web2"]]) |
188 | 188 |
|
189 |
def testPlainNamesOtherNamefield(self): |
|
190 |
self.assertEqual(qlang.MakeFilter(["mailA", "mailB"], False, |
|
191 |
namefield="id"), |
|
192 |
[qlang.OP_OR, [qlang.OP_EQUAL, "id", "mailA"], |
|
193 |
[qlang.OP_EQUAL, "id", "mailB"]]) |
|
194 |
|
|
189 | 195 |
def testForcedFilter(self): |
190 | 196 |
for i in [None, [], ["1", "2"], ["", "", ""], ["a", "b", "c", "d"]]: |
191 | 197 |
self.assertRaises(errors.OpPrereqError, qlang.MakeFilter, i, True) |
Also available in: Unified diff