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,30 @@
|
||||
package resolver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
|
||||
)
|
||||
|
||||
func TestCandidatesListsEveryStrategyOnce(t *testing.T) {
|
||||
chain, err := New(config.ResolverConfig{Strategies: []string{"primary_ip", "hostname"}, HostnameDomain: "example.com"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entry := &netbox.HostEntry{Name: "web", Kind: "vm", PrimaryIP4: "10.0.0.1", PrimaryIP6: "2001:db8::1"}
|
||||
got, err := chain.Candidates(context.Background(), entry, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []Candidate{{"10.0.0.1", "primary_ip"}, {"web.example.com", "hostname"}, {"2001:db8::1", "primary_ip"}}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("got %+v", got)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("got %+v, want %+v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,3 +69,43 @@ func newStrategy(name string, cfg config.ResolverConfig) (Strategy, error) {
|
||||
return nil, fmt.Errorf("unknown strategy %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
// Candidate is an address one strategy resolved for a host.
|
||||
type Candidate struct {
|
||||
Address string
|
||||
Strategy string
|
||||
}
|
||||
|
||||
// Candidates runs every strategy and returns their distinct addresses in the
|
||||
// configured order. Unlike Resolve, an API failure of one strategy does not
|
||||
// stop the others: candidates are only used as fallbacks for an address that
|
||||
// was already chosen by Resolve and turned out to be unreachable.
|
||||
func (c *Chain) Candidates(ctx context.Context, entry *netbox.HostEntry, client *netbox.Client) ([]Candidate, error) {
|
||||
var out []Candidate
|
||||
var errs []error
|
||||
seen := map[string]bool{}
|
||||
for _, s := range c.strategies {
|
||||
ip, err := s.Resolve(ctx, entry, client)
|
||||
if err != nil {
|
||||
if !errors.Is(err, ErrNoIP) {
|
||||
errs = append(errs, fmt.Errorf("%s: %w", s.Name(), err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !seen[ip] {
|
||||
seen[ip] = true
|
||||
out = append(out, Candidate{Address: ip, Strategy: s.Name()})
|
||||
}
|
||||
}
|
||||
// Both addresses of a dual-stack host are worth trying.
|
||||
for _, ip := range []string{entry.PrimaryIP4, entry.PrimaryIP6} {
|
||||
if ip != "" && !seen[ip] {
|
||||
seen[ip] = true
|
||||
out = append(out, Candidate{Address: ip, Strategy: "primary_ip"})
|
||||
}
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil, errors.Join(errs...)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in new issue
Block a user