feat: NetBox insights, host tools and configurable TUI
Test / test (pull_request) Successful in 4m15s
Test / test (pull_request) Successful in 4m15s
- info/open/whois: NetBox details (site, nested locations, rack units, role, platform, tenant, IPs per interface), web page, reverse IP lookup - cache status, site, role, platform, tenant, location chain and rack; ask before connecting to hosts in ssh.warn_status (--force skips) - ssh rules and command restrictions match site/role/platform/tenant/status - tui.columns selects what the TUI shows; optional TCP reachability column; detail view (Ctrl+L), NetBox page (Ctrl+G), status/location filters - resolver.fallback tries other strategies' addresses when unreachable - copy-id, known-hosts reset and address-change notices - export ansible (YAML/INI) and export ssh-config - token: keyring stores NetBox tokens in the system keyring - fix stale menu number in testenv configure test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e5bde9c61f
commit
1660bb3098
39 files changed
+4868
-96
No files matched your search
@@ -0,0 +1,101 @@
|
||||
package netbox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// locationServer serves a site with Building A › Floor 2 › Room 2.04 and two
|
||||
// racked devices in Room 2.04, plus a location loop 90 ↔ 91.
|
||||
func locationServer(t *testing.T, calls *int32) *httptest.Server {
|
||||
t.Helper()
|
||||
locations := map[string]string{
|
||||
"1": `{"id":1,"name":"Building A","slug":"building-a","parent":null}`,
|
||||
"2": `{"id":2,"name":"Floor 2","slug":"floor-2","parent":{"id":1}}`,
|
||||
"3": `{"id":3,"name":"Room 2.04","slug":"room-2-04","parent":{"id":2}}`,
|
||||
"90": `{"id":90,"name":"Loop A","slug":"loop-a","parent":{"id":91}}`,
|
||||
"91": `{"id":91,"name":"Loop B","slug":"loop-b","parent":{"id":90}}`,
|
||||
}
|
||||
device := func(id int, name string, location int, position string) string {
|
||||
return fmt.Sprintf(`{"id":%d,"name":%q,"site":{"name":"FSN1","slug":"fsn1"},"location":{"id":%d,"name":"x","slug":"x"},
|
||||
"rack":{"name":"R12"},"position":%s,"face":{"value":"front"},"device_type":{"id":9,"model":"R640"}}`, id, name, location, position)
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case strings.HasPrefix(r.URL.Path, "/api/dcim/locations/"):
|
||||
atomic.AddInt32(calls, 1)
|
||||
id := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/dcim/locations/"), "/")
|
||||
if body, ok := locations[id]; ok {
|
||||
fmt.Fprint(w, body)
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
case r.URL.Path == "/api/dcim/devices/":
|
||||
fmt.Fprintf(w, `{"count":3,"results":[%s,%s,%s]}`, device(1, "srv1", 3, "20.0"), device(2, "srv2", 3, "22"), device(3, "loopy", 90, "null"))
|
||||
case r.URL.Path == "/api/dcim/devices/1/":
|
||||
fmt.Fprint(w, device(1, "srv1", 3, "20.0"))
|
||||
case r.URL.Path == "/api/dcim/device-types/9/":
|
||||
fmt.Fprint(w, `{"id":9,"u_height":2.0}`)
|
||||
case r.URL.Path == "/api/virtualization/virtual-machines/", r.URL.Path == "/api/ipam/ip-addresses/":
|
||||
fmt.Fprint(w, `{"count":0,"results":[]}`)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
func TestSearchAllResolvesNestedLocations(t *testing.T) {
|
||||
var calls int32
|
||||
srv := locationServer(t, &calls)
|
||||
entries, err := NewClient(srv.URL, "t", 0).SearchAll(context.Background(), "", SearchOptions{Kind: "device"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
byName := map[string]HostEntry{}
|
||||
for _, e := range entries {
|
||||
byName[e.Name] = e
|
||||
}
|
||||
srv1 := byName["srv1"]
|
||||
if got := srv1.LocationPath(); got != "FSN1 › Building A › Floor 2 › Room 2.04 › R12 › U20" {
|
||||
t.Fatalf("path %q", got)
|
||||
}
|
||||
if srv1.Location[0].Slug != "building-a" || srv1.Location[2].Slug != "room-2-04" {
|
||||
t.Fatalf("chain order: %+v", srv1.Location)
|
||||
}
|
||||
// Room, floor and building are fetched once for both devices, the loop
|
||||
// stops at the depth limit instead of hanging.
|
||||
if n := atomic.LoadInt32(&calls); n != 5 {
|
||||
t.Fatalf("location requests: %d", n)
|
||||
}
|
||||
if len(byName["loopy"].Location) != maxLocationDepth || byName["loopy"].Position != "" {
|
||||
t.Fatalf("loop not bounded: %d", len(byName["loopy"].Location))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetailsShowRackUnits(t *testing.T) {
|
||||
var calls int32
|
||||
srv := locationServer(t, &calls)
|
||||
d, err := NewClient(srv.URL, "t", 0).GetDetails(context.Background(), HostEntry{ID: 1, Name: "srv1", Kind: "device"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if d.Rack != "R12" || d.Position != "20" || d.HeightU != "2" || d.RackUnits() != "U20-21 (2U, front)" {
|
||||
t.Fatalf("rack: %q %q %q %q", d.Rack, d.Position, d.HeightU, d.RackUnits())
|
||||
}
|
||||
if len(d.Location) != 3 {
|
||||
t.Fatalf("details lack location chain: %+v", d.Location)
|
||||
}
|
||||
if (HostDetails{HostEntry: HostEntry{Position: "7"}, HeightU: "1"}).RackUnits() != "U7 (1U)" {
|
||||
t.Fatal("single unit")
|
||||
}
|
||||
if (HostDetails{}).RackUnits() != "" {
|
||||
t.Fatal("unracked device should have no units")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user