Esempio n. 1
0
    def test_sequential_assignment_full_pool(self):
        four_pool = IPNetwork("192.168.0.0/30")
        assigner = SequentialAssignment()

        assert_equal("192.168.0.1", assigner.allocate(four_pool))
        assert_equal("192.168.0.2", assigner.allocate(four_pool))
        assert_equal(None, assigner.allocate(four_pool))
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
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
Esempio n. 6
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
Esempio n. 7
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
Esempio n. 8
0
 def test_sequential_assignment_tiny_pool(self):
     assigner = SequentialAssignment()
     assert_equal(None, assigner.allocate(IPNetwork("192.168.0.0/31")))
Esempio n. 9
0
 def test_sequential_assignment_pool(self):
     assigner = SequentialAssignment()
     assert_equal("192.168.0.1", assigner.allocate(pool))
Esempio n. 10
0
 def test_sequential_assignment_network(self):
     assigner = SequentialAssignment()
     assert_equal("192.168.0.1", assigner.allocate(network))