Files
ssh-netbox-wrapper/internal/netbox/models.go
T
Sebastian UnterschützandClaude Opus 5.5 1660bb3098
Test / test (pull_request) Successful in 4m15s
feat: NetBox insights, host tools and configurable TUI
- 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>
2026-11-18 12:33:59 +01:00

173 lines
4.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package netbox
import (
"encoding/json"
"strings"
"time"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
)
// SearchOptions filters a Search or SearchAll query.
// Zero value means no filtering (return all kinds, no tag filter).
type SearchOptions struct {
Tag string // filter by tag slug; empty = no filter
Kind string // "device" | "vm" | "" (both)
// Cluster filters SearchAll by virtualization cluster name (case-insensitive).
Cluster string
}
// HostEntry is a unified model for both devices and virtual machines from NetBox.
type HostEntry struct {
Cached bool
CachedAt time.Time
Static bool
Source string // named NetBox instance; empty for the legacy single-instance config
ID int
Name string
Kind string // "device" | "vm"
PrimaryIP4 string // e.g. "10.0.1.5" (prefix length stripped)
PrimaryIP6 string
Tags []string
Cluster string // virtualization cluster name, if assigned
Status string // NetBox status value, e.g. "active"
Site config.Ref
Role config.Ref
Platform config.Ref
Tenant config.Ref
// Location is the chain of nested NetBox locations from the outermost
// (directly below the site) to the device's own location.
Location []config.Ref
Rack string
Position string // lowest rack unit, e.g. "20"; empty when not racked
locationID int // the device's own location, before the chain is resolved
}
// LocationPath renders site › locations › rack › U position.
func (h HostEntry) LocationPath() string {
return config.LocationPath(h.Site, h.Location, h.Rack, h.Position)
}
// netboxRef is a nested NetBox object with a name and slug.
type netboxRef struct {
Name string `json:"name"`
Slug string `json:"slug"`
}
func (r *netboxRef) ref() config.Ref {
if r == nil {
return config.Ref{}
}
return config.Ref{Name: r.Name, Slug: r.Slug}
}
// netboxStatus is a NetBox choice field such as status.
type netboxStatus struct {
Value string `json:"value"`
Label string `json:"label"`
}
func (s *netboxStatus) value() string {
if s == nil {
return ""
}
return s.Value
}
// netboxCluster is the nested cluster of a device or virtual machine.
type netboxCluster struct {
ID int `json:"id"`
Name string `json:"name"`
}
// netboxIP represents an IP address as returned by the NetBox API.
type netboxIP struct {
Address string `json:"address"` // CIDR notation, e.g. "10.0.1.5/24"
}
// netboxDevice matches the relevant fields of the NetBox /dcim/devices/ response.
type netboxDevice struct {
ID int `json:"id"`
Name string `json:"name"`
Tags []struct {
Name string `json:"name"`
} `json:"tags"`
PrimaryIP4 *netboxIP `json:"primary_ip4"`
PrimaryIP6 *netboxIP `json:"primary_ip6"`
Cluster *netboxCluster `json:"cluster"`
Status *netboxStatus `json:"status"`
Site *netboxRef `json:"site"`
Role *netboxRef `json:"role"`
DeviceRole *netboxRef `json:"device_role"` // NetBox < 3.6
Platform *netboxRef `json:"platform"`
Tenant *netboxRef `json:"tenant"`
Location *struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"location"`
Rack *struct {
Name string `json:"name"`
} `json:"rack"`
Position json.Number `json:"position"`
}
// netboxVM matches the relevant fields of the NetBox /virtualization/virtual-machines/ response.
type netboxVM struct {
ID int `json:"id"`
Name string `json:"name"`
Tags []struct {
Name string `json:"name"`
} `json:"tags"`
PrimaryIP4 *netboxIP `json:"primary_ip4"`
PrimaryIP6 *netboxIP `json:"primary_ip6"`
Cluster *netboxCluster `json:"cluster"`
Status *netboxStatus `json:"status"`
Site *netboxRef `json:"site"`
Role *netboxRef `json:"role"`
DeviceRole *netboxRef `json:"device_role"` // NetBox < 3.6
Platform *netboxRef `json:"platform"`
Tenant *netboxRef `json:"tenant"`
Location *struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"location"`
Rack *struct {
Name string `json:"name"`
} `json:"rack"`
Position json.Number `json:"position"`
}
type netboxListResponse[T any] struct {
Count int `json:"count"`
Results []T `json:"results"`
}
type netboxIPListResponse struct {
Count int `json:"count"`
Results []struct {
Address string `json:"address"`
Interface *struct {
Name string `json:"name"`
} `json:"assigned_object"`
} `json:"results"`
}
// Target returns an unambiguous selector for cache, completion, and SSH.
func (h HostEntry) Target() string {
if h.Source == "" {
return h.Name
}
return h.Source + "/" + h.Name
}
// SplitTarget separates an optional instance prefix from a hostname.
func SplitTarget(target string) (source, name string) {
if source, name, ok := strings.Cut(target, "/"); ok {
return strings.ToLower(source), name
}
return "", target
}