def _allocate_IP():
    """
    Determine next available IP for pool in input_ and assign it
    """
    pool = input_json['ipam']['subnet']
    candidate = SequentialAssignment().allocate(IPNetwork(pool))
    print_stderr("Using IP %s" % candidate)
    return candidate
def assign_ip(version):
    """
    Assign a IP address from the configured pools.
    :param version: 4 for IPv4, 6 for IPv6.
    :return: An IPAddress, or None if an IP couldn't be
             assigned
    """
    ip = None

    assert version in [4, 6]
    # For each configured pool, attempt to assign an IP before giving up.
    for pool in client.get_ip_pools(version):
        assigner = SequentialAssignment()
        ip = assigner.allocate(pool)
        if ip is not None:
            ip = IPNetwork(ip)
            break
    return ip
Example #3
0
def _assign_to_pool(subnet):
    """
    Take subnet (str), create IP pool in datastore if none exists.
    Allocate next available IP in pool

    :param subnet (str): Subnet to create pool from
    :rtype: (IPPool, IPAddress)
    """
    pool = IPPool(subnet)
    version = IPNetwork(subnet).version
    datastore_client.add_ip_pool(version, pool)
    candidate = SequentialAssignment().allocate(pool)
    candidate = IPAddress(candidate)

    _log.info("Using Pool %s" % pool)
    _log.info("Using IP %s" % candidate)

    return pool, candidate
Example #4
0
def assign_ipv4():
    """
    Assign a IPv4 address from the configured pools.
    :return: An IPAddress, or None if an IP couldn't be
             assigned
    """
    ip = None

    # For each configured pool, attempt to assign an IP before giving up.
    for pool in datastore.get_ip_pools("v4"):
        assigner = SequentialAssignment()
        ip = assigner.allocate(pool)
        if ip is not None:
            _log.info("Found free address %s in pool %s", ip, pool)
            ip = IPAddress(ip)
            break
        else:
            _log.error("Couldn't assign address in pool %s", pool)
    return ip