[PATCH python 1/2] Python binding for storage pool lifecycle events API

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 generator.py                   |   2 +
 libvirt-override-virConnect.py |  35 ++++++++++
 libvirt-override.c             | 153 +++++++++++++++++++++++++++++++++++++++++
 sanitytest.py                  |   3 +-
 4 files changed, 192 insertions(+), 1 deletion(-)

diff --git a/generator.py b/generator.py
index 3144904..92ac7a7 100755
--- a/generator.py
+++ b/generator.py
@@ -522,6 +522,8 @@ skip_function = (
     'virConnectDomainEventDeregisterAny', # overridden in virConnect.py
     'virConnectNetworkEventRegisterAny',   # overridden in virConnect.py
     'virConnectNetworkEventDeregisterAny', # overridden in virConnect.py
+    'virConnectStoragePoolEventRegisterAny',   # overridden in virConnect.py
+    'virConnectStoragePoolEventDeregisterAny', # overridden in virConnect.py
     'virSaveLastError', # We have our own python error wrapper
     'virFreeError', # Only needed if we use virSaveLastError
     'virConnectListAllDomains', # overridden in virConnect.py
diff --git a/libvirt-override-virConnect.py b/libvirt-override-virConnect.py
index 0f76c59..1aaa586 100644
--- a/libvirt-override-virConnect.py
+++ b/libvirt-override-virConnect.py
@@ -302,6 +302,41 @@
         self.domainEventCallbackID[ret] = opaque
         return ret
 
+    def _dispatchStoragePoolEventLifecycleCallback(self, pool, event, detail, cbData):
+        """Dispatches events to python user storage pool
+           lifecycle event callbacks
+        """
+        cb = cbData["cb"]
+        opaque = cbData["opaque"]
+
+        cb(self, virStoragePool(self, _obj=pool), event, detail, opaque)
+        return 0
+
+    def storagePoolEventDeregisterAny(self, callbackID):
+        """Removes a Storage Pool Event Callback. De-registering for a
+           storage pool callback will disable delivery of this event type"""
+        try:
+            ret = libvirtmod.virConnectStoragePoolEventDeregisterAny(self._o, callbackID)
+            if ret == -1: raise libvirtError ('virConnectStoragePoolEventDeregisterAny() failed', conn=self)
+            del self.storagePoolEventCallbackID[callbackID]
+        except AttributeError:
+            pass
+
+    def storagePoolEventRegisterAny(self, pool, eventID, cb, opaque):
+        """Adds a Storage Pool Event Callback. Registering for a storage pool
+           callback will enable delivery of the events"""
+        if not hasattr(self, 'storagePoolEventCallbackID'):
+            self.storagePoolEventCallbackID = {}
+        cbData = { "cb": cb, "conn": self, "opaque": opaque }
+        if pool is None:
+            ret = libvirtmod.virConnectStoragePoolEventRegisterAny(self._o, None, eventID, cbData)
+        else:
+            ret = libvirtmod.virConnectStoragePoolEventRegisterAny(self._o, pool._o, eventID, cbData)
+        if ret == -1:
+            raise libvirtError ('virConnectStoragePoolEventRegisterAny() failed', conn=self)
+        self.storagePoolEventCallbackID[ret] = opaque
+        return ret
+
     def listAllDomains(self, flags=0):
         """List all domains and returns a list of domain objects"""
         ret = libvirtmod.virConnectListAllDomains(self._o, flags)
diff --git a/libvirt-override.c b/libvirt-override.c
index 2de95ce..01540e9 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -8768,6 +8768,155 @@ libvirt_virDomainSetPerfEvents(PyObject *self ATTRIBUTE_UNUSED,
 }
 #endif /* LIBVIR_CHECK_VERSION(1, 3, 3) */
 
+#if LIBVIR_CHECK_VERSION(1, 3, 6)
+static void
+libvirt_virConnectStoragePoolEventFreeFunc(void *opaque)
+{
+    PyObject *pyobj_conn = (PyObject*)opaque;
+    LIBVIRT_ENSURE_THREAD_STATE;
+    Py_DECREF(pyobj_conn);
+    LIBVIRT_RELEASE_THREAD_STATE;
+}
+
+static int
+libvirt_virConnectStoragePoolEventLifecycleCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
+                                                    virStoragePoolPtr pool,
+                                                    int event,
+                                                    int detail,
+                                                    void *opaque)
+{
+    PyObject *pyobj_cbData = (PyObject*)opaque;
+    PyObject *pyobj_pool;
+    PyObject *pyobj_ret = NULL;
+    PyObject *pyobj_conn;
+    PyObject *dictKey;
+    int ret = -1;
+
+    LIBVIRT_ENSURE_THREAD_STATE;
+
+    if (!(dictKey = libvirt_constcharPtrWrap("conn")))
+        goto cleanup;
+    pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
+    Py_DECREF(dictKey);
+
+    /* Create a python instance of this virNetworkPtr */
+    virStoragePoolRef(pool);
+    if (!(pyobj_pool = libvirt_virStoragePoolPtrWrap(pool))) {
+        virStoragePoolFree(pool);
+        goto cleanup;
+    }
+    Py_INCREF(pyobj_cbData);
+
+    /* Call the Callback Dispatcher */
+    pyobj_ret = PyObject_CallMethod(pyobj_conn,
+                                    (char*)"_dispatchStoragePoolEventLifecycleCallback",
+                                    (char*)"OiiO",
+                                    pyobj_pool,
+                                    event,
+                                    detail,
+                                    pyobj_cbData);
+
+    Py_DECREF(pyobj_cbData);
+    Py_DECREF(pyobj_pool);
+
+ cleanup:
+    if (!pyobj_ret) {
+        DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
+        PyErr_Print();
+    } else {
+        Py_DECREF(pyobj_ret);
+        ret = 0;
+    }
+
+    LIBVIRT_RELEASE_THREAD_STATE;
+    return ret;
+}
+
+static PyObject *
+libvirt_virConnectStoragePoolEventRegisterAny(PyObject *self ATTRIBUTE_UNUSED,
+                                              PyObject *args)
+{
+    PyObject *pyobj_conn;       /* virConnectPtr */
+    PyObject *pyobj_pool;
+    PyObject *pyobj_cbData;     /* hash of callback data */
+    int eventID;
+    virConnectPtr conn;
+    int ret = 0;
+    virConnectStoragePoolEventGenericCallback cb = NULL;
+    virStoragePoolPtr pool;
+
+    if (!PyArg_ParseTuple(args,
+                          (char *) "OOiO:virConnectStoragePoolEventRegisterAny",
+                          &pyobj_conn, &pyobj_pool, &eventID, &pyobj_cbData))
+        return NULL;
+
+    DEBUG("libvirt_virConnectStoragePoolEventRegister(%p %p %d %p) called\n",
+          pyobj_conn, pyobj_pool, eventID, pyobj_cbData);
+    conn = PyvirConnect_Get(pyobj_conn);
+    if (pyobj_pool == Py_None)
+        pool = NULL;
+    else
+        pool = PyvirStoragePool_Get(pyobj_pool);
+
+    switch ((virStoragePoolEventID) eventID) {
+    case VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE:
+        cb = VIR_STORAGE_POOL_EVENT_CALLBACK(libvirt_virConnectStoragePoolEventLifecycleCallback);
+        break;
+
+    case VIR_STORAGE_POOL_EVENT_ID_LAST:
+        break;
+    }
+
+    if (!cb) {
+        return VIR_PY_INT_FAIL;
+    }
+
+    Py_INCREF(pyobj_cbData);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    ret = virConnectStoragePoolEventRegisterAny(conn,
+                                                pool,
+                                                eventID,
+                                                cb,
+                                                pyobj_cbData,
+                                                libvirt_virConnectStoragePoolEventFreeFunc);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (ret < 0) {
+        Py_DECREF(pyobj_cbData);
+    }
+
+    return libvirt_intWrap(ret);
+}
+
+static PyObject *
+libvirt_virConnectStoragePoolEventDeregisterAny(PyObject *self ATTRIBUTE_UNUSED,
+                                                PyObject *args)
+{
+    PyObject *pyobj_conn;
+    int callbackID;
+    virConnectPtr conn;
+    int ret = 0;
+
+    if (!PyArg_ParseTuple(args, (char *) "Oi:virConnectStoragePoolEventDeregister",
+                          &pyobj_conn, &callbackID))
+        return NULL;
+
+    DEBUG("libvirt_virConnectStoragePoolEventDeregister(%p) called\n",
+          pyobj_conn);
+
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+
+    ret = virConnectStoragePoolEventDeregisterAny(conn, callbackID);
+
+    LIBVIRT_END_ALLOW_THREADS;
+
+    return libvirt_intWrap(ret);
+}
+#endif /* LIBVIR_CHECK_VERSION(1, 3, 6)*/
+
 
 /************************************************************************
  *									*
@@ -8976,6 +9125,10 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virDomainGetPerfEvents", libvirt_virDomainGetPerfEvents, METH_VARARGS, NULL},
     {(char *) "virDomainSetPerfEvents", libvirt_virDomainSetPerfEvents, METH_VARARGS, NULL},
 #endif /* LIBVIR_CHECK_VERSION(1, 3, 3) */
+#if LIBVIR_CHECK_VERSION(1, 3, 6)
+    {(char *) "virConnectStoragePoolEventRegisterAny", libvirt_virConnectStoragePoolEventRegisterAny, METH_VARARGS, NULL},
+    {(char *) "virConnectStoragePoolEventDeregisterAny", libvirt_virConnectStoragePoolEventDeregisterAny, METH_VARARGS, NULL},
+#endif /* LIBVIR_CHECK_VERSION(1, 3, 6) */
     {NULL, NULL, 0, NULL}
 };
 
diff --git a/sanitytest.py b/sanitytest.py
index faabccb..3d4b28f 100644
--- a/sanitytest.py
+++ b/sanitytest.py
@@ -173,7 +173,8 @@ for cname in wantfunctions:
         continue
     if name[0:22] == "virConnectNetworkEvent" and name[-8:] == "Callback":
         continue
-
+    if (name.startswith("virConnectStoragePoolEvent") and name.endswith("Callback")):
+        continue
 
     # virEvent APIs go into main 'libvirt' namespace not any class
     if name[0:8] == "virEvent":
-- 
2.5.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]