From 250554a95b0f1676c7908378099d25b86f4caed6 Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Wed, 2 Dec 2009 13:51:58 +0100
Subject: [PATCH] confd ClusterMasterQuery: allow fields request

Change the ClusterMasterQuery to allow a query, and if present accept a
list of fields to be returned. Currently only name and ip are accepted.

This feature will be used by NLD to route the cluster ip over the nbma.

Backwards compatibility is preserved.

Signed-off-by: Guido Trotter <ultrotter@google.com>
---
 lib/confd/querylib.py | 23 ++++++++++++++++++++---
 lib/constants.py      |  4 ++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/lib/confd/querylib.py b/lib/confd/querylib.py
index 17621dd0c..f757760df 100644
--- a/lib/confd/querylib.py
+++ b/lib/confd/querylib.py
@@ -98,12 +98,29 @@ class ClusterMasterQuery(ConfdQuery):
     """ClusterMasterQuery main execution
 
     """
-    if query is None:
+    if isinstance(query, dict):
+      if constants.CONFD_REQQ_FIELDS in query:
+        status = constants.CONFD_REPL_STATUS_OK
+        req_fields = query[constants.CONFD_REQQ_FIELDS]
+        if not isinstance(req_fields, (list, tuple)):
+          logging.debug("FIELDS request should be a list")
+          return QUERY_ARGUMENT_ERROR
+
+        answer = []
+        for field in req_fields:
+          if field == constants.CONFD_REQFIELD_NAME:
+            answer.append(self.reader.GetMasterNode())
+          elif field == constants.CONFD_REQFIELD_IP:
+            answer.append(self.reader.GetMasterIP())
+      else:
+        logging.debug("missing FIELDS in query dict")
+        return QUERY_ARGUMENT_ERROR
+    elif not query:
       status = constants.CONFD_REPL_STATUS_OK
       answer = self.reader.GetMasterNode()
     else:
-      status = constants.CONFD_REPL_STATUS_ERROR
-      answer = 'master query accepts no query argument'
+      logging.debug("Invalid master query argument: not dict or empty")
+      return QUERY_ARGUMENT_ERROR
 
     return status, answer
 
diff --git a/lib/constants.py b/lib/constants.py
index 9ff39588c..07493cd3c 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -687,6 +687,10 @@ CONFD_REQ_INSTANCES_IPS_LIST = 6
 CONFD_REQQ_LINK = "0"
 CONFD_REQQ_IP = "1"
 CONFD_REQQ_IPLIST = "2"
+CONFD_REQQ_FIELDS = "3"
+
+CONFD_REQFIELD_NAME = "0"
+CONFD_REQFIELD_IP = "1"
 
 CONFD_REQS = frozenset([
   CONFD_REQ_PING,
-- 
GitLab