v1
Test / test (push) Successful in 3m36s
Release / release (push) Successful in 7m31s

This commit is contained in:
Sebastian Unterschütz committed 2026-11-17 19:00:44 +01:00
commit 8d4915543e
132 files changed
+20628

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
// Package notes stores private server notes independently of the host cache.
package notes
import (
"context"
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"strings"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/storage"
)
type Note struct {
Text, ETag string
Host netbox.HostEntry
}
type Service struct {
Dir, Namespace string
Client *netbox.Client
}
func New(namespace string, client *netbox.Client) *Service {
dir, err := os.UserConfigDir()
if err != nil {
dir = filepath.Join(os.Getenv("HOME"), ".config")
}
return &Service{Dir: filepath.Join(dir, "netssh", "notes"), Namespace: strings.TrimRight(namespace, "/"), Client: client}
}
func (s *Service) path(host netbox.HostEntry) string {
key := fmt.Sprintf("%s\x00%s\x00%s", s.Namespace, host.Kind, strings.ToLower(host.Name))
return filepath.Join(s.Dir, fmt.Sprintf("%x.txt", sha256.Sum256([]byte(key))))
}
func (s *Service) Read(ctx context.Context, host netbox.HostEntry, remote bool) (Note, error) {
note := Note{Host: host}
if remote {
if s.Client == nil {
return note, fmt.Errorf("NetBox is not configured")
}
if host.ID == 0 {
resolved, err := s.Client.FindHost(ctx, host.Name, host.Kind)
if err != nil {
return note, err
}
note.Host = resolved
}
var err error
note.Text, note.ETag, err = s.Client.GetComments(ctx, note.Host)
return note, err
}
path, err := s.localPath(host)
if err != nil {
return note, err
}
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
return note, nil
}
note.Text = string(data)
return note, err
}
func (s *Service) Write(ctx context.Context, note Note, remote bool) error {
if remote {
if s.Client == nil {
return fmt.Errorf("NetBox is not configured")
}
return s.Client.SetComments(ctx, note.Host, note.Text, note.ETag)
}
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
}
+84
View File
@@ -0,0 +1,84 @@
package notes
import (
"context"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
"os"
"testing"
)
func TestLocalNotesPersistAndStayIsolated(t *testing.T) {
ctx := context.Background()
s := &Service{Dir: t.TempDir(), Namespace: "https://netbox.example"}
host := netbox.HostEntry{Name: "web01", Kind: "device"}
n := Note{Host: host, Text: "Maintenance\nReady 🔧"}
if err := s.Write(ctx, n, false); err != nil {
t.Fatal(err)
}
reloaded := &Service{Dir: s.Dir, Namespace: s.Namespace}
got, err := reloaded.Read(ctx, host, false)
if err != nil || got.Text != n.Text {
t.Fatalf("round trip: %+v, %v", got, err)
}
info, err := os.Stat(s.path(host))
if err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0o600 {
t.Fatalf("permissions: %v", info.Mode())
}
host.Kind = "vm"
got, err = s.Read(ctx, host, false)
if err != nil || got.Text != "" {
t.Fatalf("kind isolation: %+v, %v", got, err)
}
host.Kind = "device"
reloaded.Namespace = "https://other.example"
got, err = reloaded.Read(ctx, host, false)
if err != nil || got.Text != "" {
t.Fatalf("instance isolation: %+v, %v", got, err)
}
}
func TestUnreadableNoteIsNotTreatedAsEmpty(t *testing.T) {
s := &Service{Dir: t.TempDir()}
host := netbox.HostEntry{Name: "web01", Kind: "vm"}
if err := os.Mkdir(s.path(host), 0o700); err != nil {
t.Fatal(err)
}
if _, err := s.Read(context.Background(), host, false); err == nil {
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")
}
}