mirror of
https://gitlab.com/libvirt/libvirt-python.git
synced 2025-07-28 15:41:52 +03:00
Python binding for storage pool lifecycle events API
Code matches the network event API implementation
This commit is contained in:
committed by
Cole Robinson
parent
b4629027f8
commit
7eaab4a6c2
@ -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
|
||||
|
@ -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)
|
||||
|
@ -8768,6 +8768,155 @@ libvirt_virDomainSetPerfEvents(PyObject *self ATTRIBUTE_UNUSED,
|
||||
}
|
||||
#endif /* LIBVIR_CHECK_VERSION(1, 3, 3) */
|
||||
|
||||
#if LIBVIR_CHECK_VERSION(2, 0, 0)
|
||||
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(2, 0, 0)*/
|
||||
|
||||
|
||||
/************************************************************************
|
||||
* *
|
||||
@ -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(2, 0, 0)
|
||||
{(char *) "virConnectStoragePoolEventRegisterAny", libvirt_virConnectStoragePoolEventRegisterAny, METH_VARARGS, NULL},
|
||||
{(char *) "virConnectStoragePoolEventDeregisterAny", libvirt_virConnectStoragePoolEventDeregisterAny, METH_VARARGS, NULL},
|
||||
#endif /* LIBVIR_CHECK_VERSION(2, 0, 0) */
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
|
@ -173,6 +173,9 @@ 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
|
||||
|
Reference in New Issue
Block a user