Added python binding for storage APIs

This commit is contained in:
Daniel P. Berrange 2008-02-20 15:26:22 +00:00
parent 661c5c4476
commit 859679f72a
5 changed files with 442 additions and 3 deletions

View File

@ -1,3 +1,8 @@
Wed Feb 20 10:20:27 EST 2008 Daniel P. Berrange <berrange@redhat.com>
* python/generator.py, python/libvir.c, python/libvirt_wrap.h,
python/types.c: Add binding for storage APIs
Wed Feb 20 10:18:27 EST 2008 Daniel P. Berrange <berrange@redhat.com>
* src/remote_internal.c: Implement storage APIs in remote

View File

@ -233,14 +233,27 @@ py_types = {
'unsigned char *': ('z', None, "charPtr", "char *"),
'char *': ('z', None, "charPtr", "char *"),
'const char *': ('z', None, "charPtrConst", "const char *"),
'virDomainPtr': ('O', "virDomain", "virDomainPtr", "virDomainPtr"),
'const virDomainPtr': ('O', "virDomain", "virDomainPtr", "virDomainPtr"),
'virDomain *': ('O', "virDomain", "virDomainPtr", "virDomainPtr"),
'const virDomain *': ('O', "virDomain", "virDomainPtr", "virDomainPtr"),
'virNetworkPtr': ('O', "virNetwork", "virNetworkPtr", "virNetworkPtr"),
'const virNetworkPtr': ('O', "virNetwork", "virNetworkPtr", "virNetworkPtr"),
'virNetwork *': ('O', "virNetwork", "virNetworkPtr", "virNetworkPtr"),
'const virNetwork *': ('O', "virNetwork", "virNetworkPtr", "virNetworkPtr"),
'virStoragePoolPtr': ('O', "virStoragePool", "virStoragePoolPtr", "virStoragePoolPtr"),
'const virStoragePoolPtr': ('O', "virStoragePool", "virStoragePoolPtr", "virStoragePoolPtr"),
'virStoragePool *': ('O', "virStoragePool", "virStoragePoolPtr", "virStoragePoolPtr"),
'const virStoragePool *': ('O', "virStoragePool", "virStoragePoolPtr", "virStoragePoolPtr"),
'virStorageVolPtr': ('O', "virStorageVol", "virStorageVolPtr", "virStorageVolPtr"),
'const virStorageVolPtr': ('O', "virStorageVol", "virStorageVolPtr", "virStorageVolPtr"),
'virStorageVol *': ('O', "virStorageVol", "virStorageVolPtr", "virStorageVolPtr"),
'const virStorageVol *': ('O', "virStorageVol", "virStorageVolPtr", "virStorageVolPtr"),
'virConnectPtr': ('O', "virConnect", "virConnectPtr", "virConnectPtr"),
'const virConnectPtr': ('O', "virConnect", "virConnectPtr", "virConnectPtr"),
'virConnect *': ('O', "virConnect", "virConnectPtr", "virConnectPtr"),
@ -269,6 +282,10 @@ skip_impl = (
'virConnectListDefinedDomains',
'virConnectListNetworks',
'virConnectListDefinedNetworks',
'virConnectListStoragePools',
'virConnectListDefinedStoragePools',
'virConnectListStorageVols',
'virConnectListDefinedStorageVols',
'virConnGetLastError',
'virGetLastError',
'virDomainGetInfo',
@ -287,6 +304,14 @@ skip_impl = (
'virDomainSetSchedulerParameters',
'virDomainGetVcpus',
'virDomainPinVcpu',
'virStoragePoolGetUUID',
'virStoragePoolLookupByUUID',
'virStorageVolGetUUID',
'virStorageVolLookupByUUID',
'virStoragePoolGetInfo',
'virStorageVolGetInfo',
'virStoragePoolGetAutostart',
'virStoragePoolListVolumes',
)
@ -564,6 +589,10 @@ classes_type = {
"virDomain *": ("._o", "virDomain(self, _obj=%s)", "virDomain"),
"virNetworkPtr": ("._o", "virNetwork(self, _obj=%s)", "virNetwork"),
"virNetwork *": ("._o", "virNetwork(self, _obj=%s)", "virNetwork"),
"virStoragePoolPtr": ("._o", "virStoragePool(self, _obj=%s)", "virStoragePool"),
"virStoragePool *": ("._o", "virStoragePool(self, _obj=%s)", "virStoragePool"),
"virStorageVolPtr": ("._o", "virStorageVol(self, _obj=%s)", "virStorageVol"),
"virStorageVol *": ("._o", "virStorageVol(self, _obj=%s)", "virStorageVol"),
"virConnectPtr": ("._o", "virConnect(_obj=%s)", "virConnect"),
"virConnect *": ("._o", "virConnect(_obj=%s)", "virConnect"),
}
@ -571,19 +600,25 @@ classes_type = {
converter_type = {
}
primary_classes = ["virDomain", "virNetwork", "virConnect"]
primary_classes = ["virDomain", "virNetwork", "virStoragePool", "virStorageVol", "virConnect"]
classes_ancestor = {
}
classes_destructors = {
"virDomain": "virDomainFree",
"virNetwork": "virNetworkFree",
"virStoragePool": "virStoragePoolFree",
"virStorageVol": "virStorageVolFree",
"virConnect": "virConnectClose",
}
functions_noexcept = {
'virDomainGetID': True,
'virDomainGetName': True,
'virNetworkGetName': True,
'virStoragePoolGetName': True,
'virStorageVolGetName': True,
'virStorageVolGetkey': True,
}
reference_keepers = {
@ -596,6 +631,8 @@ function_classes["None"] = []
function_post = {
'virDomainDestroy': "self._o = None",
'virNetworkDestroy': "self._o = None",
'virStoragePoolDestroy': "self._o = None",
'virStorageVolDestroy': "self._o = None",
}
# Functions returning an integral type which need special rules to
@ -631,6 +668,18 @@ def nameFixup(name, classe, type, file):
elif name[0:16] == "virNetworkLookup":
func = name[3:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:20] == "virStoragePoolDefine":
func = name[3:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:20] == "virStoragePoolLookup":
func = name[3:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:19] == "virStorageVolDefine":
func = name[3:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:19] == "virStorageVolLookup":
func = name[3:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:12] == "virDomainGet":
func = name[12:]
func = string.lower(func[0:1]) + func[1:]
@ -643,6 +692,18 @@ def nameFixup(name, classe, type, file):
elif name[0:10] == "virNetwork":
func = name[10:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:17] == "virStoragePoolGet":
func = name[17:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:14] == "virStoragePool":
func = name[14:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:16] == "virStorageVolGet":
func = name[16:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:13] == "virStorageVol":
func = name[13:]
func = string.lower(func[0:1]) + func[1:]
elif name[0:7] == "virNode":
func = name[7:]
func = string.lower(func[0:1]) + func[1:]
@ -892,7 +953,7 @@ def buildWrappers():
else:
txt.write("Class %s()\n" % (classname))
classes.write("class %s:\n" % (classname))
if classname == "virDomain" or classname == "virNetwork":
if classname in [ "virDomain", "virNetwork", "virStoragePool", "virStorageVol" ]:
classes.write(" def __init__(self, conn, _obj=None):\n")
else:
classes.write(" def __init__(self, _obj=None):\n")
@ -900,7 +961,7 @@ def buildWrappers():
list = reference_keepers[classname]
for ref in list:
classes.write(" self.%s = None\n" % ref[1])
if classname == "virDomain" or classname == "virNetwork":
if classname in [ "virDomain", "virNetwork", "virStoragePool", "virStorageVol" ]:
classes.write(" self._conn = conn\n")
classes.write(" if _obj != None:self._o = _obj;return\n")
classes.write(" self._o = None\n\n");
@ -994,6 +1055,14 @@ def buildWrappers():
classes.write(
" if ret is None:raise libvirtError('%s() failed', net=self)\n" %
(name))
elif classname == "virStoragePool":
classes.write(
" if ret is None:raise libvirtError('%s() failed', pool=self)\n" %
(name))
elif classname == "virStorageVol":
classes.write(
" if ret is None:raise libvirtError('%s() failed', vol=self)\n" %
(name))
else:
classes.write(
" if ret is None:raise libvirtError('%s() failed')\n" %
@ -1066,6 +1135,14 @@ def buildWrappers():
classes.write ((" if " + test +
": raise libvirtError ('%s() failed', net=self)\n") %
("ret", name))
elif classname == "virStoragePool":
classes.write ((" if " + test +
": raise libvirtError ('%s() failed', pool=self)\n") %
("ret", name))
elif classname == "virStorageVol":
classes.write ((" if " + test +
": raise libvirtError ('%s() failed', vol=self)\n") %
("ret", name))
else:
classes.write ((" if " + test +
": raise libvirtError ('%s() failed')\n") %
@ -1096,6 +1173,14 @@ def buildWrappers():
classes.write ((" if " + test +
": raise libvirtError ('%s() failed', net=self)\n") %
("ret", name))
elif classname == "virStoragePool":
classes.write ((" if " + test +
": raise libvirtError ('%s() failed', pool=self)\n") %
("ret", name))
elif classname == "virStorageVol":
classes.write ((" if " + test +
": raise libvirtError ('%s() failed', vol=self)\n") %
("ret", name))
else:
classes.write ((" if " + test +
": raise libvirtError ('%s() failed')\n") %

View File

@ -1099,6 +1099,291 @@ error:
return(py_retval);
}
static PyObject *
libvirt_virConnectListStoragePools(PyObject *self ATTRIBUTE_UNUSED,
PyObject *args) {
PyObject *py_retval;
char **names = NULL;
int c_retval, i;
virConnectPtr conn;
PyObject *pyobj_conn;
if (!PyArg_ParseTuple(args, (char *)"O:virConnectListStoragePools", &pyobj_conn))
return(NULL);
conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
c_retval = virConnectNumOfStoragePools(conn);
if (c_retval < 0)
return VIR_PY_NONE;
if (c_retval) {
names = malloc(sizeof(*names) * c_retval);
if (!names)
return VIR_PY_NONE;
c_retval = virConnectListStoragePools(conn, names, c_retval);
if (c_retval < 0) {
free(names);
return VIR_PY_NONE;
}
}
py_retval = PyList_New(c_retval);
if (py_retval == NULL) {
if (names) {
for (i = 0;i < c_retval;i++)
free(names[i]);
free(names);
}
return VIR_PY_NONE;
}
if (names) {
for (i = 0;i < c_retval;i++) {
PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
free(names[i]);
}
free(names);
}
return(py_retval);
}
static PyObject *
libvirt_virConnectListDefinedStoragePools(PyObject *self ATTRIBUTE_UNUSED,
PyObject *args) {
PyObject *py_retval;
char **names = NULL;
int c_retval, i;
virConnectPtr conn;
PyObject *pyobj_conn;
if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedStoragePools", &pyobj_conn))
return(NULL);
conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
c_retval = virConnectNumOfDefinedStoragePools(conn);
if (c_retval < 0)
return VIR_PY_NONE;
if (c_retval) {
names = malloc(sizeof(*names) * c_retval);
if (!names)
return VIR_PY_NONE;
c_retval = virConnectListDefinedStoragePools(conn, names, c_retval);
if (c_retval < 0) {
free(names);
return VIR_PY_NONE;
}
}
py_retval = PyList_New(c_retval);
if (py_retval == NULL) {
if (names) {
for (i = 0;i < c_retval;i++)
free(names[i]);
free(names);
}
return VIR_PY_NONE;
}
if (names) {
for (i = 0;i < c_retval;i++) {
PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
free(names[i]);
}
free(names);
}
return(py_retval);
}
static PyObject *
libvirt_virStoragePoolListVolumes(PyObject *self ATTRIBUTE_UNUSED,
PyObject *args) {
PyObject *py_retval;
char **names = NULL;
int c_retval, i;
virStoragePoolPtr pool;
PyObject *pyobj_pool;
if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolListVolumes", &pyobj_pool))
return(NULL);
pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
c_retval = virStoragePoolNumOfVolumes(pool);
if (c_retval < 0)
return VIR_PY_NONE;
if (c_retval) {
names = malloc(sizeof(*names) * c_retval);
if (!names)
return VIR_PY_NONE;
c_retval = virStoragePoolListVolumes(pool, names, c_retval);
if (c_retval < 0) {
free(names);
return VIR_PY_NONE;
}
}
py_retval = PyList_New(c_retval);
if (py_retval == NULL) {
if (names) {
for (i = 0;i < c_retval;i++)
free(names[i]);
free(names);
}
return VIR_PY_NONE;
}
if (names) {
for (i = 0;i < c_retval;i++) {
PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
free(names[i]);
}
free(names);
}
return(py_retval);
}
static PyObject *
libvirt_virStoragePoolGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
PyObject *py_retval;
int c_retval, autostart;
virStoragePoolPtr pool;
PyObject *pyobj_pool;
if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetAutostart", &pyobj_pool))
return(NULL);
pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virStoragePoolGetAutostart(pool, &autostart);
LIBVIRT_END_ALLOW_THREADS;
if (c_retval < 0)
return VIR_PY_NONE;
py_retval = libvirt_intWrap(autostart);
return(py_retval);
}
static PyObject *
libvirt_virStoragePoolGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
PyObject *py_retval;
int c_retval;
virStoragePoolPtr pool;
PyObject *pyobj_pool;
virStoragePoolInfo info;
if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetInfo", &pyobj_pool))
return(NULL);
pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virStoragePoolGetInfo(pool, &info);
LIBVIRT_END_ALLOW_THREADS;
if (c_retval < 0)
return VIR_PY_NONE;
if ((py_retval = PyList_New(4)) == NULL)
return VIR_PY_NONE;
PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.state));
PyList_SetItem(py_retval, 1,
libvirt_longlongWrap((unsigned long long) info.capacity));
PyList_SetItem(py_retval, 2,
libvirt_longlongWrap((unsigned long long) info.allocation));
PyList_SetItem(py_retval, 3,
libvirt_longlongWrap((unsigned long long) info.available));
return(py_retval);
}
static PyObject *
libvirt_virStorageVolGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
PyObject *py_retval;
int c_retval;
virStorageVolPtr pool;
PyObject *pyobj_pool;
virStorageVolInfo info;
if (!PyArg_ParseTuple(args, (char *)"O:virStorageVolGetInfo", &pyobj_pool))
return(NULL);
pool = (virStorageVolPtr) PyvirStorageVol_Get(pyobj_pool);
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virStorageVolGetInfo(pool, &info);
LIBVIRT_END_ALLOW_THREADS;
if (c_retval < 0)
return VIR_PY_NONE;
if ((py_retval = PyList_New(3)) == NULL)
return VIR_PY_NONE;
PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.type));
PyList_SetItem(py_retval, 1,
libvirt_longlongWrap((unsigned long long) info.capacity));
PyList_SetItem(py_retval, 2,
libvirt_longlongWrap((unsigned long long) info.allocation));
return(py_retval);
}
static PyObject *
libvirt_virStoragePoolGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
PyObject *py_retval;
unsigned char uuid[VIR_UUID_BUFLEN];
virStoragePoolPtr pool;
PyObject *pyobj_pool;
int c_retval;
if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetUUID", &pyobj_pool))
return(NULL);
pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
if (pool == NULL)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virStoragePoolGetUUID(pool, &uuid[0]);
LIBVIRT_END_ALLOW_THREADS;
if (c_retval < 0)
return VIR_PY_NONE;
py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
return(py_retval);
}
static PyObject *
libvirt_virStoragePoolLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
PyObject *py_retval;
virStoragePoolPtr c_retval;
virConnectPtr conn;
PyObject *pyobj_conn;
unsigned char * uuid;
int len;
if (!PyArg_ParseTuple(args, (char *)"Oz#:virStoragePoolLookupByUUID", &pyobj_conn, &uuid, &len))
return(NULL);
conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virStoragePoolLookupByUUID(conn, uuid);
LIBVIRT_END_ALLOW_THREADS;
py_retval = libvirt_virStoragePoolPtrWrap((virStoragePoolPtr) c_retval);
return(py_retval);
}
/************************************************************************
* *
* The registration stuff *
@ -1131,6 +1416,14 @@ static PyMethodDef libvirtMethods[] = {
{(char *) "virDomainSetSchedulerParameters", libvirt_virDomainSetSchedulerParameters, METH_VARARGS, NULL},
{(char *) "virDomainGetVcpus", libvirt_virDomainGetVcpus, METH_VARARGS, NULL},
{(char *) "virDomainPinVcpu", libvirt_virDomainPinVcpu, METH_VARARGS, NULL},
{(char *) "virConnectListStoragePools", libvirt_virConnectListStoragePools, METH_VARARGS, NULL},
{(char *) "virConnectListDefinedStoragePools", libvirt_virConnectListDefinedStoragePools, METH_VARARGS, NULL},
{(char *) "virStoragePoolGetAutostart", libvirt_virStoragePoolGetAutostart, METH_VARARGS, NULL},
{(char *) "virStoragePoolListVolumes", libvirt_virStoragePoolListVolumes, METH_VARARGS, NULL},
{(char *) "virStoragePoolGetInfo", libvirt_virStoragePoolGetInfo, METH_VARARGS, NULL},
{(char *) "virStorageVolGetInfo", libvirt_virStorageVolGetInfo, METH_VARARGS, NULL},
{(char *) "virStoragePoolGetUUID", libvirt_virStoragePoolGetUUID, METH_VARARGS, NULL},
{(char *) "virStoragePoolLookupByUUID", libvirt_virStoragePoolLookupByUUID, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};

View File

@ -48,6 +48,24 @@ typedef struct {
} PyvirNetwork_Object;
#define PyvirStoragePool_Get(v) (((v) == Py_None) ? NULL : \
(((PyvirStoragePool_Object *)(v))->obj))
typedef struct {
PyObject_HEAD
virStoragePoolPtr obj;
} PyvirStoragePool_Object;
#define PyvirStorageVol_Get(v) (((v) == Py_None) ? NULL : \
(((PyvirStorageVol_Object *)(v))->obj))
typedef struct {
PyObject_HEAD
virStorageVolPtr obj;
} PyvirStorageVol_Object;
PyObject * libvirt_intWrap(int val);
PyObject * libvirt_longWrap(long val);
PyObject * libvirt_ulongWrap(unsigned long val);
@ -58,6 +76,8 @@ PyObject * libvirt_charPtrConstWrap(const char *str);
PyObject * libvirt_virConnectPtrWrap(virConnectPtr node);
PyObject * libvirt_virDomainPtrWrap(virDomainPtr node);
PyObject * libvirt_virNetworkPtrWrap(virNetworkPtr node);
PyObject * libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node);
PyObject * libvirt_virStorageVolPtrWrap(virStorageVolPtr node);
/* Provide simple macro statement wrappers (adapted from GLib, in turn from Perl):

View File

@ -144,6 +144,42 @@ libvirt_virNetworkPtrWrap(virNetworkPtr node)
return (ret);
}
PyObject *
libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node)
{
PyObject *ret;
#ifdef DEBUG
printf("libvirt_virStoragePoolPtrWrap: node = %p\n", node);
#endif
if (node == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret =
PyCObject_FromVoidPtrAndDesc((void *) node, (char *) "virStoragePoolPtr",
NULL);
return (ret);
}
PyObject *
libvirt_virStorageVolPtrWrap(virStorageVolPtr node)
{
PyObject *ret;
#ifdef DEBUG
printf("libvirt_virStorageVolPtrWrap: node = %p\n", node);
#endif
if (node == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
ret =
PyCObject_FromVoidPtrAndDesc((void *) node, (char *) "virStorageVolPtr",
NULL);
return (ret);
}
PyObject *
libvirt_virConnectPtrWrap(virConnectPtr node)
{