63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package netboxapi
|
|
|
|
import (
|
|
"context"
|
|
|
|
e "git.stinnesbeck.com/nils/errorhandler"
|
|
"github.com/netbox-community/go-netbox/v3/netbox/client/virtualization"
|
|
"github.com/netbox-community/go-netbox/v3/netbox/models"
|
|
)
|
|
|
|
// 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 {
|
|
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 clusters
|
|
clusters, err := nb.API.Virtualization.VirtualizationClustersList(¶m, nil)
|
|
if err != nil {
|
|
return nil, e.FormatError("can't get clusters", err)
|
|
}
|
|
|
|
// check if there are more prefixes on other pages
|
|
if clusters.Payload.Next == nil {
|
|
// return the results of the first batch, there are no others
|
|
return clusters.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.GetClusters(param)
|
|
if err != nil {
|
|
return nil, e.FormatError("can't get prefixes", err)
|
|
}
|
|
|
|
// append the results from next page to our prefixes to return
|
|
clusters.Payload.Results = append(clusters.Payload.Results, nextPagePrefixes...)
|
|
|
|
// return all prefixes
|
|
return clusters.Payload.Results, nil
|
|
}
|