Add the hostname resolver strategy. It connects by the NetBox name instead of an IP, with resolver.hostname_domain appended to names without a dot. Its position in the chain decides whether it is always used or only as a fallback (e.g. [primary_ip, hostname]); names that are not valid DNS hostnames fall through to the next strategy. Add static hosts under hosts: for machines that are not in NetBox, with address, user, port, tags, and their own SSH options. They are checked before NetBox (by name or shortcut), never expire, survive cache refresh and clear, and work without any NetBox configured. The cache overlays them after every load and stores only their last use, so they appear in completion, aliases, the TUI and its recent list. SSH options merge global, static host, then rules; rules can match kind: static. Manage them with netssh hosts list|add|remove and in netssh configure, which can now also continue without NetBox. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package resolver
|
|
|
|
import (
|
|
"context"
|
|
"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) {
|
|
for _, s := range c.strategies {
|
|
ip, err := s.Resolve(ctx, entry, client)
|
|
if err == nil {
|
|
return ip, nil
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
}
|