[libvirt] [dbus PATCH 3/3] Implement Update method for Network Interface

Katerina Koukiou kkoukiou at redhat.com
Wed Apr 18 13:19:10 UTC 2018


Signed-off-by: Katerina Koukiou <kkoukiou at redhat.com>
---
 data/org.libvirt.Network.xml |  9 ++++++
 src/network.c                | 74 ++++++++++++++++++++++++++++++++++++++++++++
 tests/test_network.py        | 17 ++++++++++
 3 files changed, 100 insertions(+)

diff --git a/data/org.libvirt.Network.xml b/data/org.libvirt.Network.xml
index 385eb82..5ed42a0 100644
--- a/data/org.libvirt.Network.xml
+++ b/data/org.libvirt.Network.xml
@@ -54,5 +54,14 @@
       <annotation name="org.gtk.GDBus.DocString"
         value="See https://libvirt.org/html/libvirt-libvirt-network.html#virNetworkUndefine"/>
     </method>
+    <method name="Update">
+      <annotation name="org.gtk.GDBus.DocString"
+        value="See https://libvirt.org/html/libvirt-libvirt-network.html#virNetworkUpdate"/>
+      <arg name="command" type="s" direction="in"/>
+      <arg name="section" type="s" direction="in"/>
+      <arg name="parentIndex" type="i" direction="in"/>
+      <arg name="xml" type="s" direction="in"/>
+      <arg name="flags" type="u" direction="in"/>
+    </method>
   </interface>
 </node>
diff --git a/src/network.c b/src/network.c
index 492f0bd..a09998b 100644
--- a/src/network.c
+++ b/src/network.c
@@ -3,6 +3,32 @@
 
 #include <libvirt/libvirt.h>
 
+VIRT_DBUS_ENUM_DECL(virtDBusNetworkUpdateCommand)
+VIRT_DBUS_ENUM_IMPL(virtDBusNetworkUpdateCommand,
+                    VIR_NETWORK_UPDATE_COMMAND_LAST,
+                    "none",
+                    "modify",
+                    "delete",
+                    "add-last",
+                    "add-first")
+
+VIRT_DBUS_ENUM_DECL(virtDBusNetworkUpdateSection)
+VIRT_DBUS_ENUM_IMPL(virtDBusNetworkUpdateSection,
+                    VIR_NETWORK_SECTION_LAST,
+                    "none",
+                    "bridge",
+                    "domain",
+                    "ip",
+                    "ip-dhcp-host",
+                    "ip-dhcp-range",
+                    "forward",
+                    "forward-interface",
+                    "forward-pf",
+                    "portgroup",
+                    "dns-host",
+                    "dns-txt",
+                    "dns-srv")
+
 static virNetworkPtr
 virtDBusNetworkGetVirNetwork(virtDBusConnect *connect,
                              const gchar *objectPath,
@@ -306,6 +332,53 @@ virtDBusNetworkUndefine(GVariant *inArgs G_GNUC_UNUSED,
         virtDBusUtilSetLastVirtError(error);
 }
 
+static void
+virtDBusNetworkUpdate(GVariant *inArgs,
+                      GUnixFDList *inFDs G_GNUC_UNUSED,
+                      const gchar *objectPath,
+                      gpointer userData,
+                      GVariant **outArgs G_GNUC_UNUSED,
+                      GUnixFDList **outFDs G_GNUC_UNUSED,
+                      GError **error)
+{
+    virtDBusConnect *connect = userData;
+    g_autoptr(virNetwork) network = NULL;
+    const gchar *commandStr;
+    gint command;
+    const gchar *sectionStr;
+    gint section;
+    gint parentIndex;
+    const gchar *xml;
+    guint flags;
+
+    g_variant_get(inArgs, "(&s&si&su)",
+                  &commandStr, &sectionStr,
+                  &parentIndex, &xml, &flags);
+
+    command = virtDBusNetworkUpdateCommandTypeFromString(commandStr);
+    if (command < 0) {
+        g_set_error(error, VIRT_DBUS_ERROR, VIRT_DBUS_ERROR_LIBVIRT,
+                    "Can't get valid virNetworkUpdateCommand from string '%s'.",
+                    commandStr);
+        return;
+    }
+    section = virtDBusNetworkUpdateSectionTypeFromString(sectionStr);
+    if (section < 0) {
+        g_set_error(error, VIRT_DBUS_ERROR, VIRT_DBUS_ERROR_LIBVIRT,
+                    "Can't get valid virNetworkUpdateSection from string '%s'.",
+                    sectionStr);
+        return;
+    }
+
+    network = virtDBusNetworkGetVirNetwork(connect, objectPath, error);
+    if (!network)
+        return;
+
+    if (virNetworkUpdate(network, command, section,
+                         parentIndex, xml, flags) < 0)
+        virtDBusUtilSetLastVirtError(error);
+}
+
 static virtDBusGDBusPropertyTable virtDBusNetworkPropertyTable[] = {
     { "Active", virtDBusNetworkGetActive, NULL },
     { "Autostart", virtDBusNetworkGetAutostart, virtDBusNetworkSetAutostart },
@@ -322,6 +395,7 @@ static virtDBusGDBusMethodTable virtDBusNetworkMethodTable[] = {
     { "GetDHCPLeases", virtDBusNetworkGetDHCPLeases },
     { "GetXMLDesc", virtDBusNetworkGetXMLDesc },
     { "Undefine", virtDBusNetworkUndefine },
+    { "Update", virtDBusNetworkUpdate },
     { 0 }
 };
 
diff --git a/tests/test_network.py b/tests/test_network.py
index 0dda923..2c1bd21 100755
--- a/tests/test_network.py
+++ b/tests/test_network.py
@@ -2,11 +2,17 @@
 
 import dbus
 import libvirttest
+import pytest
 
 
 class TestNetwork(libvirttest.BaseTestClass):
     """ Tests for methods and properties of the Network interface
     """
+
+    ip_dhcp_host_xml = '''
+    <host mac='00:16:3e:77:e2:ed' name='foo.example.com' ip='192.168.122.10'/>
+    '''
+
     def test_network_properties_type(self):
         """ Ensure correct return type for Network properties
         """
@@ -73,5 +79,16 @@ class TestNetwork(libvirttest.BaseTestClass):
 
         self.main_loop()
 
+    @pytest.mark.parametrize("command, section, parentIndex, xml_str, flags", [
+        ('add-first', 'ip-dhcp-host', 0, ip_dhcp_host_xml, 0),
+    ])
+    def test_network_update(self, command, section, parentIndex, xml_str, flags):
+        _, test_network = self.test_network()
+        interface_obj = dbus.Interface(test_network, 'org.libvirt.Network')
+        interface_obj.Update(command, section, parentIndex, xml_str, flags)
+        updated_netxml = interface_obj.GetXMLDesc(0)
+        assert (xml_str.strip() in updated_netxml)
+
+
 if __name__ == '__main__':
     libvirttest.run()
-- 
2.15.0




More information about the libvir-list mailing list