Example #1
0
def allocate_ip_from_pools(pool_rows, userid, address=None, floating_ip=False):
    """Try to allocate a value from a number of pools.

    This function takes as argument a number of PoolTable objects and tries to
    allocate a value from them. If all pools are empty EmptyPool is raised.
    If an address is specified and does not belong to any of the pools,
    InvalidValue is raised.

    """
    for pool_row in pool_rows:
        pool = pool_row.pool
        try:
            value = pool.get(value=address)
            pool.save()
            subnet = pool_row.subnet
            ipaddress = IPAddress.objects.create(subnet=subnet,
                                                 network=subnet.network,
                                                 userid=userid,
                                                 address=value,
                                                 floating_ip=floating_ip,
                                                 ipversion=4)
            return ipaddress
        except pools.EmptyPool:
            pass
        except pools.InvalidValue:
            pass
    if address is None:
        raise pools.EmptyPool("No more IP addresses available on pools %s" %
                              pool_rows)
    else:
        raise pools.InvalidValue("Address %s does not belong to pools %s" %
                                 (address, pool_rows))
Example #2
0
 def release_address(self, address, external=False):
     for ip_pool in self.get_ip_pools():
         if ip_pool.contains(address):
             ip_pool.put(address, external=external)
             ip_pool.save()
             return
     raise pools.InvalidValue("Network %s does not have an IP pool that"
                              " contains address %s" % (self, address))