Esempio n. 1
0
    def create(self, params):
        conn = self.conn.get()
        name = params['name']
        if name in self.get_list():
            raise InvalidOperation("KCHNET0001E", {'name': name})

        connection = params["connection"]
        # set forward mode, isolated do not need forward
        if connection != 'isolated':
            params['forward'] = {'mode': connection}

        # set subnet, bridge network do not need subnet
        if connection in ["nat", 'isolated']:
            self._set_network_subnet(params)

        # only bridge network need bridge(linux bridge) or interface(macvtap)
        if connection == 'bridge':
            self._set_network_bridge(params)

        params['name'] = escape(params['name'])
        xml = to_network_xml(**params)

        try:
            network = conn.networkDefineXML(xml.encode("utf-8"))
            network.setAutostart(True)
        except libvirt.libvirtError as e:
            raise OperationFailed("KCHNET0008E",
                                  {'name': name, 'err': e.get_error_message()})

        return name
Esempio n. 2
0
    def create(self, params):
        conn = self.conn.get()
        name = params['name']
        if name in self.get_list():
            raise InvalidOperation("KCHNET0001E", {'name': name})

        connection = params["connection"]
        # set forward mode, isolated do not need forward
        if connection != 'isolated':
            params['forward'] = {'mode': connection}

        # set subnet, bridge network do not need subnet
        if connection in ["nat", 'isolated']:
            self._set_network_subnet(params)

        # only bridge network need bridge(linux bridge) or interface(macvtap)
        if connection == 'bridge':
            self._set_network_bridge(params)

        params['name'] = escape(params['name'])
        xml = to_network_xml(**params)

        try:
            network = conn.networkDefineXML(xml.encode("utf-8"))
            network.setAutostart(True)
        except libvirt.libvirtError as e:
            raise OperationFailed("KCHNET0008E", {
                'name': name,
                'err': e.get_error_message()
            })

        return name
Esempio n. 3
0
    def test_network_xml(self):
        """
        Test network xml
        """
        params = {"name": "test",
                  "forward": {"mode": "nat", "dev": ""},
                  "net": "192.168.0.0/255.255.255.0"}
        xml = nxml.to_network_xml(**params)
        name = xpath_get_text(xml, "/network/name")[0]
        self.assertEquals(name, params['name'])

        forward_mode = xpath_get_text(xml, "/network/forward/@mode")[0]
        self.assertEquals(forward_mode, params['forward']['mode'])
        forward_dev = xpath_get_text(xml, "/network/forward/@dev")[0]
        self.assertEquals(forward_dev, '')

        address = xpath_get_text(xml, "/network/ip/@address")[0]
        self.assertEquals(address, params["net"].split("/")[0])
        netmask = xpath_get_text(xml, "/network/ip/@netmask")[0]
        self.assertEquals(netmask, params["net"].split("/")[1])

        dhcp_start = xpath_get_text(xml, "/network/ip/dhcp/range/@start")
        self.assertEquals(dhcp_start, [])
        dhcp_end = xpath_get_text(xml, "/network/ip/dhcp/range/@end")
        self.assertEquals(dhcp_end, [])

        # test optional params
        params['forward']['dev'] = "eth0"
        params['dhcp'] = {"range": {'start': '192.168.0.1',
                                    'end': '192.168.0.254'}}
        xml = nxml.to_network_xml(**params)
        forward_dev = xpath_get_text(xml, "/network/forward/@dev")[0]
        self.assertEquals(forward_dev, params['forward']['dev'])

        dhcp_start = xpath_get_text(xml, "/network/ip/dhcp/range/@start")[0]
        self.assertEquals(dhcp_start, params['dhcp']['range']['start'])
        dhcp_end = xpath_get_text(xml, "/network/ip/dhcp/range/@end")[0]
        self.assertEquals(dhcp_end, params['dhcp']['range']['end'])

        # test _get_ip_xml can accepts strings: '192.168.122.0/24',
        # which is same as "192.168.122.0/255.255.255.0"
        params["net"] = "192.168.0.0/24"
        xml = nxml.to_network_xml(**params)
        netmask = xpath_get_text(xml, "/network/ip/@netmask")[0]
        self.assertEquals(netmask,
                          str(ipaddr.IPNetwork(params["net"]).netmask))