コード例 #1
0
def ensure_device_present(nb, nb_endpoint, normalized_data):
    '''
    :returns dict(device, msg, changed, diff): dictionary resulting of the request,
        where `device` is the serialized device fetched or newly created in
        Netbox
    '''
    data = _find_ids(nb, normalized_data)
    nb_device = nb_endpoint.get(name=data["name"])
    result = {}
    if not nb_device:
        device, diff = create_netbox_object(nb_endpoint, data, module.check_mode)
        msg = "Device %s created" % (data["name"])
        changed = True
        result["diff"] = diff
    else:
        device, diff = update_netbox_object(nb_device, data, module.check_mode)
        if device is False:
            module.fail_json(
                msg="Request failed, couldn't update device: %s" % data["name"]
            )
        if diff:
            msg = "Device %s updated" % (data["name"])
            changed = True
            result["diff"] = diff
        else:
            msg = "Device %s already exists" % (data["name"])
            changed = False
    result.update({"device": device, "changed": changed, "msg": msg})
    return result
コード例 #2
0
def ensure_interface_present(nb, nb_endpoint, data):
    """
    :returns dict(interface, msg, changed): dictionary resulting of the request,
    where 'interface' is the serialized interface fetched or newly created in Netbox
    """

    if not isinstance(data, dict):
        changed = False
        return {"msg": data, "changed": changed}

    nb_intf = nb_endpoint.get(name=data["name"], device_id=data["device"])
    result = dict()

    if not nb_intf:
        intf, diff = create_netbox_object(nb_endpoint, data, module.check_mode)
        changed = True
        msg = "Interface %s created" % (data["name"])
    else:
        intf, diff = update_netbox_object(nb_intf, data, module.check_mode)
        if intf is False:
            module.fail_json(msg="Request failed, couldn't update device: %s" %
                             (data["name"]))
        if diff:
            msg = "Interface %s updated" % (data["name"])
            changed = True
            result["diff"] = diff
        else:
            msg = "Interface %s already exists" % (data["name"])
            changed = False
    result.update({"interface": intf, "msg": msg, "changed": changed})
    return result
コード例 #3
0
def get_or_create_prefix(nb_endpoint, data):
    try:
        nb_prefix = _search_prefix(nb_endpoint, data)
    except ValueError:
        return _error_multiple_prefix_results(data)

    result = dict()
    if not nb_prefix:
        prefix, diff = create_netbox_object(nb_endpoint, data,
                                            module.check_mode)
        changed = True
        msg = "Prefix %s created" % (prefix["prefix"])
        result["diff"] = diff
    else:
        prefix, diff = update_netbox_object(nb_prefix, data, module.check_mode)
        if prefix is False:
            module.fail_json(msg="Request failed, couldn't update prefix: %s" %
                             (data["prefix"]))
        if diff:
            msg = "Prefix %s updated" % (data["prefix"])
            changed = True
            result["diff"] = diff
        else:
            msg = "Prefix %s already exists" % (data["prefix"])
            changed = False

    result.update({"prefix": prefix, "msg": msg, "changed": changed})
    return result
コード例 #4
0
ファイル: test_netbox.py プロジェクト: Vemulashiva/Ansible
def test_create_netbox_object_in_check_mode(endpoint_mock, data,
                                            on_creation_diff):
    serialized_obj, diff = create_netbox_object(endpoint_mock,
                                                data,
                                                check_mode=True)
    assert endpoint_mock.create.not_called()
    assert serialized_obj == data
    assert diff == on_creation_diff
コード例 #5
0
ファイル: test_netbox.py プロジェクト: Vemulashiva/Ansible
def test_create_netbox_object(endpoint_mock, data, on_creation_diff):
    return_value = endpoint_mock.create().serialize()

    serialized_obj, diff = create_netbox_object(endpoint_mock,
                                                data,
                                                check_mode=False)
    assert endpoint_mock.create.called_once_with(data)
    assert serialized_obj == return_value
    assert diff == on_creation_diff
コード例 #6
0
def create_ip_address(nb_endpoint, data):
    if not isinstance(data, dict):
        changed = False
        return {"msg": data, "changed": changed}

    ip_addr, diff = create_netbox_object(nb_endpoint, data, module.check_mode)
    changed = True
    msg = "IP Addresses %s created" % (data["address"])

    return {
        "ip_address": ip_addr,
        "msg": msg,
        "changed": changed,
        "diff": diff
    }
コード例 #7
0
ファイル: netbox_prefix.py プロジェクト: ydd171/public
def get_new_available_prefix(nb_endpoint, data):
    try:
        parent_prefix = _search_prefix(nb_endpoint, data)
    except ValueError:
        return _error_multiple_prefix_results(data)

    result = dict()
    if not parent_prefix:
        changed = False
        msg = "Parent prefix does not exist: %s" % (data["parent"])
        return {"msg": msg, "changed": changed}
    elif parent_prefix.available_prefixes.list():
        prefix, diff = create_netbox_object(parent_prefix.available_prefixes, data, module.check_mode)
        changed = True
        msg = "Prefix %s created" % (prefix["prefix"])
        result["diff"] = diff
    else:
        changed = False
        msg = "No available prefixes within %s" % (data["parent"])

    result.update({"prefix": prefix, "msg": msg, "changed": changed})
    return result
コード例 #8
0
def get_new_available_ip_address(nb_app, data):
    prefix_query = {"prefix": data["prefix"]}
    if data.get("vrf"):
        prefix_query["vrf_id"] = data["vrf"]

    result = {}
    prefix = nb_app.prefixes.get(**prefix_query)
    if not prefix:
        changed = False
        msg = "%s does not exist - please create first" % (data["prefix"])
        return {"msg": msg, "changed": changed}
    elif prefix.available_ips.list():
        ip_addr, diff = create_netbox_object(prefix.available_ips, data,
                                             module.check_mode)
        changed = True
        msg = "IP Addresses %s created" % (ip_addr["address"])
        result["diff"] = diff
    else:
        changed = False
        msg = "No available IPs available within %s" % (data['prefix'])
        return {"msg": msg, "changed": changed}

    result.update({"ip_address": ip_addr, "msg": msg, "changed": changed})
    return result