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

[libvirt] [PATCH 7/8] python: Expose virDomainInterfacesAddresses



---
 python/libvirt-override-api.xml |   7 +++
 python/libvirt-override.c       | 120 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+)

diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index a0e0496..bea1a59 100644
--- a/python/libvirt-override-api.xml
+++ b/python/libvirt-override-api.xml
@@ -548,5 +548,12 @@
       <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/>
       <arg name='flags' type='int' info='unused, pass 0'/>
     </function>
+    <function name='virDomainInterfacesAddresses' file='python'>
+      <info>Get domain's interfaces among with their IP and HW addresses</info>
+      <return type='virDomainInterfacePtr' info='array of @domain interfaces'/>
+      <arg name='domain' type='virDomainPtr' info='domain object'/>
+      <arg name='method' type='unsigned int' info='which method use, one of virDomainInterfacesAddressesMethod'/>
+      <arg name='flags' type='unsigned int' info='extra flags, not used yet, so callers should always pass 0'/>
+    </function>
   </symbols>
 </api>
diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index 91e82c6..ac21c66 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -6588,6 +6588,125 @@ error:
     goto cleanup;
 }
 
+static PyObject *
+libvirt_virDomainInterfacesAddresses(PyObject *self ATTRIBUTE_UNUSED,
+                                     PyObject *args)
+{
+    PyObject *py_retval;
+    virDomainPtr domain;
+    PyObject *pyobj_domain;
+    unsigned int method, flags;
+    virDomainInterfacePtr ifaces = NULL;
+    int j, i, i_retval = 0;
+    bool full_free = true;
+
+    if (!PyArg_ParseTuple(args, (char *) "Oii:virDomainInterfacesAddresses",
+                          &pyobj_domain, &method, &flags))
+        return NULL;
+
+    domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    i_retval = virDomainInterfacesAddresses(domain, &ifaces, method, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (i_retval < 0) {
+        py_retval = VIR_PY_NONE;
+        goto cleanup;
+    }
+
+    if (!(py_retval = PyDict_New()))
+        goto no_memory;
+
+    for (i = 0; i < i_retval; i++) {
+        virDomainInterface iface = ifaces[i];
+        PyObject *pyIPAddrs = NULL;
+        PyObject *pyIface = NULL;
+
+        if (!(pyIface = PyDict_New()))
+            goto no_memory;
+
+        if (iface.ip_addrs_count) {
+            if (!(pyIPAddrs = PyList_New(iface.ip_addrs_count))) {
+                Py_DECREF(pyIface);
+                goto no_memory;
+            }
+        } else {
+            pyIPAddrs = VIR_PY_NONE;
+        }
+
+        for (j = 0; j < iface.ip_addrs_count; j++) {
+            virDomainIPAddress addr = iface.ip_addrs[j];
+            PyObject *pyAddr = PyDict_New();
+            const char *type = NULL;
+
+            if (!pyAddr) {
+                { Py_DECREF(pyIface); }
+                { Py_DECREF(pyIPAddrs); }
+                goto no_memory;
+            }
+
+            switch (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(pyAddr, libvirt_constcharPtrWrap("addr"),
+                           PyString_FromString(addr.addr));
+            PyDict_SetItem(pyAddr, libvirt_constcharPtrWrap("prefix"),
+                           libvirt_intWrap(addr.prefix));
+            PyDict_SetItem(pyAddr, libvirt_constcharPtrWrap("type"),
+                           PyString_FromString(type));
+            PyDict_SetItem(pyAddr, libvirt_constcharPtrWrap("dstaddr"),
+                           libvirt_charPtrWrap(addr.dstaddr));
+            PyList_SetItem(pyIPAddrs, j, pyAddr);
+        }
+
+        PyDict_SetItem(pyIface, libvirt_constcharPtrWrap("ip_addrs"),
+                       pyIPAddrs);
+        PyDict_SetItem(pyIface, libvirt_constcharPtrWrap("hwaddr"),
+                       libvirt_charPtrWrap(iface.hwaddr));
+        PyDict_SetItem(pyIface, libvirt_constcharPtrWrap("flags"),
+                       libvirt_intWrap(iface.flags));
+
+        PyDict_SetItem(py_retval, libvirt_constcharPtrWrap(iface.name),
+                       pyIface);
+    }
+
+    full_free = false;
+
+
+cleanup:
+    for (i = 0; i < i_retval; i++) {
+        /* We don't want to free values we've just
+         * shared with python variables unless
+         * there was an error and hence we are
+         * returning 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[j].dstaddr);
+            }
+        }
+        VIR_FREE(ifaces[i].ip_addrs);
+    }
+    VIR_FREE(ifaces);
+
+    return py_retval;
+no_memory:
+    Py_XDECREF(py_retval);
+    py_retval = PyErr_NoMemory();
+    goto cleanup;
+}
 
 /************************************************************************
  *									*
@@ -6708,6 +6827,7 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virNodeGetMemoryParameters", libvirt_virNodeGetMemoryParameters, METH_VARARGS, NULL},
     {(char *) "virNodeSetMemoryParameters", libvirt_virNodeSetMemoryParameters, METH_VARARGS, NULL},
     {(char *) "virNodeGetCPUMap", libvirt_virNodeGetCPUMap, METH_VARARGS, NULL},
+    {(char *) "virDomainInterfacesAddresses", libvirt_virDomainInterfacesAddresses, METH_VARARGS, NULL},
     {NULL, NULL, 0, NULL}
 };
 
-- 
1.8.0.2


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