Skip to content

Commit 8b7b273

Browse files
authored
Create first basic template (#6)
Generate template for edgeos
1 parent 6fb80ce commit 8b7b273

File tree

6 files changed

+648
-0
lines changed

6 files changed

+648
-0
lines changed

interop/edgeos/interfaces.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package edgeos
2+
3+
import "github.com/ffddorf/confgen/netbox/models"
4+
5+
type InterfaceType string
6+
7+
const (
8+
InterfaceTypeBonding InterfaceType = "bonding" // Bonding interface name
9+
InterfaceTypeBridge InterfaceType = "bridge" // Bridge interface name
10+
InterfaceTypeEthernet InterfaceType = "ethernet" // Ethernet interface name
11+
InterfaceTypeInput InterfaceType = "input" // Input functional block (IFB) interface name
12+
InterfaceTypeIpv6Tunnel InterfaceType = "ipv6-tunnel" // IPv6 Tunnel interface
13+
InterfaceTypeL2tpClient InterfaceType = "l2tp-client" // L2TP client interface name
14+
InterfaceTypeL2tpv3 InterfaceType = "l2tpv3" // L2TPv3 interface
15+
InterfaceTypeLoopback InterfaceType = "loopback" // Loopback interface name
16+
InterfaceTypeOpenvpn InterfaceType = "openvpn" // OpenVPN tunnel interface name
17+
InterfaceTypePptpClient InterfaceType = "pptp-client" // PPTP client interface name
18+
InterfaceTypePseudoEthernet InterfaceType = "pseudo-ethernet" // Pseudo Ethernet device name
19+
InterfaceTypeSwitch InterfaceType = "switch" // Switch interface name
20+
InterfaceTypeTunnel InterfaceType = "tunnel" // Tunnel interface
21+
InterfaceTypeVti InterfaceType = "vti" // Virtual Tunnel interface
22+
InterfaceTypeWirelessmodem InterfaceType = "wirelessmodem" // Wireless modem interface name
23+
)
24+
25+
func InterfaceTypeFromNetbox(netboxType models.DcimInterfaceTypeChoices) (edgeosType InterfaceType, ok bool) {
26+
ok = true
27+
switch netboxType {
28+
case models.DcimInterfaceTypeChoicesA100baseTx,
29+
models.DcimInterfaceTypeChoicesA1000baseT,
30+
models.DcimInterfaceTypeChoicesA25gbaseT,
31+
models.DcimInterfaceTypeChoicesA5gbaseT,
32+
models.DcimInterfaceTypeChoicesA10gbaseT,
33+
models.DcimInterfaceTypeChoicesA10gbaseCx4,
34+
models.DcimInterfaceTypeChoicesA1000baseXGbic,
35+
models.DcimInterfaceTypeChoicesA1000baseXSfp,
36+
models.DcimInterfaceTypeChoicesA10gbaseXSfpp,
37+
models.DcimInterfaceTypeChoicesA10gbaseXXfp,
38+
models.DcimInterfaceTypeChoicesA10gbaseXXenpak,
39+
models.DcimInterfaceTypeChoicesA10gbaseXX2,
40+
models.DcimInterfaceTypeChoicesA25gbaseXSfp28,
41+
models.DcimInterfaceTypeChoicesA50gbaseXSfp56,
42+
models.DcimInterfaceTypeChoicesA40gbaseXQsfpp,
43+
models.DcimInterfaceTypeChoicesA50gbaseXSfp28,
44+
models.DcimInterfaceTypeChoicesA100gbaseXCfp,
45+
models.DcimInterfaceTypeChoicesA100gbaseXCfp2,
46+
models.DcimInterfaceTypeChoicesA200gbaseXCfp2,
47+
models.DcimInterfaceTypeChoicesA100gbaseXCfp4,
48+
models.DcimInterfaceTypeChoicesA100gbaseXCpak,
49+
models.DcimInterfaceTypeChoicesA100gbaseXQsfp28,
50+
models.DcimInterfaceTypeChoicesA200gbaseXQsfp56,
51+
models.DcimInterfaceTypeChoicesA400gbaseXQsfpdd,
52+
models.DcimInterfaceTypeChoicesA400gbaseXOsfp,
53+
models.DcimInterfaceTypeChoicesA1gfcSfp,
54+
models.DcimInterfaceTypeChoicesA2gfcSfp,
55+
models.DcimInterfaceTypeChoicesA4gfcSfp,
56+
models.DcimInterfaceTypeChoicesA8gfcSfpp,
57+
models.DcimInterfaceTypeChoicesA16gfcSfpp,
58+
models.DcimInterfaceTypeChoicesA32gfcSfp28,
59+
models.DcimInterfaceTypeChoicesA64gfcQsfpp,
60+
models.DcimInterfaceTypeChoicesA128gfcQsfp28:
61+
edgeosType = InterfaceTypeEthernet
62+
case models.DcimInterfaceTypeChoicesBridge:
63+
edgeosType = InterfaceTypeBridge
64+
case models.DcimInterfaceTypeChoicesLag:
65+
edgeosType = InterfaceTypeBonding
66+
case models.DcimInterfaceTypeChoicesVirtual:
67+
edgeosType = InterfaceTypeLoopback
68+
default:
69+
ok = false
70+
}
71+
return
72+
}

templates/edgeos.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package templates
2+
3+
import (
4+
"strings"
5+
6+
"github.com/ffddorf/confgen/interop/edgeos"
7+
"github.com/ffddorf/confgen/netbox"
8+
"github.com/ffddorf/confgen/netbox/models"
9+
)
10+
11+
func edgeosConfigFromMap(in map[string]interface{}) string {
12+
out := new(strings.Builder)
13+
if err := edgeos.ConfigFromMap(out, in, 0); err != nil {
14+
panic(err)
15+
}
16+
return out.String()
17+
}
18+
19+
type ChildInterface = models.DeviceDeviceDeviceTypeInterfacesInterfaceTypeChild_interfacesInterfaceType
20+
21+
type edgeosInterfaceDef struct {
22+
netbox.Interface
23+
VIFs []ChildInterface
24+
}
25+
26+
// Structures interfaces coming from netbox
27+
// to be more compatible with the config
28+
// structure of EdgeOS.
29+
func edgeosPrepareInterfaces(interfaces []netbox.Interface) map[edgeos.InterfaceType][]edgeosInterfaceDef {
30+
groups := make(map[edgeos.InterfaceType][]edgeosInterfaceDef)
31+
for _, iface := range interfaces {
32+
// find interface type, skip if not known
33+
ifType, ok := edgeos.InterfaceTypeFromNetbox(iface.Type)
34+
if !ok {
35+
continue
36+
}
37+
38+
// skip interfaces that are children
39+
if iface.Parent.Id != "" {
40+
continue
41+
}
42+
43+
outIface := edgeosInterfaceDef{Interface: iface}
44+
45+
// add child interfaces
46+
if len(iface.Child_interfaces) > 0 {
47+
outIface.VIFs = iface.Child_interfaces
48+
}
49+
50+
// add to list in map
51+
if _, ok := groups[ifType]; !ok {
52+
groups[ifType] = make([]edgeosInterfaceDef, 0, 1)
53+
}
54+
groups[ifType] = append(groups[ifType], outIface)
55+
}
56+
57+
return groups
58+
}

templates/edgeos.tpl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
interfaces {
2+
{{- $ifGroups := edgeosPrepareInterfaces .Device.Interfaces }}
3+
{{- range $ifType, $ifs := $ifGroups }}
4+
{{- range $ifs }}
5+
{{ $ifType }} {{ .Name }} {
6+
{{- if .Description }}
7+
description {{ .Description | maybeQuote }}
8+
{{- end }}
9+
{{- range .Ip_addresses }}
10+
address {{ .Address }}
11+
{{- end }}
12+
{{- if not .Enabled }}
13+
disable
14+
{{- end }}
15+
{{- if .Speed }}
16+
speed {{ .Speed }}
17+
{{- end }}
18+
{{- if .Duplex }}
19+
duplex {{ .Duplex }}
20+
{{- end }}
21+
{{- /* edgeosConfigFromMap .ConfigContext */}}
22+
{{- range .VIFs }}
23+
vif {{ .Untagged_vlan.Vid }} {
24+
{{- if .Description }}
25+
description {{ .Description | maybeQuote }}
26+
{{- end }}
27+
{{- range .Ip_addresses }}
28+
address {{ .Address }}
29+
{{- end }}
30+
{{- if not .Enabled }}
31+
disable
32+
{{- end }}
33+
{{- /* edgeosConfigFromMap .ConfigContext */}}
34+
}
35+
{{- end }}
36+
}
37+
{{- end }}
38+
{{- end }}
39+
}

templates/templates.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package templates
2+
3+
import (
4+
_ "embed"
5+
"errors"
6+
"io"
7+
"text/template"
8+
9+
"github.com/ffddorf/confgen/netbox"
10+
)
11+
12+
var (
13+
// all known templates by name
14+
templates = map[string]*template.Template{}
15+
16+
// common template functions
17+
funcs = template.FuncMap{
18+
"maybeQuote": maybeQuote,
19+
"edgeosConfigFromMap": edgeosConfigFromMap,
20+
"edgeosPrepareInterfaces": edgeosPrepareInterfaces,
21+
}
22+
)
23+
24+
// template bodies embedded via `go:embed`
25+
var (
26+
//go:embed edgeos.tpl
27+
edgeosRaw string
28+
// ... load your template file here!
29+
)
30+
31+
func init() {
32+
initTemplate("edgeos", edgeosRaw)
33+
// ... initialize your template instance here!
34+
}
35+
36+
// used to initialize a template globally using its name and body
37+
func initTemplate(name string, tpl string) {
38+
templates[name] = template.Must(
39+
template.New(name).
40+
Funcs(funcs).
41+
Parse(tpl),
42+
)
43+
}
44+
45+
// TemplateData is the data needed to execute a template
46+
type TemplateData struct {
47+
Device *netbox.Device
48+
}
49+
50+
var ErrTemplateNotFound = errors.New("template not found")
51+
52+
// Render executes the template by the given name and
53+
// writes the result into `out`.
54+
func Render(out io.Writer, name string, data TemplateData) error {
55+
templ, ok := templates[name]
56+
if !ok {
57+
return ErrTemplateNotFound
58+
}
59+
return templ.Execute(out, data)
60+
}

0 commit comments

Comments
 (0)