[virt-tools-list] [PATCH virt-manager 1/5] virtinst: parse "bandwith" node in network definition

Giuseppe Scrivano gscrivan at redhat.com
Wed Jun 25 11:05:46 UTC 2014


Signed-off-by: Giuseppe Scrivano <gscrivan at redhat.com>
---
 tests/xmlparse-xml/network-multi-in.xml  |  4 +++
 tests/xmlparse-xml/network-multi-out.xml |  4 +++
 tests/xmlparse.py                        |  9 ++++++
 virtinst/network.py                      | 55 +++++++++++++++++++++++++++++++-
 4 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/tests/xmlparse-xml/network-multi-in.xml b/tests/xmlparse-xml/network-multi-in.xml
index f820cec..d06b607 100644
--- a/tests/xmlparse-xml/network-multi-in.xml
+++ b/tests/xmlparse-xml/network-multi-in.xml
@@ -40,4 +40,8 @@
       <outbound average='128' peak='256' burst='256'/>
     </bandwidth>
   </portgroup>
+  <bandwidth>
+    <inbound average="1000" peak="5000" burst="5120"/>
+    <outbound average="1000" peak="5000" burst="5120"/>
+  </bandwidth>
 </network>
diff --git a/tests/xmlparse-xml/network-multi-out.xml b/tests/xmlparse-xml/network-multi-out.xml
index 3622430..4481195 100644
--- a/tests/xmlparse-xml/network-multi-out.xml
+++ b/tests/xmlparse-xml/network-multi-out.xml
@@ -42,6 +42,10 @@
       <outbound average="128" peak="256" burst="256"/>
     </bandwidth>
   </portgroup>
+  <bandwidth>
+    <inbound average="3000" peak="4000" burst="5220"/>
+    <outbound average="2000" peak="3000" burst="5120"/>
+  </bandwidth>
   <mac address="52:54:00:69:eb:FF"/>
   <route family="ipv4" address="192.168.8.0" prefix="24" gateway="192.168.8.10"/>
 </network>
diff --git a/tests/xmlparse.py b/tests/xmlparse.py
index 0295d60..f16295e 100644
--- a/tests/xmlparse.py
+++ b/tests/xmlparse.py
@@ -1143,6 +1143,15 @@ class XMLParseTest(unittest.TestCase):
         check("mode", "nat", "route")
         check("dev", None, "eth22")
 
+        check = self._make_checker(net.bandwidth)
+        check("inbound_average", "1000", "3000")
+        check("inbound_peak", "5000", "4000")
+        check("inbound_burst", "5120", "5220")
+        check("inbound_floor", None, None)
+        check("outbound_average", "1000", "2000")
+        check("outbound_peak", "5000", "3000")
+        check("outbound_burst", "5120", "5120")
+
         self.assertEquals(len(net.portgroups), 2)
         check = self._make_checker(net.portgroups[0])
         check("name", "engineering", "foo")
diff --git a/virtinst/network.py b/virtinst/network.py
index c3baf63..2284ea4 100644
--- a/virtinst/network.py
+++ b/virtinst/network.py
@@ -82,6 +82,58 @@ class _NetworkForward(XMLBuilder):
         return Network.pretty_forward_desc(self.mode, self.dev)
 
 
+class _NetworkBandwidth(XMLBuilder):
+    _XML_ROOT_NAME = "bandwidth"
+
+    inbound_average = XMLProperty("./inbound/@average")
+    inbound_peak = XMLProperty("./inbound/@peak")
+    inbound_burst = XMLProperty("./inbound/@burst")
+    inbound_floor = XMLProperty("./inbound/@floor")
+
+    outbound_average = XMLProperty("./outbound/@average")
+    outbound_peak = XMLProperty("./outbound/@peak")
+    outbound_burst = XMLProperty("./outbound/@burst")
+
+    def is_inbound(self):
+        return bool(self.inbound_average or self.inbound_peak
+                    or self.inbound_burst or self.inbound_floor)
+
+    def is_outbound(self):
+        return bool(self.outbound_average or self.outbound_peak
+                    or self.outbound_burst)
+
+    def pretty_desc(self, inbound=True, outbound=True):
+        items_in = [(self.inbound_average, _("Average"), "KiB/s"),
+                    (self.inbound_peak, _("Peak"), "KiB"),
+                    (self.inbound_burst, _("Burst"), "KiB/s"),
+                    (self.inbound_floor, _("Floor"), "KiB/s")]
+
+        items_out = [(self.outbound_average, _("Average"), "KiB/s"),
+                     (self.outbound_peak, _("Peak"), "KiB"),
+                     (self.outbound_burst, _("Burst"), "KiB/s")]
+
+        def stringify_items(items):
+            return ", ".join(["%s: %s %s" % (desc, val, unit)
+                              for val, desc, unit in items if val])
+
+        ret = ""
+        show_name = inbound and outbound
+
+        if inbound:
+            if show_name:
+                ret += _("Inbound: ")
+            ret += stringify_items(items_in)
+
+        if outbound:
+            if ret:
+                ret += "\n"
+            if show_name:
+                ret += _("Outbound: ")
+            ret += stringify_items(items_out)
+
+        return ret
+
+
 class _NetworkPortgroup(XMLBuilder):
     _XML_ROOT_NAME = "portgroup"
 
@@ -161,7 +213,7 @@ class Network(XMLBuilder):
     _XML_ROOT_NAME = "network"
     _XML_PROP_ORDER = ["ipv6", "name", "uuid", "forward",
                        "bridge", "stp", "delay", "domain_name",
-                       "macaddr", "ips", "routes"]
+                       "macaddr", "ips", "routes", "bandwidth"]
 
     ipv6 = XMLProperty("./@ipv6", is_yesno=True)
     name = XMLProperty("./name", validate_cb=_validate_name)
@@ -182,6 +234,7 @@ class Network(XMLBuilder):
     portgroups = XMLChildProperty(_NetworkPortgroup)
     ips = XMLChildProperty(_NetworkIP)
     routes = XMLChildProperty(_NetworkRoute)
+    bandwidth = XMLChildProperty(_NetworkBandwidth, is_single=True)
 
     def add_ip(self):
         ip = _NetworkIP(self.conn)
-- 
1.9.3




More information about the virt-tools-list mailing list