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

Aline Manera alinefm at linux.vnet.ibm.com
Thu Jan 2 17:04:41 UTC 2014


On 12/30/2013 10:48 AM, shaohef at linux.vnet.ibm.com wrote:
> 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():

 >>> conn.listAllDomains()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: listAllDomains() takes exactly 2 arguments (1 given)

Did you test it in all supported distros?
I am not sure all libvirt versions has support for listAllDomains function

> +            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"}
>




More information about the Kimchi-devel mailing list