[Kimchi-devel] [PATCH 2/3] add a new function to get an available network address

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Fri Mar 7 09:53:01 UTC 2014


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

Then the default network check code can make use it.

Also add a new argument for get_one_free_network to make it more flexible

Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
---
 src/kimchi/model/networks.py | 18 +++++++++++-------
 src/kimchi/network.py        |  4 ++--
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/kimchi/model/networks.py b/src/kimchi/model/networks.py
index bd9c03a..7103085 100644
--- a/src/kimchi/model/networks.py
+++ b/src/kimchi/model/networks.py
@@ -103,17 +103,21 @@ class NetworksModel(object):
         conn = self.conn.get()
         return sorted(conn.listNetworks() + conn.listDefinedNetworks())
 
+    def _get_available_address(self, addr_pools=[]):
+        invalid_addrs=[]
+        for net_name in self.get_list():
+            network = NetworkModel.get_network(self.conn.get(), net_name)
+            xml = network.XMLDesc(0)
+            subnet = NetworkModel.get_network_from_xml(xml)['subnet']
+            subnet and invalid_addrs.append(ipaddr.IPNetwork(subnet))
+            addr_pools = addr_pools if addr_pools else knetwork.PrivateNets
+        return knetwork.get_one_free_network(invalid_addrs, addr_pools)
+
     def _set_network_subnet(self, params):
         netaddr = params.get('subnet', '')
-        net_addrs = []
         # lookup a free network address for nat and isolated automatically
         if not netaddr:
-            for net_name in self.get_list():
-                network = NetworkModel.get_network(self.conn.get(), net_name)
-                xml = network.XMLDesc(0)
-                subnet = NetworkModel.get_network_from_xml(xml)['subnet']
-                subnet and net_addrs.append(ipaddr.IPNetwork(subnet))
-            netaddr = knetwork.get_one_free_network(net_addrs)
+            netaddr = self._get_available_address()
             if not netaddr:
                 raise OperationFailed("KCHNET0009E", {'name': params['name']})
 
diff --git a/src/kimchi/network.py b/src/kimchi/network.py
index 24439df..9ad95b4 100644
--- a/src/kimchi/network.py
+++ b/src/kimchi/network.py
@@ -44,7 +44,7 @@ def get_dev_netaddrs():
 
 # used_nets should include all the subnet allocated in libvirt network
 # will get host network by get_dev_netaddrs
-def get_one_free_network(used_nets):
+def get_one_free_network(used_nets, nets_pool=PrivateNets):
     def _get_free_network(nets, used_nets):
         for net in nets.subnet(new_prefix=24):
             if not any(net.overlaps(used) for used in used_nets):
@@ -52,7 +52,7 @@ def get_one_free_network(used_nets):
         return None
 
     used_nets = used_nets + get_dev_netaddrs()
-    for nets in PrivateNets:
+    for nets in nets_pool:
         net = _get_free_network(nets, used_nets)
         if net:
             return net
-- 
1.8.4.2




More information about the Kimchi-devel mailing list