netboxapi/virtualization.go

114 lines
2.8 KiB
Go

package netboxapi
import (
"context"
"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.Virtualization.VirtualizationClustersList(&param, nil)
if err != nil {
return nil, err
}
// check if there are more clusters 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 clusters from next page
nextPageClusters, err := nb.GetClusters(param)
if err != nil {
return nil, err
}
// append the results from next page to our clusters to return
clusters.Payload.Results = append(clusters.Payload.Results, nextPageClusters...)
// return all clusters
return clusters.Payload.Results, nil
}
func (nb NetBoxAPI) GetClusterGroups(param virtualization.VirtualizationClusterGroupsListParams) ([]*models.ClusterGroup, 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 clusterGroups
clusterGroups, err := nb.Virtualization.VirtualizationClusterGroupsList(&param, nil)
if err != nil {
return nil, err
}
// check if there are more cluster groups on other pages
if clusterGroups.Payload.Next == nil {
// return the results of the first batch, there are no others
return clusterGroups.Payload.Results, nil
}
// there are more pages to get
// calculate new offset
newOffset := *param.Offset + *param.Limit
// set new offset
param.Offset = &newOffset
// get clusterGroups from next page
nextPageGroups, err := nb.GetClusterGroups(param)
if err != nil {
return nil, err
}
// append the results from next page to our cluster groups to return
clusterGroups.Payload.Results = append(clusterGroups.Payload.Results, nextPageGroups...)
// return all cluster groups
return clusterGroups.Payload.Results, nil
}