Commit aa29e95f authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Move QueryFields to query module



Also replace “sorted” with “utils.NiceSort” now that it supports a key
function.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 82d25bbb
......@@ -39,7 +39,6 @@ import OpenSSL
import socket
import tempfile
import shutil
import operator
import itertools
from ganeti import ssh
......@@ -559,15 +558,7 @@ class _QueryBase:
@return: List of L{objects.QueryFieldDefinition}
"""
if fields is None:
# Client requests all fields, sort by name
fdefs = sorted(query.GetAllFields(cls.FIELDS.values()),
key=operator.attrgetter("name"))
else:
# Keep order as requested by client
fdefs = query.Query(cls.FIELDS, fields).GetFields()
return objects.QueryFieldsResponse(fields=fdefs).ToDict()
return query.QueryFields(cls.FIELDS, fields)
def ExpandNames(self, lu):
"""Expand names for this query.
......
......@@ -235,6 +235,27 @@ def _PrepareFieldList(fields):
return result
def QueryFields(fielddefs, selected):
"""Returns list of available fields.
@type fielddefs: dict
@param fielddefs: Field definitions
@type selected: list of strings
@param selected: List of selected fields
@return: List of L{objects.QueryFieldDefinition}
"""
if selected is None:
# Client requests all fields, sort by name
fdefs = utils.NiceSort(GetAllFields(fielddefs.values()),
key=operator.attrgetter("name"))
else:
# Keep order as requested by client
fdefs = Query(fielddefs, selected).GetFields()
return objects.QueryFieldsResponse(fields=fdefs).ToDict()
def _MakeField(name, title, kind):
"""Wrapper for creating L{objects.QueryFieldDefinition} instances.
......
......@@ -23,6 +23,7 @@
import re
import unittest
import random
from ganeti import constants
from ganeti import utils
......@@ -752,5 +753,31 @@ class TestInstanceQuery(unittest.TestCase):
"ADMIN_down"]))
class TestQueryFields(unittest.TestCase):
def testAllFields(self):
for fielddefs in [query.NODE_FIELDS, query.INSTANCE_FIELDS]:
result = query.QueryFields(fielddefs, None)
self.assert_(isinstance(result, dict))
response = objects.QueryFieldsResponse.FromDict(result)
self.assertEqual([(fdef.name, fdef.title) for fdef in response.fields],
[(fdef2.name, fdef2.title)
for (fdef2, _, _) in utils.NiceSort(fielddefs.values(),
key=lambda x: x[0].name)])
def testSomeFields(self):
rnd = random.Random(5357)
for _ in range(10):
for fielddefs in [query.NODE_FIELDS, query.INSTANCE_FIELDS]:
fields = [fdef
for (fdef, _, _) in rnd.sample(fielddefs.values(),
rnd.randint(5, 20))]
result = query.QueryFields(fielddefs, [fdef.name for fdef in fields])
self.assert_(isinstance(result, dict))
response = objects.QueryFieldsResponse.FromDict(result)
self.assertEqual([(fdef.name, fdef.title) for fdef in response.fields],
[(fdef2.name, fdef2.title) for fdef2 in fields])
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