Skip to content

Commit

Permalink
Merge pull request tbotnz#112 from tbotnz/ttp_template_route
Browse files Browse the repository at this point in the history
ttp_template_route
  • Loading branch information
tbotnz authored Oct 11, 2020
2 parents 3e24fd8 + c2d45e1 commit b2db663
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"custom_scripts": "netpalm/backend/plugins/extensibles/custom_scripts/",
"jinja2_config_templates": "netpalm/backend/plugins/extensibles/j2_config_templates/",
"jinja2_service_templates": "netpalm/backend/plugins/extensibles/j2_service_templates/",
"ttp_templates": "netpalm/backend/plugins/extensibles/ttp_templates/",
"self_api_call_timeout": 15,
"default_webhook_url": "https://9d4f355779c960d7509368ad5a7e3503.m.pipedream.net",
"default_webhook_ssl_verify": true,
Expand Down
1 change: 1 addition & 0 deletions netpalm/backend/core/confload/confload.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, config_filename=None, search_tfsm=True):
self.custom_webhooks = data["custom_webhooks"]
self.webhook_jinja2_templates = data["webhook_jinja2_templates"]
self.log_config_filename = data["log_config_filename"]
self.ttp_templates = data["ttp_templates"]

#load tls
try:
Expand Down
2 changes: 2 additions & 0 deletions netpalm/backend/core/models/netmiko.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class NetmikoSendConfigArgs(BaseModel):
normalize: Optional[bool] = None
use_textfsm: Optional[bool] = None
textfsm_template: Optional[str] = None
use_ttp: Optional[bool] = None
ttp_template: Optional[str] = None
use_genie: Optional[bool] = None
cmd_verify: Optional[bool] = None

Expand Down
6 changes: 6 additions & 0 deletions netpalm/backend/plugins/drivers/netmiko/netmiko_drvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from netmiko import ConnectHandler
from netmiko.cisco_base_connection import CiscoBaseConnection

from netpalm.backend.core.confload.confload import config
from netpalm.backend.core.utilities.rediz_meta import write_meta_error

log = logging.getLogger(__name__)
Expand All @@ -25,6 +26,11 @@ def sendcommand(self, session=False, command=False):
result = {}
for commands in command:
if self.kwarg:
# normalise the ttp template name for ease of use
if "ttp_template" in self.kwarg.keys():
if self.kwarg["ttp_template"]:
template_name = config.ttp_templates + self.kwarg["ttp_template"] + ".ttp"
self.kwarg["ttp_template"] = template_name
response = session.send_command(commands, **self.kwarg)
if response:
result[commands] = response
Expand Down
2 changes: 2 additions & 0 deletions netpalm/backend/plugins/extensibles/ttp_templates/example.ttp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Cisco {{ios}} Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)
3 changes: 3 additions & 0 deletions netpalm/backend/plugins/utilities/ls/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def __init__(self, folder=False):
elif folder == "webhook_script":
self.folder_dir = config.custom_webhooks
self.strip = ".py"
elif folder == "ttp_templates":
self.folder_dir = config.ttp_templates
self.strip = ".ttp"
elif folder == "script":
self.folder_dir = config.custom_scripts
self.strip = ".py"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self):
"j2_config_templates": {"path": config.jinja2_config_templates, "extn": ".j2"},
"j2_service_templates": {"path": config.jinja2_service_templates, "extn": ".j2"},
"j2_webhook_templates": {"path": config.webhook_jinja2_templates, "extn": ".j2"},
"ttp_templates": {"path": config.ttp_templates, "extn": ".ttp"},
"custom_scripts": {"path": config.custom_scripts, "extn": ".py"},
"custom_webhooks": {"path": config.custom_webhooks, "extn": ".py"}
}
Expand Down
3 changes: 2 additions & 1 deletion netpalm/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ fastapi
uvicorn
uvloop
httptools
netmiko
ttp
netmiko==3.3.2
napalm
ncclient
requests
Expand Down
35 changes: 35 additions & 0 deletions netpalm/routers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,41 @@ async def delete_textfsm_template(template_remove: TFSMTemplateRemove):
# j2 routes

# get template list
@router.get("/ttptemplate/", response_model=ResponseBasic)
async def list_ttp_templates():
try:
r = routes["ls"](fldr="ttp_templates")
resp = jsonable_encoder(r)
return resp
except Exception as e:
raise HTTPException(status_code=500, detail=str(e).split("\n"))

# add j2 config template
@router.post("/ttptemplate/", response_model=ResponseBasic)
def add_ttp_template(template: UnivsersalTemplateAdd):
try:
req_data = template.dict()
req_data["route_type"] = "ttp_templates"
add_transaction_log_entry(entry_type=TransactionLogEntryType.unvrsl_tmp_push, data=req_data)
tmplate_mgr = unvrsl()
r = tmplate_mgr.add_template(payload=req_data)
resp = jsonable_encoder(r)
return resp
except Exception as e:
raise HTTPException(status_code=500, detail=str(e).split("\n"))

# remove j2 config template
@router.delete("/ttptemplate/", status_code=204)
def remove_ttp_template(template: UnivsersalTemplateRemove):
try:
req_data = template.dict()
req_data["route_type"] = "ttp_templates"
add_transaction_log_entry(entry_type=TransactionLogEntryType.unvrsl_tmp_delete, data=req_data)
tmplate_mgr = unvrsl()
r = tmplate_mgr.remove_template(payload=req_data)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e).split("\n"))

@router.get("/j2template/config/", response_model=ResponseBasic)
async def list_config_j2_templates():
try:
Expand Down
3 changes: 2 additions & 1 deletion netpalm/worker_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
netmiko
netmiko==3.3.2
ttp
napalm
ncclient
requests
Expand Down

0 comments on commit b2db663

Please sign in to comment.