Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
0
def ensure_site_present(nb, nb_endpoint, data):
    """
    :returns dict(interface, msg, changed): dictionary resulting of the request,
    where 'site' is the serialized interface fetched or newly created in Netbox
    """

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

    nb_site = nb_endpoint.get(slug=data["slug"])
    result = dict()
    if not nb_site:
        site, diff = create_netbox_object(nb_endpoint, data, module.check_mode)
        changed = True
        msg = "Site %s created" % (data["name"])
        result["diff"] = diff
    else:
        site, diff = update_netbox_object(nb_site, data, module.check_mode)
        if site is False:
            module.fail_json(
                msg="Request failed, couldn't update device: %s" % (data["name"])
            )
        if diff:
            msg = "Site %s updated" % (data["name"])
            changed = True
            result["diff"] = diff
        else:
            msg = "Site %s already exists" % (data["name"])
            changed = False

    result.update({"site": site, "msg": msg, "changed": changed})
    return result
Ejemplo n.º 4
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
    }
Ejemplo n.º 5
0
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
Ejemplo n.º 6
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