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

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



Sample session:

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

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

>>> c.virSecretAllocateID()
'17427ed8-4040-b6c8-10ba-9cce86dc3d8f'

>>> c.virSecretDelete('17427ed8-4040-b6c8-10ba-9cce86dc3d8f')
0

>>> c.virSecretSetXML('abc', "<secret ephemeral='no' private='yes'>\n<description>Something for use</description>\n<volume>/foo/bar</volume>\n</secret>\n")
0

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

>>> c.virSecretSetValue('reee', 'abc\0xx\xffx')
0

>>> c.virSecretGetValue('reee')
'abc\x00xx\xffx'
---
 python/generator.py           |    6 +++
 python/libvir.c               |   98 +++++++++++++++++++++++++++++++++++++++++
 python/libvirt-python-api.xml |   18 ++++++++
 3 files changed, 122 insertions(+), 0 deletions(-)

diff --git a/python/generator.py b/python/generator.py
index feff7a3..49b7f68 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -332,6 +332,9 @@ skip_impl = (
     'virEventRegisterImpl',
     'virNodeListDevices',
     'virNodeDeviceListCaps',
+    'virSecretGetValue',
+    'virSecretListSecrets',
+    'virSecretSetValue',
 )
 
 
@@ -729,6 +732,9 @@ def nameFixup(name, classe, type, file):
     elif name[0:19] == "virStorageVolLookup":
         func = name[3:]
         func = string.lower(func[0:1]) + func[1:]
+    elif name[0:9] == 'virSecret':
+        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:]
diff --git a/python/libvir.c b/python/libvir.c
index e210597..b91a801 100644
--- a/python/libvir.c
+++ b/python/libvir.c
@@ -1562,6 +1562,101 @@ libvirt_virNodeDeviceListCaps(PyObject *self ATTRIBUTE_UNUSED,
     return(py_retval);
 }
 
+static PyObject *
+libvirt_virSecretGetValue(PyObject *self ATTRIBUTE_UNUSED,
+                          PyObject *args) {
+    PyObject *py_retval;
+    void *c_retval;
+    size_t size;
+    virConnectPtr conn;
+    const char *secret_id;
+    PyObject *pyobj_conn;
+
+    if (!PyArg_ParseTuple(args, (char *)"Oz:virSecreteGetValue", &pyobj_conn,
+                          &secret_id))
+        return NULL;
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virSecretGetValue(conn, secret_id, &size);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (c_retval == NULL)
+        return VIR_PY_NONE;
+
+    py_retval = PyString_FromStringAndSize(c_retval, size);
+    memset(c_retval, 0, size);
+    free(c_retval);
+
+    return py_retval;
+}
+
+static PyObject *
+libvirt_virSecretListSecrets(PyObject *self ATTRIBUTE_UNUSED,
+                             PyObject *args) {
+    PyObject *py_retval;
+    char **ids = NULL;
+    virConnectPtr conn;
+    int c_retval, i;
+    PyObject *pyobj_conn;
+
+    if (!PyArg_ParseTuple(args, (char *)"O:virSecretListSecrets", &pyobj_conn))
+        return NULL;
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virSecretNumOfSecrets(conn);
+    LIBVIRT_END_ALLOW_THREADS;
+    if (c_retval < 0)
+        return VIR_PY_NONE;
+
+    if (c_retval) {
+        ids = malloc(sizeof(*ids) * c_retval);
+        if (!ids)
+            return VIR_PY_NONE;
+        LIBVIRT_BEGIN_ALLOW_THREADS;
+        c_retval = virSecretListSecrets(conn, ids, c_retval);
+        LIBVIRT_END_ALLOW_THREADS;
+        if (c_retval < 0) {
+            free(ids);
+            return VIR_PY_NONE;
+        }
+    }
+    py_retval = PyList_New(c_retval);
+
+    if (ids) {
+        for (i = 0;i < c_retval;i++) {
+            PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(ids[i]));
+            free(ids[i]);
+        }
+        free(ids);
+    }
+
+    return py_retval;
+}
+
+static PyObject *
+libvirt_virSecretSetValue(PyObject *self ATTRIBUTE_UNUSED,
+                          PyObject *args) {
+    PyObject *py_retval;
+    int c_retval;
+    virConnectPtr conn;
+    PyObject *pyobj_conn;
+    const char *secret_id, *secret;
+    int size;
+
+    if (!PyArg_ParseTuple(args, (char *)"Ozz#:virSecretSetValue", &pyobj_conn,
+                          &secret_id, &secret, &size))
+        return(NULL);
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virSecretSetValue(conn, secret_id, secret, size);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    py_retval = libvirt_intWrap(c_retval);
+    return(py_retval);
+}
 
 /*******************************************
  * Helper functions to avoid importing modules
@@ -2261,6 +2356,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 *) "virSecretGetValue", libvirt_virSecretGetValue, METH_VARARGS, NULL},
+    {(char *) "virSecretListSecrets", libvirt_virSecretListSecrets, 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..c1d6546 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 secret value associated with secret_id.</info>
+      <return type='char *' info='the secret value or None in case of error'/>
+      <arg name='conn' type='virConnectPtr' info='virConnect connection'/>
+      <arg name='secret_id' type='const char *' info='A secret ID'/>
+    </function>
+    <function name='virSecretListSecrets' 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 secret value with secret_id.  Allocates secret_id if it was not previously allocated.</info>
+      <return type='int' info='0 on success, -1 on failure.'/>
+      <arg name='conn' type='virConnectPtr' info='virConnect connection'/>
+      <arg name='secret_id' type='const char *' info='A secret ID'/>
+      <arg name='secret' type='const char *' info='The secret'/>
+    </function>
   </symbols>
 </api>
-- 
1.6.2.5


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