Browse Source

[2853] Add Python binding for getStatus() method

Mukund Sivaraman 12 years ago
parent
commit
fae036eecd

+ 56 - 0
src/lib/python/isc/datasrc/configurableclientlist_python.cc

@@ -196,6 +196,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 =
         static_cast<s_ConfigurableClientList*>(po_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

+ 23 - 0
src/lib/python/isc/datasrc/tests/clientlist_test.py

@@ -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()