コード例 #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
def ensure_ip_address_present(nb_endpoint, data):
    """
    :returns dict(ip_address, msg, changed): dictionary resulting of the request,
    where 'ip_address' is the serialized ip fetched or newly created in Netbox
    """
    if not isinstance(data, dict):
        changed = False
        return {"msg": data, "changed": changed}

    try:
        nb_addr = _search_ip(nb_endpoint, data)
    except ValueError:
        return _error_multiple_ip_results(data)

    result = {}
    if not nb_addr:
        return create_ip_address(nb_endpoint, data)
    else:
        ip_addr, diff = update_netbox_object(nb_addr, data, module.check_mode)
        if ip_addr is False:
            module.fail_json(msg="Request failed, couldn't update IP: %s" %
                             (data["address"]))
        if diff:
            msg = "IP Address %s updated" % (data["address"])
            changed = True
            result["diff"] = diff
        else:
            ip_addr = nb_addr.serialize()
            changed = False
            msg = "IP Address %s already exists" % (data["address"])

        return {"ip_address": ip_addr, "msg": msg, "changed": changed}
コード例 #5
0
ファイル: test_netbox.py プロジェクト: Vemulashiva/Ansible
def test_update_netbox_object_with_changes(nb_obj_mock, changed_serialized_obj,
                                           on_update_diff):
    serialized_obj, diff = update_netbox_object(nb_obj_mock,
                                                changed_serialized_obj,
                                                check_mode=False)
    assert nb_obj_mock.update.called_once_with(changed_serialized_obj)
    assert serialized_obj == nb_obj_mock.serialize()
    assert diff == on_update_diff
コード例 #6
0
ファイル: test_netbox.py プロジェクト: Vemulashiva/Ansible
def test_update_netbox_object_no_changes(nb_obj_mock):
    unchanged_data = nb_obj_mock.serialize()
    serialized_obj, diff = update_netbox_object(nb_obj_mock,
                                                unchanged_data,
                                                check_mode=True)
    assert nb_obj_mock.update.not_called()
    assert serialized_obj == unchanged_data
    assert diff is None
コード例 #7
0
ファイル: test_netbox.py プロジェクト: Vemulashiva/Ansible
def test_update_netbox_object_with_changes_in_check_mode(
        nb_obj_mock, changed_serialized_obj, on_update_diff):
    updated_serialized_obj = nb_obj_mock.serialize().copy()
    updated_serialized_obj.update(changed_serialized_obj)

    serialized_obj, diff = update_netbox_object(nb_obj_mock,
                                                changed_serialized_obj,
                                                check_mode=True)
    assert nb_obj_mock.update.not_called()

    assert serialized_obj == updated_serialized_obj
    assert diff == on_update_diff