62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package netboxapi
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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, 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, 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
|
|
}
|