nautobot / nautobot-app-golden-config

Golden Configuration App for Nautobot.
https://docs.nautobot.com/projects/golden-config/en/latest/
Other
101 stars 57 forks source link

Feature: Add REST API to render intended config Jinja templates #824

Closed gsnider2195 closed 3 weeks ago

gsnider2195 commented 1 month ago

This is part of the jinja template editor epic. This PR introduces the /api/plugins/golden-config/generate-intended-config/ REST API endpoint. This endpoint allows template developers to test jinja template rendering of intended configs using an arbitrary git repository on a single device.

This PR also contains some housekeeping changes:

Not implemented

Docs

{9F03993E-46D3-4172-910D-6F567A803E3D}

Sample Data

Git repo used for testing this feature is here

Jinja template used for testing

!
username test password test
no logging console
logging format timestamp high-resolution
logging facility local6
!
hostname {{ hostname }}
ip name-server 1.2.3.4
ip name-server 5.6.7.8
ip domain-name test.example.com
!
ntp source Management1
ntp server 1.2.3.4 prefer
ntp server 5.6.7.8
!
snmp-server contact "Enterprise Network Operations"
snmp-server location {{ location.name }}
snmp-server source-interface Management1
snmp-server community snmptest ro RO-SNMP-ACL
snmp-server host 1.1.1.1 traps version 2c snmptest
snmp-server enable traps entity
snmp-server enable traps lldp
snmp-server enable traps snmp
!
tacacs-server key tacacstest
tacacs-server host 2.2.2.2
ip tacacs source-interface Management1
!
aaa authentication login default group tacacs+ local
aaa authentication enable default group tacacs+ local
aaa authorization console
aaa authorization exec default group tacacs+ none
aaa authorization commands 1,15 default group tacacs+ none
aaa accounting exec default start-stop group tacacs+
aaa accounting commands 15 default start-stop group tacacs+
!
no aaa root
!
banner login
This system is privately owned and operated. Access
to this system is restricted to authorized users
only. Criminal and civil laws prohibit unauthorized
use. Violators will be prosecuted. You must disconnect
immediately if you are not an authorized user.
EOF
!
management api http-commands
  no shutdown
!

Request

curl -X 'GET' \
  'http://localhost:8080/api/plugins/golden-config/generate-intended-config/?device_id=231b8765-054d-4abe-bdbf-cd60e049cd8d&git_repository_id=82c051e0-d0a9-4008-948a-936a409c654a' \
  -H 'accept: application/json'

Response

{
  "intended_config": "!\nusername test password test\nno logging console\nlogging format timestamp high-resolution\nlogging facility local6\n!\nhostname Access Point-10\nip name-server 1.2.3.4\nip name-server 5.6.7.8\nip domain-name test.example.com\n!\nntp source Management1\nntp server 1.2.3.4 prefer\nntp server 5.6.7.8\n!\nsnmp-server contact \"Enterprise Network Operations\"\nsnmp-server location Aisle-41\nsnmp-server source-interface Management1\nsnmp-server community snmptest ro RO-SNMP-ACL\nsnmp-server host 1.1.1.1 traps version 2c snmptest\nsnmp-server enable traps entity\nsnmp-server enable traps lldp\nsnmp-server enable traps snmp\n!\ntacacs-server key tacacstest\ntacacs-server host 2.2.2.2\nip tacacs source-interface Management1\n!\naaa authentication login default group tacacs+ local\naaa authentication enable default group tacacs+ local\naaa authorization console\naaa authorization exec default group tacacs+ none\naaa authorization commands 1,15 default group tacacs+ none\naaa accounting exec default start-stop group tacacs+\naaa accounting commands 15 default start-stop group tacacs+\n!\nno aaa root\n!\nbanner login\nThis system is privately owned and operated. Access\nto this system is restricted to authorized users\nonly. Criminal and civil laws prohibit unauthorized\nuse. Violators will be prosecuted. You must disconnect\nimmediately if you are not an authorized user.\nEOF\n!\nmanagement api http-commands\n  no shutdown\n!",
  "intended_config_lines": [
    "!",
    "username test password test",
    "no logging console",
    "logging format timestamp high-resolution",
    "logging facility local6",
    "!",
    "hostname Access Point-10",
    "ip name-server 1.2.3.4",
    "ip name-server 5.6.7.8",
    "ip domain-name test.example.com",
    "!",
    "ntp source Management1",
    "ntp server 1.2.3.4 prefer",
    "ntp server 5.6.7.8",
    "!",
    "snmp-server contact \"Enterprise Network Operations\"",
    "snmp-server location Aisle-41",
    "snmp-server source-interface Management1",
    "snmp-server community snmptest ro RO-SNMP-ACL",
    "snmp-server host 1.1.1.1 traps version 2c snmptest",
    "snmp-server enable traps entity",
    "snmp-server enable traps lldp",
    "snmp-server enable traps snmp",
    "!",
    "tacacs-server key tacacstest",
    "tacacs-server host 2.2.2.2",
    "ip tacacs source-interface Management1",
    "!",
    "aaa authentication login default group tacacs+ local",
    "aaa authentication enable default group tacacs+ local",
    "aaa authorization console",
    "aaa authorization exec default group tacacs+ none",
    "aaa authorization commands 1,15 default group tacacs+ none",
    "aaa accounting exec default start-stop group tacacs+",
    "aaa accounting commands 15 default start-stop group tacacs+",
    "!",
    "no aaa root",
    "!",
    "banner login",
    "This system is privately owned and operated. Access",
    "to this system is restricted to authorized users",
    "only. Criminal and civil laws prohibit unauthorized",
    "use. Violators will be prosecuted. You must disconnect",
    "immediately if you are not an authorized user.",
    "EOF",
    "!",
    "management api http-commands",
    "  no shutdown",
    "!"
  ]
}
itdependsnetworks commented 4 weeks ago

@jeffkala let's sync on this one. The PR looks good, I just want to make sure that there is thought around where the core rendering logic goes, as an example could/should we make it the same piece of code that is ran in the job

gsnider2195 commented 3 weeks ago

As discussed, we'll update this to spin up a Nornir task to leverage nornir_nautobot.plugins.tasks.dispatcher.default.DispatcherMixin.generate_config instead of going straight to nautobot's render_jinja2

We'll need to set the output_file_location to /dev/null to prevent generate_config from generating a file every time we call it