From aba8e78d7d488d634721dac93279b0cd610535f7 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Fri, 19 Nov 2010 21:26:04 +0100
Subject: [PATCH] cmdlib: Add base for query classes

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/cmdlib.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 4c10658e7..da80e8aed 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -54,6 +54,8 @@ from ganeti import compat
 from ganeti import masterd
 from ganeti import netutils
 from ganeti import ht
+from ganeti import query
+from ganeti import qlang
 
 import ganeti.masterd.instance # pylint: disable-msg=W0611
 
@@ -486,6 +488,83 @@ class Tasklet:
     raise NotImplementedError
 
 
+class _QueryBase:
+  """Base for query utility classes.
+
+  """
+  #: Attribute holding field definitions
+  FIELDS = None
+
+  def __init__(self, names, fields, use_locking):
+    """Initializes this class.
+
+    """
+    self.names = names
+    self.use_locking = use_locking
+
+    self.query = query.Query(self.FIELDS, fields)
+    self.requested_data = self.query.RequestedData()
+
+  @classmethod
+  def FieldsQuery(cls, fields):
+    """Returns list of available fields.
+
+    @return: List of L{objects.QueryFieldDefinition}
+
+    """
+    if fields is None:
+      # Client requests all fields
+      fdefs = query.GetAllFields(cls.FIELDS.values())
+    else:
+      fdefs = query.Query(cls.FIELDS, fields).GetFields()
+
+    return {
+      "fields": [fdef.ToDict() for fdef in fdefs],
+      }
+
+  def ExpandNames(self, lu):
+    """Expand names for this query.
+
+    See L{LogicalUnit.ExpandNames}.
+
+    """
+    raise NotImplementedError()
+
+  def DeclareLocks(self, level):
+    """Declare locks for this query.
+
+    See L{LogicalUnit.DeclareLocks}.
+
+    """
+    raise NotImplementedError()
+
+  def _GetQueryData(self, lu):
+    """Collects all data for this query.
+
+    @return: Query data object
+
+    """
+    raise NotImplementedError()
+
+  def NewStyleQuery(self, lu):
+    """Collect data and execute query.
+
+    """
+    data = self._GetQueryData(lu)
+
+    return {
+      "data": self.query.Query(data),
+      "fields": [fdef.ToDict()
+                 for fdef in self.query.GetFields()],
+      }
+
+  def OldStyleQuery(self, lu):
+    """Collect data and execute query.
+
+    """
+    return self.query.OldStyleQuery(self._GetQueryData(lu))
+
+
 def _GetWantedNodes(lu, nodes):
   """Returns list of checked and expanded node names.
 
-- 
GitLab