v1
This commit is contained in:
commit
8d4915543e
132 files changed
+20628
No files matched your search
@@ -0,0 +1,90 @@
|
||||
package netbox
|
||||
|
||||
import "strings"
|
||||
|
||||
// SearchOptions filters a Search or SearchAll query.
|
||||
// Zero value means no filtering (return all kinds, no tag filter).
|
||||
type SearchOptions struct {
|
||||
Tag string // filter by tag slug; empty = no filter
|
||||
Kind string // "device" | "vm" | "" (both)
|
||||
// Cluster filters SearchAll by virtualization cluster name (case-insensitive).
|
||||
Cluster string
|
||||
}
|
||||
|
||||
// HostEntry is a unified model for both devices and virtual machines from NetBox.
|
||||
type HostEntry struct {
|
||||
Source string // named NetBox instance; empty for the legacy single-instance config
|
||||
ID int
|
||||
Name string
|
||||
Kind string // "device" | "vm"
|
||||
PrimaryIP4 string // e.g. "10.0.1.5" (prefix length stripped)
|
||||
PrimaryIP6 string
|
||||
Tags []string
|
||||
Cluster string // virtualization cluster name, if assigned
|
||||
}
|
||||
|
||||
// netboxCluster is the nested cluster of a device or virtual machine.
|
||||
type netboxCluster struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// netboxIP represents an IP address as returned by the NetBox API.
|
||||
type netboxIP struct {
|
||||
Address string `json:"address"` // CIDR notation, e.g. "10.0.1.5/24"
|
||||
}
|
||||
|
||||
// netboxDevice matches the relevant fields of the NetBox /dcim/devices/ response.
|
||||
type netboxDevice struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Tags []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"tags"`
|
||||
PrimaryIP4 *netboxIP `json:"primary_ip4"`
|
||||
PrimaryIP6 *netboxIP `json:"primary_ip6"`
|
||||
Cluster *netboxCluster `json:"cluster"`
|
||||
}
|
||||
|
||||
// netboxVM matches the relevant fields of the NetBox /virtualization/virtual-machines/ response.
|
||||
type netboxVM struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Tags []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"tags"`
|
||||
PrimaryIP4 *netboxIP `json:"primary_ip4"`
|
||||
PrimaryIP6 *netboxIP `json:"primary_ip6"`
|
||||
Cluster *netboxCluster `json:"cluster"`
|
||||
}
|
||||
|
||||
type netboxListResponse[T any] struct {
|
||||
Count int `json:"count"`
|
||||
Results []T `json:"results"`
|
||||
}
|
||||
|
||||
type netboxIPListResponse struct {
|
||||
Count int `json:"count"`
|
||||
Results []struct {
|
||||
Address string `json:"address"`
|
||||
Interface *struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"assigned_object"`
|
||||
} `json:"results"`
|
||||
}
|
||||
|
||||
// Target returns an unambiguous selector for cache, completion, and SSH.
|
||||
func (h HostEntry) Target() string {
|
||||
if h.Source == "" {
|
||||
return h.Name
|
||||
}
|
||||
return h.Source + "/" + h.Name
|
||||
}
|
||||
|
||||
// SplitTarget separates an optional instance prefix from a hostname.
|
||||
func SplitTarget(target string) (source, name string) {
|
||||
if source, name, ok := strings.Cut(target, "/"); ok {
|
||||
return strings.ToLower(source), name
|
||||
}
|
||||
return "", target
|
||||
}
|
||||
Reference in new issue
Block a user