[PATCH v3 6/7] python: Expose virDomainInterfacesAddresses

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

 



---
 python/libvirt-override-api.xml |    6 ++
 python/libvirt-override.c       |  115 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 121 insertions(+), 0 deletions(-)

diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index 67ef36e..eb08151 100644
--- a/python/libvirt-override-api.xml
+++ b/python/libvirt-override-api.xml
@@ -487,5 +487,11 @@
       <arg name='domain' type='virDomainPtr' info='a domain object'/>
       <arg name='flags' type='unsigned int' info='unused, always pass 0'/>
     </function>
+    <function name='virDomainInterfacesAddresses' file='python'>
+      <info>returns array of IP addresses for all domain interfaces</info>
+      <arg name='dom' type='virDomainPtr' info='pointer to the domain'/>
+      <arg name='flags' type='unsigned int' info='extra flags; not used yet, so callers should always pass 0'/>
+      <return type='virDomainInterfacePtr' info="dictionary of domain interfaces among with their HW and IP addresses"/>
+    </function>
   </symbols>
 </api>
diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index 8b41dff..7081626 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -3982,6 +3982,120 @@ cleanup:
     return py_retval;
 }
 
+static PyObject *
+libvirt_virDomainInterfacesAddresses(PyObject *self ATTRIBUTE_UNUSED,
+                                     PyObject *args)
+{
+    PyObject *py_retval = VIR_PY_NONE;
+    virDomainPtr domain;
+    PyObject *pyobj_domain;
+    unsigned int flags;
+    virDomainInterfacePtr ifaces = NULL;
+    int ifaces_count = 0;
+    int i, j;
+    bool full_free = true;
+
+    if (!PyArg_ParseTuple(args, (char *) "Oi:virDomainInterfacePtr",
+                          &pyobj_domain, &flags))
+        return NULL;
+
+    domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    ifaces_count = virDomainInterfacesAddresses(domain, &ifaces, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+    if (ifaces_count < 0)
+        goto cleanup;
+
+    if (!(py_retval = PyDict_New()))
+        goto no_memory;
+
+    for (i = 0; i < ifaces_count; i++) {
+        virDomainInterfacePtr iface = &(ifaces[i]);
+        PyObject *py_ip_addrs = NULL;
+        PyObject *py_iface = NULL;
+
+        if (!(py_iface = PyDict_New()))
+            goto no_memory;
+
+        if (iface->ip_addrs_count) {
+            if (!(py_ip_addrs = PyList_New(iface->ip_addrs_count))) {
+                { Py_DECREF(py_iface); }
+                goto no_memory;
+            }
+        } else {
+            py_ip_addrs = VIR_PY_NONE;
+        }
+
+        for (j = 0; j < iface->ip_addrs_count; j++) {
+            virDomainIPAddressPtr ip_addr = &(iface->ip_addrs[j]);
+            PyObject *py_addr = PyDict_New();
+            const char *type;
+
+            if (!py_addr) {
+                { Py_DECREF(py_iface); }
+                { Py_DECREF(py_ip_addrs); }
+                goto no_memory;
+            }
+
+            switch (ip_addr->type) {
+            case VIR_IP_ADDR_TYPE_IPV4:
+                type = "ipv4";
+                break;
+            case VIR_IP_ADDR_TYPE_IPV6:
+                type = "ipv6";
+                break;
+            default:
+                type = "unknown";
+                break;
+            }
+
+            PyDict_SetItem(py_addr, libvirt_constcharPtrWrap("addr"),
+                           PyString_FromString(ip_addr->addr));
+            PyDict_SetItem(py_addr, libvirt_constcharPtrWrap("prefix"),
+                           libvirt_intWrap(ip_addr->prefix));
+            PyDict_SetItem(py_addr, libvirt_constcharPtrWrap("type"),
+                           PyString_FromString(type));
+
+            PyList_SetItem(py_ip_addrs, j, py_addr);
+        }
+
+        PyDict_SetItem(py_iface, libvirt_constcharPtrWrap("ip_addrs"),
+                       py_ip_addrs);
+        PyDict_SetItem(py_iface, libvirt_constcharPtrWrap("hwaddr"),
+                       libvirt_charPtrWrap(iface->hwaddr));
+
+        PyDict_SetItem(py_retval, libvirt_charPtrWrap(iface->name),
+                       py_iface);
+    }
+
+    full_free = false;
+
+cleanup:
+    for (i = 0; i < ifaces_count; i++) {
+        /* We don't want to free values we've just
+         * shared with python variables unless
+         * there was an error and hence we are
+         * retyrning PY_NONE or equivalent */
+        if (full_free) {
+            VIR_FREE(ifaces[i].name);
+            VIR_FREE(ifaces[i].hwaddr);
+            for (j = 0; j < ifaces[i].ip_addrs_count; j++)
+                VIR_FREE(ifaces[i].ip_addrs[j].addr);
+        }
+        VIR_FREE(ifaces[i].ip_addrs);
+    }
+    VIR_FREE(ifaces);
+
+    return py_retval;
+
+no_memory:
+    Py_XDECREF(py_retval);
+    py_retval = PyErr_NoMemory();
+    goto cleanup;
+}
+
+
 /*******************************************
  * Helper functions to avoid importing modules
  * for every callback
@@ -5915,6 +6029,7 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virDomainBlockPeek", libvirt_virDomainBlockPeek, METH_VARARGS, NULL},
     {(char *) "virDomainMemoryPeek", libvirt_virDomainMemoryPeek, METH_VARARGS, NULL},
     {(char *) "virDomainGetDiskErrors", libvirt_virDomainGetDiskErrors, METH_VARARGS, NULL},
+    {(char *) "virDomainInterfacesAddresses", libvirt_virDomainInterfacesAddresses, METH_VARARGS, NULL},
     {NULL, NULL, 0, NULL}
 };
 
-- 
1.7.8.6

--
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]