def config(self, session=False, command=False): try: result = {} url = self.transport + "://" + self.host + ":" + str( self.port) + self.kwarg["uri"] if hasattr(requests, str(self.action)): response = getattr(requests, str(self.action))( url, auth=(self.username, self.password), data=json.dumps(self.payload), params=self.params, headers=self.headers, **self.connection_args) try: res = json.loads(response.text) except Exception: res = "" pass result[url] = {} result[url]["status_code"] = response.status_code result[url]["result"] = res return result else: raise Exception(self.action + " not found in requests") except Exception as e: write_meta_error(f"{e}")
def connect(self): try: driver = napalm.get_network_driver(self.driver) napalmses = driver(**self.connection_args) return napalmses except Exception as e: write_meta_error(f"{e}")
def connect(self): try: if not self.headers: self.headers = self.default_headers else: del self.connection_args["headers"] return True except Exception as e: write_meta_error(f"{e}")
def render_service(**kwargs): templat = kwargs.get("netpalm_service_name") exeservice = False try: s = service() res = s.validate_template(template_name=templat, kwargs=kwargs) exeservice = s.execute_service(service=res, kwargs=kwargs) except Exception as e: write_meta_error(f"render_service {e}") return exeservice
def validate_template(self, template_name, **kwargs): try: kwarg = kwargs.get('kwargs', False) args = kwarg.get("args") rendered_template = render_j2template(templat=template_name, template_type="service", kwargs=args) data = json.loads(rendered_template["data"]["task_result"] ["template_render_result"]) model_service_template(__root__=data) return data except Exception as e: write_meta_error(f"validate_template {e}")
def sendcommand(self, session=False, command=False): try: result = {} session.open() for c in command: if hasattr(session, str(c)): response = getattr(session, str(c))() result[c] = response else: response = session.cli([c]) result[c] = response[c].split("\n") return result except Exception as e: write_meta_error(f"{e}")
def editconfig(self, session=False, dry_run=False): try: result = {} if self.kwarg: response = session.edit_config(**self.kwarg) if dry_run: session.discard_changes() else: session.commit() if response: result["edit_config"] = response else: write_meta_error("args are required") return result except Exception as e: write_meta_error(f"{e}")
def script_exec(**kwargs): webhook = kwargs.get("webhook",False) result = False try: scrip = script_kiddy(kwargs=kwargs) result = scrip.s_exec() except Exception as e: write_meta_error(f"{e}") try: if webhook: current_jobdata = prepare_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 config(self, session=False, command=False, dry_run=False): try: if type(command) == list: napalmconfig = "" for comm in command: napalmconfig += comm + "\n" else: napalmconfig = command session.open() session.load_merge_candidate(config=napalmconfig) diff = session.compare_config() if dry_run: response = session.discard_config() else: response = session.commit_config() result = {} result["changes"] = diff.split("\n") return result except Exception as e: write_meta_error(f"{e}")
def execute_api_call(self, oper, payload): try: #update config to include https when finished webserver bundle headers = { 'x-api-key': self.api_key, 'Content-Type': 'application/json' } res = requests.post( f"http://{self.netpalm_container_name}:{self.listen_port}/{oper}", data=payload, timeout=self.self_api_call_timeout, headers=headers) if res.status_code == 201: return res.json() else: write_meta_error( f"error calling self api response {res.status_code}") except Exception as e: write_meta_error(f"execute_api_call service {e}")
def sendcommand(self, session=False, command=False): try: #restconf get call result = {} url = self.transport + "://" + self.host + ":" + str( self.port) + self.kwarg["uri"] response = requests.get(url, auth=(self.username, self.password), params=self.params, headers=self.headers, **self.connection_args) try: res = json.loads(response.text) except Exception: res = "" pass result[url] = {} result[url]["status_code"] = response.status_code result[url]["result"] = res return result except Exception as e: write_meta_error(f"{e}")
def dryrun(**kwargs): lib = kwargs.get("library", False) config = kwargs.get("config", False) j2conf = kwargs.get("j2config", False) webhook = kwargs.get("webhook", 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": ncc = ncclien(**kwargs) sesh = ncc.connect() result = ncc.editconfig(session=sesh, dry_run=True) ncc.logout(sesh) except Exception as e: write_meta_error(f"{e}") try: if webhook: current_jobdata = prepare_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 getconfig(self, session=False, command=False): try: result = {} if self.kwarg: render_json = self.kwarg.get("render_json", False) rjsflag = False if render_json: del self.kwarg["render_json"] rjsflag = True response = session.get_config(**self.kwarg).data_xml if rjsflag: respdict = xmltodict.parse(response) if respdict: result["get_config"] = respdict else: write_meta_error("no response") else: result["get_config"] = response else: write_meta_error("args are required") return result except Exception as e: write_meta_error(f"{e}")
def exec_command(**kwargs): 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 = prepare_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): 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) result = False pre_check_ok = True 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}") if not pre_checks and not post_checks: try: if lib == "netmiko": netmik = netmko(**kwargs) sesh = netmik.connect() result = netmik.config(sesh, config) netmik.logout(sesh) elif lib == "napalm": napl = naplm(**kwargs) sesh = napl.connect() result = napl.config(sesh, config) 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) except Exception as e: write_meta_error(f"{e}") else: try: 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): write_meta_error( f"PreCheck Failed: {matchstr} not found in {pre_check_result}" ) pre_check_ok = False if precheck[ "match_type"] == "exclude" and matchstr in str( pre_check_result): write_meta_error( f"PreCheck Failed: {matchstr} found in {pre_check_result}" ) pre_check_ok = False if pre_check_ok: result = netmik.config(sesh, config) 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 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): write_meta_error( f"PreCheck Failed: {matchstr} not found in {pre_check_result}" ) pre_check_ok = False if precheck[ "match_type"] == "exclude" and matchstr in str( pre_check_result): write_meta_error( f"PreCheck Failed: {matchstr} found in {pre_check_result}" ) pre_check_ok = False 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): 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.editconfig(sesh) ncc.logout(sesh) elif lib == "restconf": rcc = restconf(**kwargs) sesh = rcc.connect() result = rcc.config(sesh) rcc.logout(sesh) except Exception as e: write_meta_error(f"{e}") try: if webhook: current_jobdata = prepare_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 logout(self, session): try: response = session.close_session() return response except Exception as e: write_meta_error(f"{e}")
def connect(self): try: conn = manager.connect(**self.connection_args) return conn except Exception as e: write_meta_error(f"{e}")
def logout(self, session): try: return True except Exception as e: write_meta_error(f"{e}")