def test_netmko_config(netmiko_connection_handler: Mock, rq_job): netmiko_driver = netmko(kwarg={}, connection_args={}) mock_session = netmiko_driver.connect() netmiko_connection_handler.assert_called( ) # make *certain* mock is getting used _ = netmiko_driver.config(mock_session, "hostname asdf", dry_run=True, enter_enable=True) mock_session.send_config_set.assert_called_once_with(["hostname asdf"]) assert not mock_session.commit.called assert mock_session.enable.called mock_session.commit.reset_mock() mock_session.enable.reset_mock() _ = netmiko_driver.config(mock_session, "hostname asdf") mock_session.send_config_set.assert_called_with(["hostname asdf"]) assert mock_session.commit.called assert not mock_session.enable.called del mock_session.commit _ = netmiko_driver.config(mock_session, "hostname asdf") mock_session.send_config_set.assert_called_with(["hostname asdf"]) assert mock_session.save_config.called
def test_netmko_sendcommand(netmiko_connection_handler: Mock): netmiko_driver = netmko(kwarg={}, connection_args={}) sesh = netmiko_driver.connect() netmiko_connection_handler.assert_called( ) # make *certain* mock is getting used result = netmiko_driver.sendcommand(sesh, NETMIKO_COMMANDS.keys()) for command in NETMIKO_COMMANDS.keys(): netmiko_connection_handler.session.send_command.assert_any_call( command) for command, value in NETMIKO_COMMANDS.items(): assert result[command] == value.splitlines()
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
def test_netmko_connect(netmiko_connection_handler: Mock): netmiko_driver = netmko(kwarg={}, connection_args=NETMIKO_C_ARGS) sesh = netmiko_driver.connect() netmiko_connection_handler.assert_called_once_with(**NETMIKO_C_ARGS)
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
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