Files
ssh-netbox-wrapper/internal/resolver/chain.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

112 lines
3.4 KiB
Go

package resolver
import (
"context"
"errors"
"fmt"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
)
// Chain tries each strategy in order until one returns an IP.
type Chain struct {
strategies []Strategy
}
// New builds a Chain from the strategy names listed in the resolver config.
func New(cfg config.ResolverConfig) (*Chain, error) {
var strategies []Strategy
for _, name := range cfg.Strategies {
s, err := newStrategy(name, cfg)
if err != nil {
return nil, fmt.Errorf("resolver strategy %q: %w", name, err)
}
strategies = append(strategies, s)
}
return &Chain{strategies: strategies}, nil
}
func (c *Chain) Resolve(ctx context.Context, entry *netbox.HostEntry, client *netbox.Client) (string, error) {
address, _, err := c.ResolveWithStrategy(ctx, entry, client)
return address, err
}
func (c *Chain) ResolveWithStrategy(ctx context.Context, entry *netbox.HostEntry, client *netbox.Client) (string, string, error) {
for _, s := range c.strategies {
ip, err := s.Resolve(ctx, entry, client)
if err == nil {
return ip, s.Name(), nil
}
// Only "this strategy has no address" moves on. After an API failure
// the next strategy would connect to (and cache) a different address
// than the configured order asks for.
if !errors.Is(err, ErrNoIP) {
return "", "", fmt.Errorf("resolver strategy %q for %q: %w", s.Name(), entry.Name, err)
}
}
return "", "", fmt.Errorf("no strategy resolved an IP for %q", entry.Name)
}
func newStrategy(name string, cfg config.ResolverConfig) (Strategy, error) {
switch name {
case "primary_ip":
return &PrimaryIPStrategy{}, nil
case "management_subnet":
s, err := NewManagementSubnetStrategy(cfg.ManagementSubnets)
if err != nil {
return nil, err
}
return s, nil
case "hostname":
return &HostnameStrategy{domain: cfg.HostnameDomain}, nil
case "interface_name":
if cfg.InterfaceName == "" {
return nil, fmt.Errorf("interface_name strategy requires resolver.interface_name to be set")
}
return &InterfaceNameStrategy{name: cfg.InterfaceName}, nil
default:
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
}