Add local and NetBox server notes and improve reliability
Release / release (push) Successful in 6m52s

This commit is contained in:
Sebastian Unterschütz committed 2026-10-27 17:51:59 +01:00
1 parent 2852dff2c4
commit 12cc64fdd9
17 files changed
+848 -83

No files matched your search

+99 -17
View File
@@ -1,13 +1,17 @@
package netbox
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
type Client struct {
@@ -27,13 +31,16 @@ func NewClient(baseURL, token string, tokenVersion int) *Client {
baseURL: strings.TrimRight(baseURL, "/"),
token: token,
tokenVersion: tokenVersion,
httpClient: &http.Client{},
httpClient: &http.Client{Timeout: 15 * time.Second},
}
}
// Search queries up to 50 devices and VMs in parallel and merges the results.
// Use SearchOptions to restrict by kind or tag.
func (c *Client) Search(ctx context.Context, query string, opts SearchOptions) ([]HostEntry, error) {
if opts.Kind != "" && opts.Kind != "device" && opts.Kind != "vm" {
return nil, fmt.Errorf("invalid host kind %q", opts.Kind)
}
var (
mu sync.Mutex
results []HostEntry
@@ -88,16 +95,17 @@ func (c *Client) Search(ctx context.Context, query string, opts SearchOptions) (
// SearchAll paginates through all matching devices and VMs, fetching every page.
// Intended for cache refresh; use Search for interactive queries.
func (c *Client) SearchAll(ctx context.Context, query string, opts SearchOptions) ([]HostEntry, error) {
if opts.Kind != "" && opts.Kind != "device" && opts.Kind != "vm" {
return nil, fmt.Errorf("invalid host kind %q", opts.Kind)
}
var (
mu sync.Mutex
results []HostEntry
errs []error
wg sync.WaitGroup
started int
)
if opts.Kind != "vm" {
started++
wg.Add(1)
go func() {
defer wg.Done()
@@ -113,7 +121,6 @@ func (c *Client) SearchAll(ctx context.Context, query string, opts SearchOptions
}
if opts.Kind != "device" {
started++
wg.Add(1)
go func() {
defer wg.Done()
@@ -130,11 +137,8 @@ func (c *Client) SearchAll(ctx context.Context, query string, opts SearchOptions
wg.Wait()
if len(errs) == started {
if started == 1 {
return nil, errs[0]
}
return nil, fmt.Errorf("netbox search failed: %v; %v", errs[0], errs[1])
if len(errs) > 0 {
return nil, errors.Join(errs...)
}
return results, nil
}
@@ -265,16 +269,31 @@ func TokenVersion(token string) int {
}
func (c *Client) get(ctx context.Context, apiURL string, out any) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
_, err := c.request(ctx, http.MethodGet, apiURL, nil, "", out)
return err
}
func (c *Client) request(ctx context.Context, method, apiURL string, body io.Reader, etag string, out any) (string, error) {
req, err := http.NewRequestWithContext(ctx, method, apiURL, body)
if err != nil {
return fmt.Errorf("creating request: %w", err)
return "", fmt.Errorf("creating request: %w", err)
}
scheme := "Token "
if c.tokenVersion == 2 {
scheme = "Bearer "
}
req.Header.Set("Authorization", scheme+c.token)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
if etag != "" {
req.Header.Set("If-Match", etag)
}
req.Header.Set("Authorization", "Token "+c.token)
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("request to %s: %w", apiURL, err)
return "", fmt.Errorf("request to %s: %w", apiURL, err)
}
defer resp.Body.Close()
@@ -283,17 +302,17 @@ func (c *Client) get(ctx context.Context, apiURL string, out any) error {
if c.tokenVersion == 1 {
hint += " — legacy v1 token detected, consider upgrading to a v2 token (starts with nbt_)"
}
return fmt.Errorf("%s: %s", apiURL, hint)
return "", fmt.Errorf("%s: %s", apiURL, hint)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("netbox returned %d for %s", resp.StatusCode, apiURL)
return "", fmt.Errorf("netbox returned %d for %s", resp.StatusCode, apiURL)
}
if err := json.NewDecoder(resp.Body).Decode(out); err != nil {
return fmt.Errorf("decoding response: %w", err)
return "", fmt.Errorf("decoding response: %w", err)
}
return nil
return resp.Header.Get("ETag"), nil
}
func deviceToEntry(d netboxDevice) HostEntry {
@@ -331,3 +350,66 @@ func stripPrefix(cidr string) string {
}
return cidr
}
// FindHost refuses ambiguous names so writes cannot target an arbitrary server.
func (c *Client) FindHost(ctx context.Context, name, kind string) (HostEntry, error) {
if kind != "" && kind != "device" && kind != "vm" {
return HostEntry{}, fmt.Errorf("invalid kind %q", kind)
}
entries, err := c.SearchAll(ctx, name, SearchOptions{Kind: kind})
if err != nil {
return HostEntry{}, err
}
var matches []HostEntry
for _, e := range entries {
if strings.EqualFold(e.Name, name) {
matches = append(matches, e)
}
}
if len(matches) != 1 {
return HostEntry{}, fmt.Errorf("host %q: found %d exact matches; use --kind or a unique hostname", name, len(matches))
}
return matches[0], nil
}
func (c *Client) hostURL(host HostEntry) (string, error) {
var path string
switch host.Kind {
case "device":
path = "dcim/devices"
case "vm":
path = "virtualization/virtual-machines"
default:
return "", fmt.Errorf("invalid host kind %q", host.Kind)
}
if host.ID <= 0 {
return "", fmt.Errorf("invalid host ID")
}
return fmt.Sprintf("%s/api/%s/%d/", c.baseURL, path, host.ID), nil
}
func (c *Client) GetComments(ctx context.Context, host HostEntry) (string, string, error) {
u, err := c.hostURL(host)
if err != nil {
return "", "", err
}
var out struct {
Comments string `json:"comments"`
}
etag, err := c.request(ctx, http.MethodGet, u, nil, "", &out)
return out.Comments, etag, err
}
func (c *Client) SetComments(ctx context.Context, host HostEntry, comments, etag string) error {
u, err := c.hostURL(host)
if err != nil {
return err
}
data, err := json.Marshal(map[string]string{"comments": comments})
if err != nil {
return err
}
var out json.RawMessage
_, err = c.request(ctx, http.MethodPatch, u, bytes.NewReader(data), etag, &out)
return err
}