]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2853] Add Python binding for getStatus() method
authorMukund Sivaraman <muks@isc.org>
Fri, 7 Jun 2013 07:45:28 +0000 (13:15 +0530)
committerMukund Sivaraman <muks@isc.org>
Fri, 7 Jun 2013 07:55:22 +0000 (13:25 +0530)
src/lib/python/isc/datasrc/configurableclientlist_python.cc
src/lib/python/isc/datasrc/tests/clientlist_test.py

index 36b6cf972ee7abc635e25ee6cab3de4d2d0720d7..b33d04343ba2d0736c65ae209a45d0f290c9e25d 100644 (file)
@@ -195,6 +195,44 @@ ConfigurableClientList_getCachedZoneWriter(PyObject* po_self, PyObject* args) {
     }
 }
 
+PyObject*
+ConfigurableClientList_getStatus(PyObject* po_self, PyObject*) {
+    s_ConfigurableClientList* self =
+        static_cast<s_ConfigurableClientList*>(po_self);
+    try {
+        const std::vector<DataSourceStatus> status = self->cppobj->getStatus();
+        if (status.empty()) {
+            Py_RETURN_NONE;
+        }
+
+        PyObject *slist = PyList_New(status.size());
+        if (!slist) {
+            return (NULL);
+        }
+
+        for (size_t i = 0; i < status.size(); ++i) {
+            PyObject *tup = Py_BuildValue("(ssI)",
+                                          status[i].getName().c_str(),
+                                          status[i].getSegmentType().c_str(),
+                                          status[i].getSegmentState());
+            if (!tup) {
+                Py_DECREF(slist);
+                return (NULL);
+            }
+            PyList_SET_ITEM(slist, i, tup);
+        }
+
+        return (slist);
+    } catch (const std::exception& exc) {
+        PyErr_SetString(getDataSourceException("Error"), exc.what());
+        return (NULL);
+    } catch (...) {
+        PyErr_SetString(getDataSourceException("Error"),
+                        "Unknown C++ exception");
+        return (NULL);
+    }
+}
+
 PyObject*
 ConfigurableClientList_find(PyObject* po_self, PyObject* args) {
     s_ConfigurableClientList* self =
@@ -295,6 +333,13 @@ This returns a ZoneWriter that can be used to (re)load a zone.\n\
 Parameters:\n\
   zone              The name of the zone to (re)load.\
   datasrc_name      The name of the data source where the zone is to be loaded." },
+    { "get_status", ConfigurableClientList_getStatus,
+      METH_NOARGS,
+        "get_status() -> list\n\
+\n\
+Wrapper around C++ ConfigurableClientList::getStatus\n\
+\n\
+This returns a list of tuples, each containing the status of a data source client." },
     { "find", ConfigurableClientList_find, METH_VARARGS,
 "find(zone, want_exact_match=False, want_finder=True) -> datasrc_client,\
 zone_finder, exact_match\n\
@@ -425,6 +470,17 @@ initModulePart_ConfigurableClientList(PyObject* mod) {
                              "CACHE_STATUS_ZONE_SUCCESS",
                              Py_BuildValue("I", ConfigurableClientList::ZONE_SUCCESS));
 
+        // MemorySegmentState enum
+        installClassVariable(configurableclientlist_type,
+                             "SEGMENT_UNUSED",
+                             Py_BuildValue("I", SEGMENT_UNUSED));
+        installClassVariable(configurableclientlist_type,
+                             "SEGMENT_WAITING",
+                             Py_BuildValue("I", SEGMENT_WAITING));
+        installClassVariable(configurableclientlist_type,
+                             "SEGMENT_INUSE",
+                             Py_BuildValue("I", SEGMENT_INUSE));
+
         // FIXME: These should eventually be moved to the
         // ZoneTableSegment class when we add Python bindings for the
         // memory data source specific bits. But for now, we add these
index 499e0121ec9121e6e2e2d22f5e220b1d693288a0..c24a10b67210c0a802e82c2cf304d12f4ba2009e 100644 (file)
@@ -205,6 +205,29 @@ class ClientListTest(unittest.TestCase):
         # The segment is still in READ_ONLY mode.
         self.find_helper()
 
+    def test_get_status(self):
+        """
+        Test getting status of various data sources.
+        """
+
+        self.clist = isc.datasrc.ConfigurableClientList(isc.dns.RRClass.IN)
+
+        status = self.clist.get_status()
+        self.assertIsNone(status)
+
+        self.clist.configure('''[{
+            "type": "MasterFiles",
+            "params": {
+                "example.org": "''' + TESTDATA_PATH + '''example.org.zone"
+            },
+            "cache-enable": true
+        }]''', True)
+
+        status = self.clist.get_status()
+        self.assertEqual(1, len(status))
+        self.assertTupleEqual(('MasterFiles', 'local', isc.datasrc.ConfigurableClientList.SEGMENT_INUSE),
+                              status[0])
+
 if __name__ == "__main__":
     isc.log.init("bind10")
     isc.log.resetUnitTestRootLogger()