From a review of everything since v0.3.0: - NetBox's UI was served on Rollcall's origin, so a script in a NetBox page (stored XSS, a custom link) could read the Rollcall UI's stored admin token and call Rollcall's API with the admin certificate. It now has a listener of its own (ipam.ui_listen, default :8444): another origin. The Rollcall UI builds its NetBox links from that port. - The proxy dropped X-Rollcall-User but not X-Rollcall_User, which a WSGI server reads as the same HTTP_X_ROLLCALL_USER; every spelling is dropped now. - Deployed machines fetched the root bundle over TLS checked against the system's trust store: a public CA's certificate for the rack's name would have done. The refresh is pinned to the Rollcall roots installed on the machine and takes only CA certificates (tried on node001 and node002). - A NetBox device type NetBox will not resize (0U while its devices have positions) no longer stops those machines' device sync; it is logged. - A NetBox cable on a device whose machine is gone here rolled back all cable and switch sync; such cables are skipped. - Switch names are looked up in NetBox 50 at a time (a large site would outgrow one URL). - rack_units: "vendor model" wins over the bare model when both are set (map order chose before); cable precedence comments match the code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
542 lines
17 KiB
Go
542 lines
17 KiB
Go
package main
|
|
|
|
// Cabling: what each machine's ports are plugged into, and where the
|
|
// switches at the other end stand. A port's switch port comes from, first,
|
|
// the LLDP its commissioning saw (lldpcli's neighbours per interface): what
|
|
// is really there; then one entered here; then a cable recorded in NetBox on
|
|
// the machine's device (dcim interfaces, through patch panels). The BMC's port
|
|
// is not seen by the host's LLDP: its cable (to the out-of-band switch) is
|
|
// NetBox's or entered here. When NetBox records another switch port than
|
|
// LLDP sees, the port says so (netbox_says).
|
|
//
|
|
// A switch's place comes from NetBox (the device's site, location, rack and
|
|
// position, read with the layout) or is set here; a switch with neither is
|
|
// drawn where most of its machines are. A switch whose NetBox role names
|
|
// out-of-band or management, or that only BMCs are cabled to, is "oob".
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
type Port struct {
|
|
Name string `json:"name"`
|
|
MAC string `json:"mac,omitempty"`
|
|
Speed string `json:"speed,omitempty"`
|
|
Link *bool `json:"link,omitempty"`
|
|
Card string `json:"card,omitempty"` // PCI device without the function: the ports of one card
|
|
Switch string `json:"switch,omitempty"`
|
|
SwitchPort string `json:"switch_port,omitempty"`
|
|
PortDescr string `json:"port_descr,omitempty"`
|
|
VLAN string `json:"vlan,omitempty"`
|
|
Kind string `json:"kind,omitempty"` // bmc for the BMC's own port
|
|
Source string `json:"source,omitempty"` // lldp | netbox | local
|
|
NetBoxSays string `json:"netbox_says,omitempty"` // NetBox's switch port when LLDP sees another
|
|
}
|
|
|
|
// Cable is one recorded port-to-switch-port connection (NetBox's or entered here).
|
|
type Cable struct {
|
|
Machine string `json:"machine"`
|
|
Port string `json:"port"` // an interface name, or "bmc"
|
|
Switch string `json:"switch"`
|
|
SwitchPort string `json:"switch_port"`
|
|
Source string `json:"source"`
|
|
}
|
|
|
|
type MachinePorts struct {
|
|
ID string `json:"id"`
|
|
Ports []Port `json:"ports"`
|
|
}
|
|
|
|
type Switch struct {
|
|
Name string `json:"name"`
|
|
Descr string `json:"descr,omitempty"`
|
|
MgmtIP string `json:"mgmt_ip,omitempty"`
|
|
Building string `json:"building,omitempty"`
|
|
Room string `json:"room,omitempty"`
|
|
Rack string `json:"rack,omitempty"`
|
|
Unit int `json:"unit,omitempty"`
|
|
Height int `json:"height,omitempty"`
|
|
Model string `json:"model,omitempty"`
|
|
Role string `json:"role,omitempty"` // oob for an out-of-band (BMC) switch
|
|
Source string `json:"source,omitempty"` // netbox | local | "" (not placed)
|
|
Links int `json:"links"`
|
|
}
|
|
|
|
type lldpNeighbour struct{ Switch, Port, PortDescr, VLAN, Descr, MgmtIP string }
|
|
|
|
// lldpOf reads inventory "lldp": lldpcli's "interface", an object of
|
|
// interfaces or a list of one-interface objects, each one entry or a list.
|
|
func lldpOf(inv obj) map[string]lldpNeighbour {
|
|
out := map[string]lldpNeighbour{}
|
|
add := func(ifn string, v any) {
|
|
for _, e := range asList(v) {
|
|
eo := asObj(e)
|
|
ch := asObj(eo["chassis"])
|
|
var name string
|
|
var c obj
|
|
for k, v := range ch {
|
|
name, c = k, asObj(v)
|
|
}
|
|
if name == "" {
|
|
continue
|
|
}
|
|
nb := lldpNeighbour{Switch: name, Descr: str(c["descr"]), PortDescr: str(dig(eo, "port", "descr"))}
|
|
nb.Port = str(dig(eo, "port", "id", "value"))
|
|
if nb.Port == "" {
|
|
nb.Port = str(dig(eo, "port", "id"))
|
|
}
|
|
for _, ip := range asList(c["mgmt-ip"]) {
|
|
if s := str(ip); s != "" && nb.MgmtIP == "" {
|
|
nb.MgmtIP = s
|
|
}
|
|
}
|
|
for _, v := range asList(eo["vlan"]) {
|
|
if vo := asObj(v); vo != nil && nb.VLAN == "" {
|
|
nb.VLAN = str(vo["vlan-id"])
|
|
}
|
|
}
|
|
if _, seen := out[ifn]; !seen {
|
|
out[ifn] = nb
|
|
}
|
|
}
|
|
}
|
|
switch l := inv["lldp"].(type) {
|
|
case obj:
|
|
for ifn, v := range l {
|
|
add(ifn, v)
|
|
}
|
|
case []any:
|
|
for _, x := range l {
|
|
for ifn, v := range asObj(x) {
|
|
add(ifn, v)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// portsOf: a machine's ports from its inventory, with the switch port each is cabled to
|
|
func portsOf(m *Machine) ([]Port, map[string]lldpNeighbour) {
|
|
inv := invOf(m)
|
|
nb := lldpOf(inv)
|
|
var out []Port
|
|
for _, x := range toList(inv["network"]) {
|
|
n := asObj(x)
|
|
p := Port{Name: str(n["name"]), MAC: str(n["mac"]), Speed: str(n["speed"])}
|
|
if p.Name == "" {
|
|
continue
|
|
}
|
|
if b, ok := n["link"].(bool); ok {
|
|
p.Link = &b
|
|
}
|
|
if p.Speed == "Unknown!" {
|
|
p.Speed = ""
|
|
}
|
|
if bus := str(n["bus_info"]); strings.Count(bus, ":") == 2 { // 0000:3b:00.1 → 0000:3b:00
|
|
p.Card, _, _ = strings.Cut(bus, ".")
|
|
}
|
|
if l, ok := nb[p.Name]; ok {
|
|
p.Switch, p.SwitchPort, p.PortDescr, p.VLAN, p.Source = l.Switch, l.Port, l.PortDescr, l.VLAN, "lldp"
|
|
}
|
|
out = append(out, p)
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].Card < out[j].Card })
|
|
return out, nb
|
|
}
|
|
|
|
// ---------------------------------------------------------------- store
|
|
|
|
func (s *Store) Switches(ctx context.Context) ([]Switch, error) {
|
|
rows, err := s.pool.Query(ctx, `SELECT name, coalesce(building,''), coalesce(room,''), coalesce(rack,''), coalesce(unit,0), coalesce(height,1), coalesce(model,''), coalesce(role,''), source FROM switches ORDER BY name`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []Switch{}
|
|
for rows.Next() {
|
|
var x Switch
|
|
if err := rows.Scan(&x.Name, &x.Building, &x.Room, &x.Rack, &x.Unit, &x.Height, &x.Model, &x.Role, &x.Source); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, x)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// PutSwitch places a switch; one placed by hand is not moved by NetBox.
|
|
func (s *Store) PutSwitch(ctx context.Context, x Switch) error {
|
|
_, err := s.pool.Exec(ctx, `INSERT INTO switches(name, building, room, rack, unit, height, model, role, source) VALUES ($1,$2,$3,$4,nullif($5,0),nullif($6,0),nullif($7,''),nullif($8,''),$9)
|
|
ON CONFLICT (name) DO UPDATE SET building=$2, room=$3, rack=$4, unit=nullif($5,0), height=nullif($6,0), model=coalesce(nullif($7,''), switches.model),
|
|
role=coalesce(nullif($8,''), switches.role), source=$9
|
|
WHERE switches.source <> 'local' OR $9 = 'local'`, x.Name, x.Building, x.Room, x.Rack, x.Unit, x.Height, x.Model, x.Role, orDefault(x.Source, "local"))
|
|
return err
|
|
}
|
|
|
|
func (s *Store) Cables(ctx context.Context) ([]Cable, error) {
|
|
rows, err := s.pool.Query(ctx, `SELECT machine_id, port, switch, switch_port, source FROM cables ORDER BY machine_id, port`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []Cable{}
|
|
for rows.Next() {
|
|
var c Cable
|
|
if err := rows.Scan(&c.Machine, &c.Port, &c.Switch, &c.SwitchPort, &c.Source); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// PutCable records a cable; one entered here is not replaced by NetBox's.
|
|
func (s *Store) PutCable(ctx context.Context, c Cable) error {
|
|
_, err := s.pool.Exec(ctx, `INSERT INTO cables(machine_id, port, switch, switch_port, source) VALUES ($1,$2,$3,$4,$5)
|
|
ON CONFLICT (machine_id, port, source) DO UPDATE SET switch=$3, switch_port=$4`, c.Machine, c.Port, c.Switch, c.SwitchPort, orDefault(c.Source, "local"))
|
|
return err
|
|
}
|
|
|
|
func (s *Store) DeleteCable(ctx context.Context, machine, port, source string) error {
|
|
_, err := s.pool.Exec(ctx, `DELETE FROM cables WHERE machine_id=$1 AND port=$2 AND source=$3`, machine, port, source)
|
|
return err
|
|
}
|
|
|
|
// ReplaceNetBoxCables puts NetBox's cables in place of the ones read before.
|
|
func (s *Store) ReplaceNetBoxCables(ctx context.Context, cs []Cable) error {
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
if _, err := tx.Exec(ctx, `DELETE FROM cables WHERE source='netbox'`); err != nil {
|
|
return err
|
|
}
|
|
for _, c := range cs {
|
|
// a device whose rollcall_machine names no machine here (deleted, typed in) is skipped
|
|
if _, err := tx.Exec(ctx, `INSERT INTO cables(machine_id, port, switch, switch_port, source) SELECT $1,$2,$3,$4,'netbox'
|
|
WHERE EXISTS (SELECT 1 FROM machines WHERE id=$1) ON CONFLICT DO NOTHING`, c.Machine, c.Port, c.Switch, c.SwitchPort); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit(ctx)
|
|
}
|
|
|
|
func (s *Store) DeleteSwitch(ctx context.Context, name string) error {
|
|
_, err := s.pool.Exec(ctx, `DELETE FROM switches WHERE name=$1`, name)
|
|
return err
|
|
}
|
|
|
|
// ---------------------------------------------------------------- handlers
|
|
|
|
// handleCabling: every located machine's ports and the switches they lead to.
|
|
func (a *App) handleCabling(w http.ResponseWriter, r *http.Request) {
|
|
ms, err := a.store.List(r.Context())
|
|
if err != nil {
|
|
fail(w, 500, err)
|
|
return
|
|
}
|
|
placed, _ := a.store.Switches(r.Context())
|
|
sw := map[string]*Switch{}
|
|
key := func(name string) string { // an LLDP name may be the FQDN of a NetBox name
|
|
k, _, _ := strings.Cut(strings.ToLower(name), ".")
|
|
return k
|
|
}
|
|
for i := range placed {
|
|
sw[key(placed[i].Name)] = &placed[i]
|
|
}
|
|
recorded, _ := a.store.Cables(r.Context())
|
|
byMachine := map[string][]Cable{}
|
|
for _, c := range recorded {
|
|
byMachine[c.Machine] = append(byMachine[c.Machine], c)
|
|
}
|
|
nonBMC := map[string]bool{} // switches something other than a BMC is cabled to
|
|
out := []MachinePorts{}
|
|
for _, m := range ms {
|
|
if m.Location.Rack == "" {
|
|
continue
|
|
}
|
|
ports, nb := portsOf(m)
|
|
ports = mergeCables(ports, byMachine[m.ID])
|
|
get := func(name string) *Switch {
|
|
s := sw[key(name)]
|
|
if s == nil {
|
|
s = &Switch{Name: name}
|
|
sw[key(name)] = s
|
|
}
|
|
return s
|
|
}
|
|
for _, l := range nb { // what the switch says of itself
|
|
s := get(l.Switch)
|
|
s.Descr = orDefault(s.Descr, l.Descr)
|
|
s.MgmtIP = orDefault(s.MgmtIP, l.MgmtIP)
|
|
}
|
|
for _, p := range ports {
|
|
if p.Switch == "" {
|
|
continue
|
|
}
|
|
s := get(p.Switch)
|
|
s.Links++
|
|
if p.Kind != "bmc" {
|
|
nonBMC[key(p.Switch)] = true
|
|
}
|
|
}
|
|
if len(ports) > 0 {
|
|
out = append(out, MachinePorts{ID: m.ID, Ports: ports})
|
|
}
|
|
}
|
|
for k, s := range sw {
|
|
if s.Role == "" && s.Links > 0 && !nonBMC[k] {
|
|
s.Role = "oob"
|
|
}
|
|
}
|
|
for _, mp := range out { // switch names as the machines saw them, the place as stored
|
|
for i := range mp.Ports {
|
|
if s := sw[key(mp.Ports[i].Switch)]; s != nil && mp.Ports[i].Switch != "" {
|
|
mp.Ports[i].Switch = s.Name
|
|
}
|
|
}
|
|
}
|
|
list := []Switch{}
|
|
for _, s := range sw {
|
|
list = append(list, *s)
|
|
}
|
|
sort.Slice(list, func(i, j int) bool { return list[i].Name < list[j].Name })
|
|
writeJSON(w, 200, obj{"machines": out, "switches": list})
|
|
}
|
|
|
|
// mergeCables adds the recorded cables to the ports LLDP filled: a port
|
|
// without a neighbour takes the one entered here, else NetBox's; the BMC's
|
|
// port is added when a cable is recorded for it. A NetBox cable that
|
|
// disagrees with LLDP is kept as netbox_says.
|
|
func mergeCables(ports []Port, cs []Cable) []Port {
|
|
pick := func(port string) (nb, local *Cable) {
|
|
for i := range cs {
|
|
if strings.EqualFold(cs[i].Port, port) {
|
|
if cs[i].Source == "netbox" {
|
|
nb = &cs[i]
|
|
} else {
|
|
local = &cs[i]
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
fill := func(p *Port) {
|
|
nb, local := pick(p.Name)
|
|
switch {
|
|
case p.Source == "lldp":
|
|
if nb != nil && (!strings.EqualFold(nb.SwitchPort, p.SwitchPort) || !sameSwitch(nb.Switch, p.Switch)) {
|
|
p.NetBoxSays = nb.Switch + " " + nb.SwitchPort
|
|
}
|
|
case local != nil:
|
|
p.Switch, p.SwitchPort, p.Source = local.Switch, local.SwitchPort, "local"
|
|
case nb != nil:
|
|
p.Switch, p.SwitchPort, p.Source = nb.Switch, nb.SwitchPort, "netbox"
|
|
}
|
|
}
|
|
for i := range ports {
|
|
fill(&ports[i])
|
|
}
|
|
bmc := Port{Name: "bmc", Kind: "bmc"}
|
|
if fill(&bmc); bmc.Switch != "" {
|
|
ports = append(ports, bmc)
|
|
}
|
|
return ports
|
|
}
|
|
|
|
// sameSwitch: an LLDP name may be the FQDN of a NetBox name
|
|
func sameSwitch(a, b string) bool {
|
|
a, _, _ = strings.Cut(strings.ToLower(a), ".")
|
|
b, _, _ = strings.Cut(strings.ToLower(b), ".")
|
|
return a == b
|
|
}
|
|
|
|
// handleCables records a cable by hand: {"machine","port","switch","switch_port"}
|
|
// (port "bmc" for the BMC's); DELETE ?machine=&port= removes it.
|
|
func (a *App) handleCables(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == http.MethodDelete {
|
|
q := r.URL.Query()
|
|
if err := a.store.DeleteCable(r.Context(), q.Get("machine"), q.Get("port"), "local"); err != nil {
|
|
fail(w, 500, err)
|
|
return
|
|
}
|
|
writeJSON(w, 200, obj{"deleted": q.Get("machine") + " " + q.Get("port")})
|
|
return
|
|
}
|
|
var c Cable
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<16)).Decode(&c); err != nil || c.Machine == "" || c.Port == "" || strings.TrimSpace(c.Switch) == "" {
|
|
fail(w, 400, errors.New("machine, port and switch are required"))
|
|
return
|
|
}
|
|
if _, err := a.store.Get(r.Context(), c.Machine); err != nil {
|
|
fail(w, 404, fmt.Errorf("machine %s: %w", c.Machine, err))
|
|
return
|
|
}
|
|
c.Switch, c.SwitchPort, c.Source = strings.TrimSpace(c.Switch), strings.TrimSpace(c.SwitchPort), "local"
|
|
if err := a.store.PutCable(r.Context(), c); err != nil {
|
|
fail(w, 500, err)
|
|
return
|
|
}
|
|
writeJSON(w, 200, c)
|
|
}
|
|
|
|
// handleSwitches places a switch by hand: {"name","building","room","rack","unit","height"};
|
|
// DELETE ?name= forgets the place (NetBox's comes back on the next sync).
|
|
func (a *App) handleSwitches(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == http.MethodDelete {
|
|
if err := a.store.DeleteSwitch(r.Context(), r.URL.Query().Get("name")); err != nil {
|
|
fail(w, 500, err)
|
|
return
|
|
}
|
|
writeJSON(w, 200, obj{"deleted": r.URL.Query().Get("name")})
|
|
return
|
|
}
|
|
var x Switch
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<16)).Decode(&x); err != nil || strings.TrimSpace(x.Name) == "" || x.Rack == "" {
|
|
fail(w, 400, errors.New("name and rack are required"))
|
|
return
|
|
}
|
|
if x.Unit < 0 || x.Unit > 60 || x.Height < 0 || x.Height > 10 {
|
|
fail(w, 400, errors.New("unit 0..60, height 0..10"))
|
|
return
|
|
}
|
|
x.Source = "local"
|
|
if err := a.store.PutSwitch(r.Context(), x); err != nil {
|
|
fail(w, 500, err)
|
|
return
|
|
}
|
|
writeJSON(w, 200, x)
|
|
}
|
|
|
|
// ---------------------------------------------------------------- NetBox
|
|
|
|
// syncSwitches reads the cables NetBox records on Rollcall's devices, then
|
|
// copies the place of every switch those and the machines' LLDP name that
|
|
// NetBox has as a device with a rack.
|
|
func (a *App) syncSwitches(ctx context.Context, nb *netBox) error {
|
|
ms, err := a.store.List(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
names := map[string]bool{}
|
|
cables, err := nb.cables(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("cables: %w", err)
|
|
}
|
|
if err := a.store.ReplaceNetBoxCables(ctx, cables); err != nil {
|
|
return err
|
|
}
|
|
for _, c := range cables {
|
|
names[c.Switch] = true
|
|
}
|
|
for _, m := range ms {
|
|
for _, l := range lldpOf(invOf(m)) {
|
|
names[l.Switch] = true
|
|
if short, _, ok := strings.Cut(l.Switch, "."); ok {
|
|
names[short] = true
|
|
}
|
|
}
|
|
}
|
|
if len(names) == 0 {
|
|
return nil
|
|
}
|
|
var list []string
|
|
for n := range names {
|
|
list = append(list, n)
|
|
}
|
|
sort.Strings(list)
|
|
var devs []obj
|
|
for len(list) > 0 { // 50 names per request: a large site would outgrow a URL
|
|
batch := list[:min(50, len(list))]
|
|
list = list[len(batch):]
|
|
got, err := nb.all(ctx, "dcim/devices/", url.Values{"name": batch})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
devs = append(devs, got...)
|
|
}
|
|
for _, x := range switchPlaces(devs) {
|
|
if err := a.store.PutSwitch(ctx, x); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// switchPlaces: the devices that stand in a rack, as switches
|
|
func switchPlaces(devs []obj) []Switch {
|
|
var out []Switch
|
|
for _, d := range devs {
|
|
rack := str(dig(d, "rack", "name"))
|
|
if rack == "" {
|
|
continue
|
|
}
|
|
x := Switch{Name: str(d["name"]), Building: str(dig(d, "site", "name")), Room: str(dig(d, "location", "name")), Rack: rack,
|
|
Unit: int(num(d["position"])), Height: 1, Model: str(dig(d, "device_type", "model")), Source: "netbox"}
|
|
if role := strings.ToLower(str(dig(d, "role", "name")) + " " + str(dig(d, "role", "slug"))); strings.Contains(role, "oob") || strings.Contains(role, "out-of-band") || strings.Contains(role, "mgmt") || strings.Contains(role, "management") {
|
|
x.Role = "oob"
|
|
}
|
|
out = append(out, x)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// bmcName: an interface NetBox models for the BMC (iDRAC, iLO, XCC, IMM, IPMI, BMC, OOB, mgmt)
|
|
var bmcName = regexp.MustCompile(`(?i)^(idrac|ilo|xcc|imm|ipmi|bmc|oob|mgmt|management)`)
|
|
|
|
// cables: the cables on Rollcall's devices (tag and machine field) to
|
|
// another device's interface, through patch panels (NetBox's connected
|
|
// endpoint), as machine port → switch port; an interface
|
|
// marked management-only or named like a BMC is the machine's "bmc".
|
|
func (n *netBox) cables(ctx context.Context) ([]Cable, error) {
|
|
devs, err := n.all(ctx, "dcim/devices/", url.Values{"tag": {n.tag}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
machineOf := map[int64]string{}
|
|
var ids []string
|
|
for _, d := range devs {
|
|
if id := str(dig(d, "custom_fields", n.field)); id != "" && id != "-" {
|
|
machineOf[nbID(d)] = id
|
|
ids = append(ids, fmt.Sprint(nbID(d)))
|
|
}
|
|
}
|
|
var out []Cable
|
|
for len(ids) > 0 { // a page of devices per request
|
|
batch := ids[:min(50, len(ids))]
|
|
ids = ids[len(batch):]
|
|
ifs, err := n.all(ctx, "dcim/interfaces/", url.Values{"device_id": batch, "cabled": {"true"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, f := range ifs {
|
|
m := machineOf[nbRef(f, "device")]
|
|
peers, kind := f["connected_endpoints"], str(f["connected_endpoints_type"]) // the far end, through patch panels
|
|
if kind != "dcim.interface" {
|
|
peers, kind = f["link_peers"], str(f["link_peers_type"])
|
|
}
|
|
for _, p := range toList(peers) {
|
|
po := asObj(p)
|
|
sw := str(dig(po, "device", "name"))
|
|
if m == "" || sw == "" || kind != "dcim.interface" {
|
|
continue
|
|
}
|
|
port := str(f["name"])
|
|
if f["mgmt_only"] == true || bmcName.MatchString(port) {
|
|
port = "bmc"
|
|
}
|
|
out = append(out, Cable{Machine: m, Port: port, Switch: sw, SwitchPort: str(po["name"]), Source: "netbox"})
|
|
}
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|