Пример #1
0
def dryrun(**kwargs):
    lib = kwargs.get("library", False)
    config = kwargs.get("config", False)
    j2conf = kwargs.get("j2config", False)
    webhook = kwargs.get("webhook", False)
    enable_mode = kwargs.get("enable_mode", False)
    result = False

    if j2conf:
        j2confargs = j2conf.get("args")
        try:
            res = render_j2template(j2conf["template"], template_type="config", kwargs=j2confargs)
            config = res["data"]["task_result"]["template_render_result"]
        except Exception as e:
            config = False
            write_meta_error(f"{e}")

    try:
        result = {}
        if lib == "napalm":
            napl = naplm(**kwargs)
            sesh = napl.connect()
            result = napl.config(session=sesh, command=config, dry_run=True)
            napl.logout(sesh)
        elif lib == "ncclient":
            # if we rendered j2config, add it to the kwargs['args'] dict
            if j2conf and config:
                if not kwargs.get('args', False):
                    kwargs['args'] = {}
                kwargs['args']['config'] = config
            ncc = ncclien(**kwargs)
            sesh = ncc.connect()
            result = ncc.editconfig(session=sesh, dry_run=True)
            ncc.logout(sesh)
        elif lib == "netmiko":
            netmik = netmko(**kwargs)
            sesh = netmik.connect()
            result = netmik.config(sesh, config, enable_mode, dry_run=True)
            netmik.logout(sesh)
    except Exception as e:
        write_meta_error(f"{e}")

    try:
        if webhook:
            current_jobdata = render_netpalm_payload(job_result=result)
            exec_webhook_func(jobdata=current_jobdata, webhook_payload=webhook)
    except Exception as e:
        write_meta_error(f"{e}")

    return result
Пример #2
0
def test_ncclient_getmethod(ncclient_manager: Mock, rq_job):
    c_arg_copy = NCCLIENT_C_ARGS.copy()
    args = {
        "source":
        "running",
        "filter":
        "<filter type='subtree'>"
        "<System xmlns='http://cisco.com/ns/yang/cisco-nx-os-device'></System>"
        "</filter>",
    }
    ncclient_driver = ncclien(args=args, connection_args=c_arg_copy)
    sesh = ncclient_driver.connect()
    result = ncclient_driver.getmethod(sesh)
    sesh.get.assert_called_with(**args)
    assert result["get_config"] is sesh.get().data_xml
Пример #3
0
def test_ncclient_editconfig_dry_run(ncclient_manager: Mock, rq_job):
    c_arg_copy = NCCLIENT_C_ARGS.copy()
    args = {
        "source":
        "running",
        "filter":
        "<filter type='subtree'>"
        "<System xmlns='http://cisco.com/ns/yang/cisco-nx-os-device'></System>"
        "</filter>",
    }
    ncclient_driver = ncclien(args=args, connection_args=c_arg_copy)
    sesh = ncclient_driver.connect()
    result = ncclient_driver.editconfig(sesh, dry_run=True)
    sesh.edit_config.assert_called_with(**args)
    assert result["edit_config"] is sesh.edit_config().xml
    assert not sesh.commit.called
    assert sesh.discard_changes.called
Пример #4
0
def test_ncclient_editconfig_rjson(ncclient_manager: Mock, rq_job, xml_parse):
    c_arg_copy = NCCLIENT_C_ARGS.copy()
    args = {
        "source":
        "running",
        "filter":
        "<filter type='subtree'>"
        "<System xmlns='http://cisco.com/ns/yang/cisco-nx-os-device'></System>"
        "</filter>",
        "render_json":
        True
    }
    ncclient_driver = ncclien(args=args.copy(), connection_args=c_arg_copy)
    sesh = ncclient_driver.connect()
    result = ncclient_driver.editconfig(sesh)
    sesh.edit_config.assert_called_with(
        source=args["source"], filter=args["filter"])  # excluding render_json
    assert result["edit_config"] is xml_parse()
Пример #5
0
def ncclient_get(**kwargs):
    """main function for executing getconfig commands to southbound drivers"""
    lib = kwargs.get("library", False)

    result = False

    try:
        result = {}
        if lib == "ncclient":
            ncc = ncclien(**kwargs)
            sesh = ncc.connect()
            result = ncc.getmethod(sesh)
            ncc.logout(sesh)
        else:
            raise NotImplementedError(f"unknown 'library' parameter {lib}")
    except Exception as e:
        write_meta_error(f"{e}")

    return result
Пример #6
0
def exec_command(**kwargs):
    """main function for executing getconfig commands to southbound drivers"""
    log.debug(f'called w/ {kwargs}')
    lib = kwargs.get("library", False)
    command = kwargs.get("command", False)
    webhook = kwargs.get("webhook", False)
    post_checks = kwargs.get("post_checks", False)

    result = False

    if type(command) == str:
        commandlst = [command]
    else:
        commandlst = command

    if not post_checks:
        try:
            result = {}
            if lib == "netmiko":
                netmik = netmko(**kwargs)
                sesh = netmik.connect()
                result = netmik.sendcommand(sesh, commandlst)
                netmik.logout(sesh)
            elif lib == "napalm":
                napl = naplm(**kwargs)
                sesh = napl.connect()
                result = napl.sendcommand(sesh, commandlst)
                napl.logout(sesh)
            elif lib == "ncclient":
                ncc = ncclien(**kwargs)
                sesh = ncc.connect()
                result = ncc.getconfig(sesh)
                ncc.logout(sesh)
            elif lib == "restconf":
                rc = restconf(**kwargs)
                sesh = rc.connect()
                result = rc.sendcommand(sesh)
                rc.logout(sesh)
            else:
                raise NotImplementedError(f"unknown 'library' parameter {lib}")
        except Exception as e:
            write_meta_error(f"{e}")

    else:
        try:
            result = {}
            if lib == "netmiko":
                netmik = netmko(**kwargs)
                sesh = netmik.connect()
                if commandlst:
                    result = netmik.sendcommand(sesh, commandlst)
                if post_checks:
                    for postcheck in post_checks:
                        command = postcheck["get_config_args"]["command"]
                        post_check_result = netmik.sendcommand(sesh, [command])
                        for matchstr in postcheck["match_str"]:
                            if postcheck[
                                    "match_type"] == "include" and matchstr not in str(
                                        post_check_result):
                                write_meta_error(
                                    f"PostCheck Failed: {matchstr} not found in {post_check_result}"
                                )
                            if postcheck[
                                    "match_type"] == "exclude" and matchstr in str(
                                        post_check_result):
                                write_meta_error(
                                    f"PostCheck Failed: {matchstr} found in {post_check_result}"
                                )
                netmik.logout(sesh)
            elif lib == "napalm":
                napl = naplm(**kwargs)
                sesh = napl.connect()
                if commandlst:
                    result = napl.sendcommand(sesh, commandlst)
                if post_checks:
                    for postcheck in post_checks:
                        command = postcheck["get_config_args"]["command"]
                        post_check_result = napl.sendcommand(sesh, [command])
                        for matchstr in postcheck["match_str"]:
                            if postcheck[
                                    "match_type"] == "include" and matchstr not in str(
                                        post_check_result):
                                write_meta_error(
                                    f"PostCheck Failed: {matchstr} not found in {post_check_result}"
                                )
                            if postcheck[
                                    "match_type"] == "exclude" and matchstr in str(
                                        post_check_result):
                                write_meta_error(
                                    f"PostCheck Failed: {matchstr} found in {post_check_result}"
                                )
                napl.logout(sesh)
            elif lib == "ncclient":
                ncc = ncclien(**kwargs)
                sesh = ncc.connect()
                result = ncc.getconfig(sesh)
                ncc.logout(sesh)
            elif lib == "restconf":
                rc = restconf(**kwargs)
                sesh = rc.connect()
                result = rc.sendcommand(sesh)
                rc.logout(sesh)
        except Exception as e:
            write_meta_error(f"{e}")

    try:
        if webhook:
            current_jobdata = render_netpalm_payload(job_result=result)
            exec_webhook_func(jobdata=current_jobdata, webhook_payload=webhook)
    except Exception as e:
        write_meta_error(f"{e}")

    return result
Пример #7
0
def test_ncclient_getmethod_empty_args(ncclient_manager: Mock, rq_job):
    c_arg_copy = NCCLIENT_C_ARGS.copy()
    ncclient_driver = ncclien(connection_args=c_arg_copy)
    sesh = ncclient_driver.connect()
    with pytest.raises(Exception):
        result = ncclient_driver.getmethod(sesh)
Пример #8
0
def test_ncclient_connect(ncclient_manager: Mock, rq_job):
    c_arg_copy = NCCLIENT_C_ARGS.copy()
    ncclient_driver = ncclien(kwarg={}, connection_args=c_arg_copy)
    sesh = ncclient_driver.connect()
    assert sesh is ncclient_manager.mocked_session
    ncclient_manager.connect.assert_called_with(**c_arg_copy)
Пример #9
0
def exec_config(**kwargs):
    """main function for executing setconfig commands to southbound drivers"""
    lib = kwargs.get("library", False)
    config = kwargs.get("config", False)
    j2conf = kwargs.get("j2config", False)
    webhook = kwargs.get("webhook", False)
    pre_checks = kwargs.get("pre_checks", False)
    post_checks = kwargs.get("post_checks", False)
    enable_mode = kwargs.get("enable_mode", False)

    result = False
    pre_check_ok = True

    try:
        write_mandatory_meta()

        if j2conf:
            j2confargs = j2conf.get("args")
            res = render_j2template(j2conf["template"],
                                    template_type="config",
                                    kwargs=j2confargs)
            config = res["data"]["task_result"]["template_render_result"]

        if not pre_checks and not post_checks:

            if lib == "netmiko":
                netmik = netmko(**kwargs)
                sesh = netmik.connect()
                result = netmik.config(sesh, config, enable_mode)
                netmik.logout(sesh)
            elif lib == "napalm":
                napl = naplm(**kwargs)
                sesh = napl.connect()
                result = napl.config(sesh, config)
                napl.logout(sesh)
            elif lib == "ncclient":
                # if we rendered j2config, add it to the kwargs['args'] dict
                if j2conf and config:
                    if not kwargs.get('args', False):
                        kwargs['args'] = {}
                    kwargs['args']['config'] = config
                ncc = ncclien(**kwargs)
                sesh = ncc.connect()
                result = ncc.editconfig(sesh)
                ncc.logout(sesh)
            elif lib == "restconf":
                rcc = restconf(**kwargs)
                sesh = rcc.connect()
                result = rcc.config(sesh)
                rcc.logout(sesh)

        else:
            if lib == "netmiko":
                netmik = netmko(**kwargs)
                sesh = netmik.connect()
                if pre_checks:
                    for precheck in pre_checks:
                        command = precheck["get_config_args"]["command"]
                        pre_check_result = netmik.sendcommand(sesh, [command])
                        for matchstr in precheck["match_str"]:
                            if precheck[
                                    "match_type"] == "include" and matchstr not in str(
                                        pre_check_result):
                                raise NetpalmCheckError(
                                    f"PreCheck Failed: {matchstr} not found in {pre_check_result}"
                                )
                            if precheck[
                                    "match_type"] == "exclude" and matchstr in str(
                                        pre_check_result):
                                raise NetpalmCheckError(
                                    f"PreCheck Failed: {matchstr} found in {pre_check_result}"
                                )

                if pre_check_ok:
                    result = netmik.config(sesh, config, enable_mode)
                    if post_checks:
                        for postcheck in post_checks:
                            command = postcheck["get_config_args"]["command"]
                            post_check_result = netmik.sendcommand(
                                sesh, [command])
                            for matchstr in postcheck["match_str"]:
                                if postcheck[
                                        "match_type"] == "include" and matchstr not in str(
                                            post_check_result):
                                    raise NetpalmCheckError(
                                        f"PostCheck Failed: {matchstr} not found in {post_check_result}"
                                    )
                                if postcheck[
                                        "match_type"] == "exclude" and matchstr in str(
                                            post_check_result):
                                    raise NetpalmCheckError(
                                        f"PostCheck Failed: {matchstr} found in {post_check_result}"
                                    )
                netmik.logout(sesh)

            elif lib == "napalm":
                napl = naplm(**kwargs)
                sesh = napl.connect()
                if pre_checks:
                    for precheck in pre_checks:
                        command = precheck["get_config_args"]["command"]
                        pre_check_result = napl.sendcommand(sesh, [command])
                        for matchstr in precheck["match_str"]:
                            if precheck[
                                    "match_type"] == "include" and matchstr not in str(
                                        pre_check_result):
                                raise NetpalmCheckError(
                                    f"PreCheck Failed: {matchstr} not found in {pre_check_result}"
                                )
                            if precheck[
                                    "match_type"] == "exclude" and matchstr in str(
                                        pre_check_result):
                                raise NetpalmCheckError(
                                    f"PreCheck Failed: {matchstr} found in {pre_check_result}"
                                )

                if pre_check_ok:
                    result = napl.config(sesh, config)
                    if post_checks:
                        for postcheck in post_checks:
                            command = postcheck["get_config_args"]["command"]
                            post_check_result = napl.sendcommand(
                                sesh, [command])
                            for matchstr in postcheck["match_str"]:
                                if postcheck[
                                        "match_type"] == "include" and matchstr not in str(
                                            post_check_result):
                                    raise NetpalmCheckError(
                                        f"PostCheck Failed: {matchstr} not found in {post_check_result}"
                                    )
                                if postcheck[
                                        "match_type"] == "exclude" and matchstr in str(
                                            post_check_result):
                                    raise NetpalmCheckError(
                                        f"PostCheck Failed: {matchstr} found in {post_check_result}"
                                    )
                napl.logout(sesh)

            elif lib == "ncclient":
                ncc = ncclien(**kwargs)
                sesh = ncc.connect()
                result = ncc.editconfig(sesh)
                ncc.logout(sesh)
            elif lib == "restconf":
                rcc = restconf(**kwargs)
                sesh = rcc.connect()
                result = rcc.config(sesh)
                rcc.logout(sesh)

        if webhook:
            current_jobdata = render_netpalm_payload(job_result=result)
            exec_webhook_func(jobdata=current_jobdata, webhook_payload=webhook)

    except Exception as e:
        write_meta_error(e)

    return result