[Kimchi-devel] [PATCH] network improvment: add vms field

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Mon Dec 30 12:48:25 UTC 2013


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/controller.py |  1 +
 src/kimchi/model.py      | 15 +++++++++++++++
 3 files changed, 17 insertions(+)

diff --git a/docs/API.md b/docs/API.md
index 9edc551..a3e6c8a 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -348,6 +348,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/controller.py b/src/kimchi/controller.py
index 2940278..63b0820 100644
--- a/src/kimchi/controller.py
+++ b/src/kimchi/controller.py
@@ -459,6 +459,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 a6790b8..015eb46 100644
--- a/src/kimchi/model.py
+++ b/src/kimchi/model.py
@@ -546,6 +546,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()
@@ -844,6 +849,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():
+            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)
@@ -872,6 +886,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