2023-03-26 13:05:58 +00:00
|
|
|
// Package netboxapi provides custom functions for the default
|
|
|
|
// go-netbox package. It mostly handles pagination.
|
|
|
|
package netboxapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
transport "github.com/go-openapi/runtime/client"
|
|
|
|
"github.com/netbox-community/go-netbox/v3/netbox/client"
|
|
|
|
)
|
|
|
|
|
2023-03-26 15:24:27 +00:00
|
|
|
// NetBoxAPI is the type we use to add custom functions
|
2023-03-26 13:05:58 +00:00
|
|
|
type NetBoxAPI struct {
|
2023-03-26 15:34:41 +00:00
|
|
|
API client.NetBoxAPI
|
2023-03-26 13:05:58 +00:00
|
|
|
Token string
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNetBoxClient returns a client to NetBox at the given url,
|
|
|
|
// using the provided token
|
|
|
|
func NewNetBoxClient(token string, url string) (NetBoxAPI, error) {
|
|
|
|
// Construct the API basis and configure authentication
|
|
|
|
t := transport.New(url, client.DefaultBasePath, []string{"https"})
|
|
|
|
t.DefaultAuthentication = transport.APIKeyAuth(
|
|
|
|
"Authorization",
|
|
|
|
"header",
|
|
|
|
fmt.Sprintf("Token %v", token),
|
|
|
|
)
|
|
|
|
|
|
|
|
// create NetBox api variable
|
|
|
|
n := client.New(t, nil)
|
|
|
|
|
|
|
|
// create a new variable for our custom functions
|
|
|
|
nb := NetBoxAPI{
|
2023-03-26 15:34:41 +00:00
|
|
|
API: *n,
|
2023-03-26 13:05:58 +00:00
|
|
|
Token: token,
|
|
|
|
URL: url,
|
|
|
|
}
|
|
|
|
return nb, nil
|
|
|
|
}
|