예제 #1
0
파일: plugin.py 프로젝트: tonicbupt/nerub
def create_network():
    data = request.get_json(force=True)
    app.logger.debug('CreateNetwork JSON=%s', data)

    network_id = data['NetworkID']
    app.logger.info('Creating profile %s', network_id)

    ip_data = data['IPv4Data']
    if ip_data:
        Network.create(network_id, ip_data[0]['Pool'])

    app.logger.debug('CreateNetwork response JSON=%s', '{}')
    return jsonify({})
예제 #2
0
파일: plugin.py 프로젝트: tonicbupt/nerub
def create_endpoint():
    data = request.get_json(force=True)
    app.logger.debug('CreateEndpoint JSON=%s', data)

    endpoint_id = data['EndpointID']
    network_id = data['NetworkID']
    interface = data['Interface']

    app.logger.info('Creating endpoint %s', endpoint_id)

    # docker sent me 172.19.0.3/16 ...
    address_ip4 = interface.get('Address', None)
    if address_ip4 and '/' in address_ip4:
        address_ip4 = IPAddress(address_ip4.split('/', 1)[0])

    network = Network.get(network_id)

    if not network:
        error_message = "CreateEndpoint called but network doesn\'t exist" \
                        " Endpoint ID: %s Network ID: %s" % \
                        (endpoint_id, network_id)
        app.logger.error(error_message)
        raise Exception(error_message)

    network.acquire_ip(endpoint_id, hostname, ip=address_ip4)
    app.logger.debug('CreateEndpoint response JSON=%s', {})
    return jsonify({})
예제 #3
0
def test_ip_acquire(test_db):
    network_id = hashlib.sha1('network_id').hexdigest()
    ep_id1 = hashlib.sha1('ep_id1').hexdigest()
    ep_id2 = hashlib.sha1('ep_id2').hexdigest()

    network = Network.create(network_id, '10.100.0.1/16')

    ip1 = network.acquire_ip(ep_id1, 'hostname')
    assert ip1 is not None
    assert ip1.hostname == 'hostname'
    assert ip1.endpoint_id == ep_id1
    assert ip1.network_id == network_id
    assert ip1.ip

    ip2 = network.acquire_ip(ep_id2, 'hostname', '10.100.0.2')
    assert ip2 is not None
    assert ip2.hostname == 'hostname'
    assert ip2.endpoint_id == ep_id2
    assert ip2.network_id == network_id
    assert str(ip2.ip) == '10.100.0.2'

    ip3 = network.acquire_ip(ep_id2, 'hostname', '10.101.0.1')
    assert ip3 is None

    ip4 = IP.get(ep_id1)
    assert ip4.endpoint_id == ip1.endpoint_id == ep_id1
    ip5 = IP.get(ep_id2)
    assert ip5.endpoint_id == ip2.endpoint_id == ep_id2
예제 #4
0
def test_network_create(test_db):
    network_id = hashlib.sha1('network_id').hexdigest()
    network = Network.create(network_id, '10.100.0.1/16')
    assert network is not None
    assert network.network_id == network_id
    assert network.cidr == IPNetwork('10.100.0.1/16')
    assert network.cidr.size == 65536

    n1 = Network.get(network_id)
    assert n1.network_id == network.network_id
    n2 = Network.get('random id')
    assert n2 is None

    n3 = Network.get_by_cidr('10.100.0.1/16')
    assert n3.network_id == network.network_id
    n4 = Network.get_by_cidr('10.100.0.1/15')
    assert n4 is None
예제 #5
0
def test_network_delete(test_db):
    network_id1 = hashlib.sha1('network_id1').hexdigest()
    network_id2 = hashlib.sha1('network_id2').hexdigest()
    ep_id = hashlib.sha1('ep_id').hexdigest()

    n1 = Network.create(network_id1, '10.100.0.1/16')
    n1.delete()
    n2 = Network.get(network_id1)
    assert n2 is None

    n3 = Network.create(network_id2, '10.100.0.1/16')
    n3.acquire_ip(ep_id, 'hostname')
    with pytest.raises(Exception):
        n3.delete()

    n4 = Network.get(network_id2)
    assert n4.network_id == n3.network_id == network_id2
예제 #6
0
파일: plugin.py 프로젝트: tonicbupt/nerub
def delete_network():
    data = request.get_json(force=True)
    app.logger.debug('DeleteNetwork JSON=%s', data)

    network_id = data['NetworkID']
    network = Network.get(network_id)
    if network:
        network.delete()

    app.logger.info('Removed network %s', network_id)
    return jsonify({})