195 lines
5.0 KiB
Go
195 lines
5.0 KiB
Go
package netbox
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
type Client struct {
|
|
baseURL string
|
|
token string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func NewClient(baseURL, token string) *Client {
|
|
return &Client{
|
|
baseURL: strings.TrimRight(baseURL, "/"),
|
|
token: token,
|
|
httpClient: &http.Client{},
|
|
}
|
|
}
|
|
|
|
// Search queries devices and VMs in parallel and merges the results.
|
|
func (c *Client) Search(ctx context.Context, query string) ([]HostEntry, error) {
|
|
var (
|
|
mu sync.Mutex
|
|
results []HostEntry
|
|
errs []error
|
|
wg sync.WaitGroup
|
|
)
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
devices, err := c.searchDevices(ctx, query)
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if err != nil {
|
|
errs = append(errs, fmt.Errorf("devices: %w", err))
|
|
return
|
|
}
|
|
results = append(results, devices...)
|
|
}()
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
vms, err := c.searchVMs(ctx, query)
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if err != nil {
|
|
errs = append(errs, fmt.Errorf("vms: %w", err))
|
|
return
|
|
}
|
|
results = append(results, vms...)
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
if len(errs) == 2 {
|
|
return nil, fmt.Errorf("netbox search failed: %v; %v", errs[0], errs[1])
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
// GetIPs returns all IP addresses assigned to a host, used by resolver strategies
|
|
// that need more than just the primary IP.
|
|
func (c *Client) GetIPs(ctx context.Context, entry HostEntry) ([]string, error) {
|
|
var apiURL string
|
|
switch entry.Kind {
|
|
case "device":
|
|
apiURL = fmt.Sprintf("%s/api/ipam/ip-addresses/?device_id=%d&limit=100", c.baseURL, entry.ID)
|
|
case "vm":
|
|
apiURL = fmt.Sprintf("%s/api/ipam/ip-addresses/?virtual_machine_id=%d&limit=100", c.baseURL, entry.ID)
|
|
default:
|
|
return nil, fmt.Errorf("unknown host kind: %q", entry.Kind)
|
|
}
|
|
|
|
var resp netboxIPListResponse
|
|
if err := c.get(ctx, apiURL, &resp); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ips := make([]string, 0, len(resp.Results))
|
|
for _, r := range resp.Results {
|
|
ips = append(ips, stripPrefix(r.Address))
|
|
}
|
|
return ips, nil
|
|
}
|
|
|
|
// GetIPsWithFilter calls /api/ipam/ip-addresses/ with arbitrary filter query parameters.
|
|
func (c *Client) GetIPsWithFilter(ctx context.Context, filterParams string) ([]string, error) {
|
|
apiURL := fmt.Sprintf("%s/api/ipam/ip-addresses/?%s&limit=100", c.baseURL, filterParams)
|
|
var resp netboxIPListResponse
|
|
if err := c.get(ctx, apiURL, &resp); err != nil {
|
|
return nil, err
|
|
}
|
|
ips := make([]string, 0, len(resp.Results))
|
|
for _, r := range resp.Results {
|
|
ips = append(ips, stripPrefix(r.Address))
|
|
}
|
|
return ips, nil
|
|
}
|
|
|
|
func (c *Client) searchDevices(ctx context.Context, query string) ([]HostEntry, error) {
|
|
apiURL := fmt.Sprintf("%s/api/dcim/devices/?name__ic=%s&limit=50", c.baseURL, url.QueryEscape(query))
|
|
var resp netboxListResponse[netboxDevice]
|
|
if err := c.get(ctx, apiURL, &resp); err != nil {
|
|
return nil, err
|
|
}
|
|
entries := make([]HostEntry, 0, len(resp.Results))
|
|
for _, d := range resp.Results {
|
|
entries = append(entries, deviceToEntry(d))
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
func (c *Client) searchVMs(ctx context.Context, query string) ([]HostEntry, error) {
|
|
apiURL := fmt.Sprintf("%s/api/virtualization/virtual-machines/?name__ic=%s&limit=50", c.baseURL, url.QueryEscape(query))
|
|
var resp netboxListResponse[netboxVM]
|
|
if err := c.get(ctx, apiURL, &resp); err != nil {
|
|
return nil, err
|
|
}
|
|
entries := make([]HostEntry, 0, len(resp.Results))
|
|
for _, v := range resp.Results {
|
|
entries = append(entries, vmToEntry(v))
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
func (c *Client) get(ctx context.Context, apiURL string, out any) error {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("creating request: %w", err)
|
|
}
|
|
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)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
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 nil
|
|
}
|
|
|
|
func deviceToEntry(d netboxDevice) HostEntry {
|
|
e := HostEntry{ID: d.ID, Name: d.Name, Kind: "device"}
|
|
if d.PrimaryIP4 != nil {
|
|
e.PrimaryIP4 = stripPrefix(d.PrimaryIP4.Address)
|
|
}
|
|
if d.PrimaryIP6 != nil {
|
|
e.PrimaryIP6 = stripPrefix(d.PrimaryIP6.Address)
|
|
}
|
|
for _, t := range d.Tags {
|
|
e.Tags = append(e.Tags, t.Name)
|
|
}
|
|
return e
|
|
}
|
|
|
|
func vmToEntry(v netboxVM) HostEntry {
|
|
e := HostEntry{ID: v.ID, Name: v.Name, Kind: "vm"}
|
|
if v.PrimaryIP4 != nil {
|
|
e.PrimaryIP4 = stripPrefix(v.PrimaryIP4.Address)
|
|
}
|
|
if v.PrimaryIP6 != nil {
|
|
e.PrimaryIP6 = stripPrefix(v.PrimaryIP6.Address)
|
|
}
|
|
for _, t := range v.Tags {
|
|
e.Tags = append(e.Tags, t.Name)
|
|
}
|
|
return e
|
|
}
|
|
|
|
// stripPrefix removes the CIDR prefix length from a NetBox IP (e.g. "10.0.1.5/24" → "10.0.1.5").
|
|
func stripPrefix(cidr string) string {
|
|
if idx := strings.Index(cidr, "/"); idx != -1 {
|
|
return cidr[:idx]
|
|
}
|
|
return cidr
|
|
}
|