[libvirt] [PATCH 10/20] Secret manipulation step 10: Add Python API

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

 



Sample session:

>>> import libvirt
>>> c = libvirt.open('qemu:///session')

>>> c.listSecrets()
['12247729-47d2-a783-88ce-b329d4781cd3', 'reee', 'abc']

>>> s = c.secretDefineXML("<secret ephemeral='no' private='no'>\n<description>Something for use</description>\n<volume>/foo/bar</volume>\n</secret>\n")

>>> s.UUIDString()
'340c2dfb-811b-eda8-da9e-25ccd7bfd650'

>>> s.XMLDesc()
"<secret ephemeral='no' private='no'>\n  <uuid>340c2dfb-811b-eda8-da9e-25ccd7bfd650</uuid>\n  <description>Something for use</description>\n  <volume>/foo/bar</volume>\n</secret>\n"

>>> s.setValue('abc\0xx\xffx')
0

>>> s.value()
'abc\x00xx\xffx'

>>> s.undefine()
0

Changes since the third submission:
- Add "flags" parameter to virSecretDefineXML(), virSecretGetXMLDesc(),
  virSecretGetValue(), virSecretSetValue(), and all derived interfaces.
- remove "get" prefix from method names of virSecret, to be more
  consistent with other classes.
---
 python/generator.py           |   31 +++++++++++--
 python/libvir.c               |  100 +++++++++++++++++++++++++++++++++++++++++
 python/libvirt-python-api.xml |   18 +++++++
 python/libvirt_wrap.h         |    9 ++++
 python/types.c                |   13 +++++
 5 files changed, 167 insertions(+), 4 deletions(-)

diff --git a/python/generator.py b/python/generator.py
index feff7a3..4dbad1c 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -270,6 +270,11 @@ py_types = {
     'const virNodeDevicePtr':  ('O', "virNodeDevice", "virNodeDevicePtr", "virNodeDevicePtr"),
     'virNodeDevice *':  ('O', "virNodeDevice", "virNodeDevicePtr", "virNodeDevicePtr"),
     'const virNodeDevice *':  ('O', "virNodeDevice", "virNodeDevicePtr", "virNodeDevicePtr"),
+
+    'virSecretPtr':  ('O', "virSecret", "virSecretPtr", "virSecretPtr"),
+    'const virSecretPtr':  ('O', "virSecret", "virSecretPtr", "virSecretPtr"),
+    'virSecret *':  ('O', "virSecret", "virSecretPtr", "virSecretPtr"),
+    'const virSecret *':  ('O', "virSecret", "virSecretPtr", "virSecretPtr"),
 }
 
 py_return_types = {
@@ -296,6 +301,7 @@ skip_impl = (
     'virConnectListDefinedNetworks',
     'virConnectListInterfaces',
     'virConnectListDefinedInterfaces',
+    'virConnectListSecrets',
     'virConnectListStoragePools',
     'virConnectListDefinedStoragePools',
     'virConnectListStorageVols',
@@ -320,6 +326,8 @@ skip_impl = (
     'virDomainSetSchedulerParameters',
     'virDomainGetVcpus',
     'virDomainPinVcpu',
+    'virSecretGetValue',
+    'virSecretSetValue',
     'virStoragePoolGetUUID',
     'virStoragePoolGetUUIDString',
     'virStoragePoolLookupByUUID',
@@ -623,6 +631,8 @@ classes_type = {
     "virStorageVol *": ("._o", "virStorageVol(self, _obj=%s)", "virStorageVol"),
     "virNodeDevicePtr": ("._o", "virNodeDevice(self, _obj=%s)", "virNodeDevice"),
     "virNodeDevice *": ("._o", "virNodeDevice(self, _obj=%s)", "virNodeDevice"),
+    "virSecretPtr": ("._o", "virSecret(self, _obj=%s)", "virSecret"),
+    "virSecret *": ("._o", "virSecret(self, _obj=%s)", "virSecret"),
     "virConnectPtr": ("._o", "virConnect(_obj=%s)", "virConnect"),
     "virConnect *": ("._o", "virConnect(_obj=%s)", "virConnect"),
 }
@@ -632,7 +642,7 @@ converter_type = {
 
 primary_classes = ["virDomain", "virNetwork", "virInterface",
                    "virStoragePool", "virStorageVol",
-                   "virConnect", "virNodeDevice" ]
+                   "virConnect", "virNodeDevice", "virSecret" ]
 
 classes_ancestor = {
 }
@@ -642,7 +652,8 @@ classes_destructors = {
     "virInterface": "virInterfaceFree",
     "virStoragePool": "virStoragePoolFree",
     "virStorageVol": "virStorageVolFree",
-    "virNodeDevice" : "virNodeDeviceFree"
+    "virNodeDevice" : "virNodeDeviceFree",
+    "virSecret": "virSecretFree"
 }
 
 functions_noexcept = {
@@ -714,6 +725,12 @@ def nameFixup(name, classe, type, file):
     elif name[0:18] == "virInterfaceLookup":
         func = name[3:]
         func = string.lower(func[0:1]) + func[1:]
+    elif name[0:15] == "virSecretDefine":
+        func = name[3:]
+        func = string.lower(func[0:1]) + func[1:]
+    elif name[0:15] == "virSecretLookup":
+        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:]
@@ -747,6 +764,12 @@ def nameFixup(name, classe, type, file):
     elif name[0:12] == "virInterface":
         func = name[10:]
         func = string.lower(func[0:1]) + func[1:]
+    elif name[0:12] == 'virSecretGet':
+        func = name[12:]
+        func = string.lower(func[0:1]) + func[1:]
+    elif name[0:9] == 'virSecret':
+        func = name[9:]
+        func = string.lower(func[0:1]) + func[1:]
     elif name[0:17] == "virStoragePoolGet":
         func = name[17:]
         func = string.lower(func[0:1]) + func[1:]
@@ -1018,7 +1041,7 @@ def buildWrappers():
 	    else:
 		txt.write("Class %s()\n" % (classname))
 		classes.write("class %s:\n" % (classname))
-                if classname in [ "virDomain", "virNetwork", "virInterface", "virStoragePool", "virStorageVol", "virNodeDevice" ]:
+                if classname in [ "virDomain", "virNetwork", "virInterface", "virStoragePool", "virStorageVol", "virNodeDevice", "virSecret" ]:
                     classes.write("    def __init__(self, conn, _obj=None):\n")
                 else:
                     classes.write("    def __init__(self, _obj=None):\n")
@@ -1026,7 +1049,7 @@ def buildWrappers():
 		    list = reference_keepers[classname]
 		    for ref in list:
 		        classes.write("        self.%s = None\n" % ref[1])
-                if classname in [ "virDomain", "virNetwork", "virInterface", "virNodeDevice" ]:
+                if classname in [ "virDomain", "virNetwork", "virInterface", "virNodeDevice", "virSecret" ]:
                     classes.write("        self._conn = conn\n")
                 elif classname in [ "virStorageVol", "virStoragePool" ]:
                     classes.write("        self._conn = conn\n" + \
diff --git a/python/libvir.c b/python/libvir.c
index e210597..e2c196a 100644
--- a/python/libvir.c
+++ b/python/libvir.c
@@ -1562,6 +1562,103 @@ libvirt_virNodeDeviceListCaps(PyObject *self ATTRIBUTE_UNUSED,
     return(py_retval);
 }
 
+static PyObject *
+libvirt_virConnectListSecrets(PyObject *self ATTRIBUTE_UNUSED,
+                              PyObject *args) {
+    PyObject *py_retval;
+    char **uuids = NULL;
+    virConnectPtr conn;
+    int c_retval, i;
+    PyObject *pyobj_conn;
+
+    if (!PyArg_ParseTuple(args, (char *)"O:virConnectListSecrets", &pyobj_conn))
+        return NULL;
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virConnectNumOfSecrets(conn);
+    LIBVIRT_END_ALLOW_THREADS;
+    if (c_retval < 0)
+        return VIR_PY_NONE;
+
+    if (c_retval) {
+        uuids = malloc(sizeof(*uuids) * c_retval);
+        if (!uuids)
+            return VIR_PY_NONE;
+        LIBVIRT_BEGIN_ALLOW_THREADS;
+        c_retval = virConnectListSecrets(conn, uuids, c_retval);
+        LIBVIRT_END_ALLOW_THREADS;
+        if (c_retval < 0) {
+            free(uuids);
+            return VIR_PY_NONE;
+        }
+    }
+    py_retval = PyList_New(c_retval);
+
+    if (uuids) {
+        for (i = 0;i < c_retval;i++) {
+            PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(uuids[i]));
+            free(uuids[i]);
+        }
+        free(uuids);
+    }
+
+    return py_retval;
+}
+
+static PyObject *
+libvirt_virSecretGetValue(PyObject *self ATTRIBUTE_UNUSED,
+                          PyObject *args) {
+    PyObject *py_retval;
+    unsigned char *c_retval;
+    size_t size;
+    virSecretPtr secret;
+    PyObject *pyobj_secret;
+    unsigned int flags;
+
+    if (!PyArg_ParseTuple(args, (char *)"Oi:virSecretGetValue", &pyobj_secret,
+                          &flags))
+        return NULL;
+    secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virSecretGetValue(secret, &size, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (c_retval == NULL)
+        return VIR_PY_NONE;
+
+    py_retval = PyString_FromStringAndSize((const char *)c_retval, size);
+    memset(c_retval, 0, size);
+    free(c_retval);
+
+    return py_retval;
+}
+
+static PyObject *
+libvirt_virSecretSetValue(PyObject *self ATTRIBUTE_UNUSED,
+                          PyObject *args) {
+    PyObject *py_retval;
+    int c_retval;
+    virSecretPtr secret;
+    PyObject *pyobj_secret;
+    const char *value;
+    int size;
+    unsigned int flags;
+
+    if (!PyArg_ParseTuple(args, (char *)"Oz#i:virSecretSetValue", &pyobj_secret,
+                          &value, &size, &flags))
+        return NULL;
+    secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virSecretSetValue(secret, (const unsigned char *)value, size,
+                                 flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    py_retval = libvirt_intWrap(c_retval);
+    return py_retval;
+}
 
 /*******************************************
  * Helper functions to avoid importing modules
@@ -2261,6 +2358,9 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virEventInvokeTimeoutCallback", libvirt_virEventInvokeTimeoutCallback, METH_VARARGS, NULL},
     {(char *) "virNodeListDevices", libvirt_virNodeListDevices, METH_VARARGS, NULL},
     {(char *) "virNodeDeviceListCaps", libvirt_virNodeDeviceListCaps, METH_VARARGS, NULL},
+    {(char *) "virConnectListSecrets", libvirt_virConnectListSecrets, METH_VARARGS, NULL},
+    {(char *) "virSecretGetValue", libvirt_virSecretGetValue, METH_VARARGS, NULL},
+    {(char *) "virSecretSetValue", libvirt_virSecretSetValue, METH_VARARGS, NULL},
     {NULL, NULL, 0, NULL}
 };
 
diff --git a/python/libvirt-python-api.xml b/python/libvirt-python-api.xml
index 43a5b4e..e5c1fb9 100644
--- a/python/libvirt-python-api.xml
+++ b/python/libvirt-python-api.xml
@@ -172,5 +172,23 @@
       <arg name='dev' type='virNodeDevicePtr' info='pointer to the node device'/>
       <return type='str *' info='the list of Names or None in case of error'/>
     </function>
+    <function name='virSecretGetValue' file='libvirt' module='libvirt'>
+      <info>Fetches the value associated with a secret.</info>
+      <return type='char *' info='the secret value or None in case of error'/>
+      <arg name='secret' type='virSecretPtr' info='virSecret secret'/>
+      <arg name='flags' type='unsigned int' info='flags (unused; pass 0)'/>
+    </function>
+    <function name='virConnectListSecrets' file='libvirt' module='libvirt'>
+      <info>List the defined secret IDs</info>
+      <arg name='conn' type='virConnectPtr' info='virConnect connection'/>
+      <return type='str *' info='the list of secret IDs or None in case of error'/>
+    </function>
+    <function name='virSecretSetValue' file='libvirt' module='libvirt'>
+      <info>Associates a value with a secret.</info>
+      <return type='int' info='0 on success, -1 on failure.'/>
+      <arg name='secret' type='virSecretPtr' info='virSecret secret'/>
+      <arg name='value' type='const char *' info='The secret value'/>
+      <arg name='flags' type='unsigned int' info='flags (unused; pass 0)'/>
+    </function>
   </symbols>
 </api>
diff --git a/python/libvirt_wrap.h b/python/libvirt_wrap.h
index 4a32edd..99d5805 100644
--- a/python/libvirt_wrap.h
+++ b/python/libvirt_wrap.h
@@ -83,6 +83,14 @@ typedef struct {
     virNodeDevicePtr obj;
 } PyvirNodeDevice_Object;
 
+#define PyvirSecret_Get(v) (((v) == Py_None) ? NULL : \
+        (((PyvirSecret_Object *)(v))->obj))
+
+typedef struct {
+    PyObject_HEAD
+    virSecretPtr obj;
+} PyvirSecret_Object;
+
 
 #define PyvirEventHandleCallback_Get(v) (((v) == Py_None) ? NULL : \
         (((PyvirEventHandleCallback_Object *)(v))->obj))
@@ -135,6 +143,7 @@ PyObject * libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node);
 PyObject * libvirt_virFreeCallbackWrap(virFreeCallback node);
 PyObject * libvirt_virVoidPtrWrap(void* node);
 PyObject * libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node);
+PyObject * libvirt_virSecretPtrWrap(virSecretPtr node);
 
 
 /* Provide simple macro statement wrappers (adapted from GLib, in turn from Perl):
diff --git a/python/types.c b/python/types.c
index de75471..c445f5f 100644
--- a/python/types.c
+++ b/python/types.c
@@ -194,6 +194,19 @@ libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node)
 }
 
 PyObject *
+libvirt_virSecretPtrWrap(virSecretPtr node)
+{
+    PyObject *ret;
+
+    if (node == NULL) {
+        Py_INCREF(Py_None);
+        return Py_None;
+    }
+    ret = PyCObject_FromVoidPtrAndDesc(node, (char *) "virSecretPtr", NULL);
+    return (ret);
+}
+
+PyObject *
 libvirt_virEventHandleCallbackWrap(virEventHandleCallback node)
 {
     PyObject *ret;
-- 
1.6.2.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]