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>
44 lines
1.5 KiB
Go
44 lines
1.5 KiB
Go
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 TestHostnameStrategy(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, domain, want string
|
|
}{
|
|
{"Web01.Example.com", "", "web01.example.com"},
|
|
{"web01", "example.com", "web01.example.com"},
|
|
{"web01", ".example.com.", "web01.example.com"},
|
|
{"web01.lab", "example.com", "web01.lab"}, // already qualified
|
|
} {
|
|
got, err := (&HostnameStrategy{domain: tc.domain}).Resolve(context.Background(), &netbox.HostEntry{Name: tc.name}, nil)
|
|
if err != nil || got != tc.want {
|
|
t.Errorf("%q + %q: got %q, %v; want %q", tc.name, tc.domain, got, err, tc.want)
|
|
}
|
|
}
|
|
for _, name := range []string{"Core Switch 1", "-x", "a..b", "web_01"} {
|
|
if _, err := (&HostnameStrategy{}).Resolve(context.Background(), &netbox.HostEntry{Name: name}, nil); err != ErrNoIP {
|
|
t.Errorf("%q: got %v, want ErrNoIP", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHostnameAsFallback(t *testing.T) {
|
|
chain, err := New(config.ResolverConfig{Strategies: []string{"primary_ip", "hostname"}, HostnameDomain: "example.com"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ip, _ := chain.Resolve(context.Background(), &netbox.HostEntry{Name: "web01", PrimaryIP4: "192.0.2.1"}, nil); ip != "192.0.2.1" {
|
|
t.Fatalf("primary IP first: %q", ip)
|
|
}
|
|
if ip, _ := chain.Resolve(context.Background(), &netbox.HostEntry{Name: "web02"}, nil); ip != "web02.example.com" {
|
|
t.Fatalf("hostname fallback: %q", ip)
|
|
}
|
|
}
|