[Kimchi-devel] [PATCH V4 1/3] network improvment: add vms field

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Mon Jan 6 08:49:48 UTC 2014


From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>

Add vms field for network GET method
update API.md

add a function to get all vms attach to a network
update model

add vms field for Network Resource property
update controller

Now get the info of default network:
$ curl -u <user>:<password> -H 'Content-type: application/json' -H 'Accept:
application/json' -X GET http://localhost:8000/networks/default
{
  "subnet":"192.168.122.0/24",
  "connection":"nat",
  "name":"default",
  "autostart":true,
  "state":"active",
  "interface":"virbr0",
  "dhcp":{
    "start":"192.168.122.2",
    "end":"192.168.122.254"
  },
  "vms":[
    "rhel6",
    "opensuse12"
  ]
}
we can see, there are two vms attched to this network

Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
---
 docs/API.md                    |  1 +
 src/kimchi/control/networks.py |  1 +
 src/kimchi/model.py            | 15 +++++++++++++++
 3 files changed, 17 insertions(+)

diff --git a/docs/API.md b/docs/API.md
index 0013e86..32045ac 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -364,6 +364,7 @@ A interface represents available interface on host.
         * active: The Network is ready for use
         * inactive: The Network is not available
     * autostart: Network autostart onboot
+    * vms: all vms attached to this network
     * subnet: Network segment in slash-separated format with ip address and prefix
     * dhcp: DHCP services on the virtual network is enabled.
         * start: start boundary of a pool of addresses to be provided to DHCP clients.
diff --git a/src/kimchi/control/networks.py b/src/kimchi/control/networks.py
index ad95d69..f3f0b41 100644
--- a/src/kimchi/control/networks.py
+++ b/src/kimchi/control/networks.py
@@ -40,6 +40,7 @@ class Network(Resource):
     @property
     def data(self):
         return {'name': self.ident,
+                'vms': self.info['vms'],
                 'autostart': self.info['autostart'],
                 'connection': self.info['connection'],
                 'interface': self.info['interface'],
diff --git a/src/kimchi/model.py b/src/kimchi/model.py
index a21fcf7..3e61390 100644
--- a/src/kimchi/model.py
+++ b/src/kimchi/model.py
@@ -547,6 +547,11 @@ class Model(object):
         xpath = "/domain/devices/disk[@device='disk']/source/@file"
         return xmlutils.xpath_get_text(xml, xpath)
 
+    def _vm_get_networks(self, dom):
+        xml = dom.XMLDesc(0)
+        xpath = "/domain/devices/interface[@type='network']/source/@network"
+        return xmlutils.xpath_get_text(xml, xpath)
+
     def vm_delete(self, name):
         if self._vm_exists(name):
             conn = self.conn.get()
@@ -859,6 +864,15 @@ class Model(object):
         conn = self.conn.get()
         return sorted(conn.listNetworks() + conn.listDefinedNetworks())
 
+    def _get_vms_attach_to_a_network(self, network):
+        vms = []
+        conn = self.conn.get()
+        for dom in conn.listAllDomains(0):
+            networks = self._vm_get_networks(dom)
+            if network in networks:
+                vms.append(dom.name())
+        return vms
+
     def network_lookup(self, name):
         network = self._get_network(name)
         xml = network.XMLDesc(0)
@@ -887,6 +901,7 @@ class Model(object):
                 'interface': interface,
                 'subnet': subnet,
                 'dhcp': dhcp,
+                'vms': self._get_vms_attach_to_a_network(name),
                 'autostart': network.autostart() == 1,
                 'state':  network.isActive() and "active" or "inactive"}
 
-- 
1.8.4.2




More information about the Kimchi-devel mailing list