Skip to content

Commit

Permalink
netbox: Support arbitrary tag definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
the-maldridge committed Jan 17, 2023
1 parent 43a771e commit 751b815
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ in the environment:
global data.
* `NETBOX_TOKEN` - Token with suitable permissions to read netbox
data.
* `NETBOX_TAG` - Only service hosts that have a specific tag.
* `NETBOX_URL` - URL to the netbox server.
* `DNSMASQ_TEMPLATE` - A go template expression for the dhcp-hosts
file. Defaults to a suitable configuration for IPv4. The default
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ func main() {
os.Exit(1)
}

site := os.Getenv("NETBOX_SITE")
nb, err := netbox.NewClient(netbox.WithNetBoxURL(netboxURL), netbox.WithToken(token))
if err != nil {
log.Println("Error initializing client:", err)
os.Exit(1)
}

devices, err := nb.ListDevices(site)
site := os.Getenv("NETBOX_SITE")
tag := os.Getenv("NETBOX_TAG")
devices, err := nb.ListDevices(site, tag)
if err != nil {
log.Println("Error listing devices:", err)
os.Exit(1)
Expand Down
7 changes: 5 additions & 2 deletions netbox/netbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ func NewClient(opts ...Option) (*Client, error) {
}

// ListDevices searches for netboxes that match the given options.
func (nb *Client) ListDevices(site string) ([]Device, error) {
func (nb *Client) ListDevices(site, tag string) ([]Device, error) {
queryURL := *nb.baseURL
queryURL.Path = "/api/dcim/devices/"

queryVals := url.Values{}
queryVals.Add("tag", "pxe-enable")
queryVals.Add("has_primary_ip", "yes")

if tag != "" {
queryVals.Add("tag", tag)
}

if site != "" {
queryVals.Add("site", site)
}
Expand Down

0 comments on commit 751b815

Please sign in to comment.