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-04-26 05:29:35 +00:00
|
|
|
client.NetBoxAPI // API of NetBox
|
|
|
|
Token string // Token used to access the NetBox API
|
|
|
|
URL string // URL of this NetBox instance
|
2023-03-26 13:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNetBoxClient returns a client to NetBox at the given url,
|
|
|
|
// using the provided token
|
2023-04-24 12:46:41 +00:00
|
|
|
func NewNetBoxClient(token string, url string) NetBoxAPI {
|
2023-03-26 13:05:58 +00:00
|
|
|
// 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-04-26 05:29:35 +00:00
|
|
|
NetBoxAPI: *n,
|
|
|
|
Token: token,
|
|
|
|
URL: url,
|
2023-03-26 13:05:58 +00:00
|
|
|
}
|
2023-04-24 12:46:41 +00:00
|
|
|
return nb
|
2023-03-26 13:05:58 +00:00
|
|
|
}
|