feat: search multiple NetBox instances with isolated host identities
Release / release (push) Successful in 5m16s

This commit is contained in:
Sebastian Unterschütz committed 2026-10-27 23:14:42 +01:00
1 parent a2a6dd7591
commit 7f53b90266
26 files changed
+1299 -115

No files matched your search

+23 -2
View File
@@ -52,7 +52,11 @@ func (s *Service) Read(ctx context.Context, host netbox.HostEntry, remote bool)
note.Text, note.ETag, err = s.Client.GetComments(ctx, note.Host)
return note, err
}
data, err := os.ReadFile(s.path(host))
path, err := s.localPath(host)
if err != nil {
return note, err
}
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
return note, nil
}
@@ -67,5 +71,22 @@ func (s *Service) Write(ctx context.Context, note Note, remote bool) error {
}
return s.Client.SetComments(ctx, note.Host, note.Text, note.ETag)
}
return storage.WriteFile(s.path(note.Host), []byte(note.Text), 0o600)
path, err := s.localPath(note.Host)
if err != nil {
return err
}
return storage.WriteFile(path, []byte(note.Text), 0o600)
}
func (s *Service) localPath(host netbox.HostEntry) (string, error) {
if s.Client == nil {
return s.path(host), nil
}
host, namespace, err := s.Client.HostScope(host)
if err != nil {
return "", err
}
scoped := *s
scoped.Namespace = namespace
return scoped.path(host), nil
}
+32
View File
@@ -50,3 +50,35 @@ func TestUnreadableNoteIsNotTreatedAsEmpty(t *testing.T) {
t.Fatal("expected read error")
}
}
func TestLocalNotesSeparateInstancesAndSurviveRename(t *testing.T) {
ctx := context.Background()
client := netbox.NewMultiClient([]netbox.Source{{Name: "office", URL: "https://office.example"}, {Name: "lab", URL: "https://lab.example"}})
service := &Service{Dir: t.TempDir(), Client: client}
for _, source := range []string{"office", "lab"} {
note := Note{Host: netbox.HostEntry{Name: source + "/web", Kind: "device"}, Text: source + " private note"}
if err := service.Write(ctx, note, false); err != nil {
t.Fatal(err)
}
}
for _, source := range []string{"office", "lab"} {
note, err := service.Read(ctx, netbox.HostEntry{Name: source + "/web", Kind: "device"}, false)
if err != nil || note.Text != source+" private note" {
t.Fatalf("notes crossed instances: %+v, %v", note, err)
}
}
service.Client = netbox.NewMultiClient([]netbox.Source{{Name: "renamed", URL: "https://office.example"}})
note, err := service.Read(ctx, netbox.HostEntry{Name: "renamed/web", Kind: "device"}, false)
if err != nil || note.Text != "office private note" {
t.Fatalf("renaming source lost note: %+v, %v", note, err)
}
// This is also the namespace used before migrating a legacy single NetBox.
legacy := &Service{Dir: service.Dir, Namespace: "https://office.example"}
note, err = legacy.Read(ctx, netbox.HostEntry{Name: "web", Kind: "device"}, false)
if err != nil || note.Text != "office private note" {
t.Fatalf("legacy note namespace changed: %+v, %v", note, err)
}
if err := service.Write(ctx, Note{Host: netbox.HostEntry{Name: "web", Kind: "device"}}, false); err == nil {
t.Fatal("unqualified notes accepted for multiple sources")
}
}