We'll start with a simple empty query, which returns ok if it gets no
query argument, and an error if it gets any. We also move the
implementation of the non-implemented queries to the new querylib.py, so
we don't have to special-case them in ConfdProcessor.

Signed-off-by: Guido Trotter <ultrot...@google.com>
---
 Makefile.am           |    3 +-
 lib/confd/querylib.py |   79 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/confd/server.py   |   26 ++++++++++------
 3 files changed, 97 insertions(+), 11 deletions(-)
 create mode 100644 lib/confd/querylib.py

diff --git a/Makefile.am b/Makefile.am
index 9e4038f..ca838e3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -114,7 +114,8 @@ http_PYTHON = \
 
 confd_PYTHON = \
        lib/confd/__init__.py \
-       lib/confd/server.py
+       lib/confd/server.py \
+       lib/confd/querylib.py
 
 docrst = \
        doc/admin.rst \
diff --git a/lib/confd/querylib.py b/lib/confd/querylib.py
new file mode 100644
index 0000000..beeb217
--- /dev/null
+++ b/lib/confd/querylib.py
@@ -0,0 +1,79 @@
+#!/usr/bin/python
+#
+
+# Copyright (C) 2009, Google Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+
+"""Ganeti configuration daemon queries library.
+
+"""
+
+from ganeti import constants
+
+
+class ConfdQuery(object):
+  """Confd Query base class.
+
+  """
+
+  def __init__(self, reader):
+    """Constructor for Confd Query
+
+    @type reader: L{ssconf.SimpleConfigReader}
+    @param reader: ConfigReader to use to access the config
+
+    """
+    self.reader = reader
+
+  def Exec(self, query):
+    """Process a single UDP request from a client.
+
+    Different queries should override this function, which by defaults returns
+    a "non-implemented" answer.
+
+    @type query: (undefined)
+    @param query: ConfdRequest 'query' field
+    @rtype: (integer, undefined)
+    @return: status and answer to give to the client
+
+    """
+    status = constants.CONFD_REPL_STATUS_NOTIMPLEMENTED
+    answer = 'not implemented'
+    return status, answer
+
+
+class EmptyQuery(ConfdQuery):
+  """An empty confd query.
+
+  It will return success on an empty argument, and an error on any other 
argument.
+
+  """
+
+  def Exec(self, query):
+    """EmptyQuery main execution
+
+    """
+    if query is None:
+      status = constants.CONFD_REPL_STATUS_OK
+      answer = 'ok'
+    else:
+      status = constants.CONFD_REPL_STATUS_ERROR
+      answer = 'non-empty empty query :)'
+
+    return status, answer
+
diff --git a/lib/confd/server.py b/lib/confd/server.py
index 94ebd82..ed5f9e1 100644
--- a/lib/confd/server.py
+++ b/lib/confd/server.py
@@ -35,12 +35,17 @@ from ganeti import errors
 from ganeti import utils
 from ganeti import serializer
 
+from ganeti.confd import querylib
+
 
 class ConfdProcessor(object):
   """A processor for confd requests.
 
   """
   DISPATCH_TABLE = {
+      constants.CONFD_REQ_EMPTY: querylib.EmptyQuery,
+      constants.CONFD_REQ_NODE_ROLE_BYNAME: querylib.ConfdQuery,
+      constants.CONFD_REQ_NODE_PIP_BY_INSTANCE_IP: querylib.ConfdQuery,
   }
 
   def __init__(self, reader):
@@ -127,16 +132,17 @@ class ConfdProcessor(object):
       raise errors.ConfdRequestError(msg)
 
     if request.type not in self.DISPATCH_TABLE:
-      answer = 'not implemented'
-      status = constants.CONFD_REPL_STATUS_NOTIMPLEMENTED
-      reply = objects.ConfdReply(
-                protocol=constants.CONFD_PROTOCOL_VERSION,
-                status=status,
-                answer=answer,
-                )
-    else:
-      # TODO: actually dispatch queries to some classes to handle them
-      assert False, "DISPATCH_TABLE is populated but handler is not"
+      msg = "Valid request %d not in DISPATCH_TABLE" % request.type
+      raise errors.ProgrammerError(msg)
+
+    query_object = self.DISPATCH_TABLE[request.type](self.reader)
+    status, answer = query_object.Exec(request.query)
+    reply = objects.ConfdReply(
+              protocol=constants.CONFD_PROTOCOL_VERSION,
+              status=status,
+              answer=answer,
+              serial=self.reader.GetConfigSerialNo(),
+              )
 
     logging.debug("Sending reply: %s" % reply)
 
-- 
1.5.6.5

Reply via email to