added GetDevices function
This commit is contained in:
parent
88f9345cac
commit
97f1af7f6d
62
dcim.go
Normal file
62
dcim.go
Normal file
@ -0,0 +1,62 @@
|
||||
package netboxapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
e "git.stinnesbeck.com/nils/errorhandler"
|
||||
"github.com/netbox-community/go-netbox/v3/netbox/client/dcim"
|
||||
"github.com/netbox-community/go-netbox/v3/netbox/models"
|
||||
)
|
||||
|
||||
// GetDevices returns devices from the api nb filtered by the parameter list param
|
||||
func (nb NetBoxAPI) GetDevices(param dcim.DcimDevicesListParams) ([]*models.DeviceWithConfigContext, error) {
|
||||
// check if context was set, if not set background
|
||||
if param.Context == nil {
|
||||
param.Context = context.Background()
|
||||
}
|
||||
|
||||
var defaultOffset int64
|
||||
var defaultLimit int64 = 50
|
||||
|
||||
// set Offset if not set before
|
||||
if param.Limit == nil {
|
||||
param.Limit = &defaultLimit
|
||||
}
|
||||
|
||||
// set Offset if not set before
|
||||
if param.Offset == nil {
|
||||
param.Offset = &defaultOffset
|
||||
}
|
||||
|
||||
// get devices
|
||||
devices, err := nb.API.Dcim.DcimDevicesList(¶m, nil)
|
||||
if err != nil {
|
||||
return nil, e.FormatError("can't get devices", err)
|
||||
}
|
||||
|
||||
// check if there are more prefixes on other pages
|
||||
if devices.Payload.Next == nil {
|
||||
// return the results of the first batch, there are no others
|
||||
return devices.Payload.Results, nil
|
||||
}
|
||||
|
||||
// there are more pages to get
|
||||
|
||||
// calculate new offset
|
||||
newOffset := *param.Offset + *param.Limit
|
||||
|
||||
// set new offset
|
||||
param.Offset = &newOffset
|
||||
|
||||
// get prefixes from next page
|
||||
nextPagePrefixes, err := nb.GetDevices(param)
|
||||
if err != nil {
|
||||
return nil, e.FormatError("can't get prefixes", err)
|
||||
}
|
||||
|
||||
// append the results from next page to our prefixes to return
|
||||
devices.Payload.Results = append(devices.Payload.Results, nextPagePrefixes...)
|
||||
|
||||
// return all prefixes
|
||||
return devices.Payload.Results, nil
|
||||
}
|
4
ipam.go
4
ipam.go
@ -8,7 +8,7 @@ import (
|
||||
"github.com/netbox-community/go-netbox/v3/netbox/models"
|
||||
)
|
||||
|
||||
// GetPrefixes returns prefixes from the api filtered by the parameter list param
|
||||
// GetPrefixes returns prefixes from the api nb filtered by the parameter list param
|
||||
func (nb NetBoxAPI) GetPrefixes(param ipam.IpamPrefixesListParams) ([]*models.Prefix, error) {
|
||||
// check if context was set, if not set background
|
||||
if param.Context == nil {
|
||||
@ -61,7 +61,7 @@ func (nb NetBoxAPI) GetPrefixes(param ipam.IpamPrefixesListParams) ([]*models.Pr
|
||||
return prefixes.Payload.Results, nil
|
||||
}
|
||||
|
||||
// GetIPAddresses returns IP addresses from the api filtered by the parameter list param
|
||||
// GetIPAddresses returns IP addresses from the api nb filtered by the parameter list param
|
||||
func (nb NetBoxAPI) GetIPAddresses(param ipam.IpamIPAddressesListParams) ([]*models.IPAddress, error) {
|
||||
// check if context was set, if not set background
|
||||
if param.Context == nil {
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"github.com/netbox-community/go-netbox/v3/netbox/models"
|
||||
)
|
||||
|
||||
// GetClusters returns Clusters from the api filtered by the parameter list param
|
||||
// GetClusters returns Clusters from the api nb filtered by the parameter list param
|
||||
func (nb NetBoxAPI) GetClusters(param virtualization.VirtualizationClustersListParams) ([]*models.Cluster, error) {
|
||||
// check if context was set, if not set background
|
||||
if param.Context == nil {
|
||||
|
Loading…
Reference in New Issue
Block a user