2023-03-28 06:42:48 +00:00
|
|
|
package netboxapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/netbox-community/go-netbox/v3/netbox/client/virtualization"
|
|
|
|
"github.com/netbox-community/go-netbox/v3/netbox/models"
|
|
|
|
)
|
|
|
|
|
2023-03-28 06:50:42 +00:00
|
|
|
// GetClusters returns Clusters from the api nb filtered by the parameter list param
|
2023-03-28 06:42:48 +00:00
|
|
|
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 {
|
2023-04-24 12:46:41 +00:00
|
|
|
return nil, err
|
2023-03-28 06:42:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 12:46:41 +00:00
|
|
|
// check if there are more clusters on other pages
|
2023-03-28 06:42:48 +00:00
|
|
|
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
|
|
|
|
|
2023-04-24 12:46:41 +00:00
|
|
|
// get clusters from next page
|
|
|
|
nextPageClusters, err := nb.GetClusters(param)
|
2023-03-28 06:42:48 +00:00
|
|
|
if err != nil {
|
2023-04-24 12:46:41 +00:00
|
|
|
return nil, err
|
2023-03-28 06:42:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 12:46:41 +00:00
|
|
|
// append the results from next page to our clusters to return
|
|
|
|
clusters.Payload.Results = append(clusters.Payload.Results, nextPageClusters...)
|
2023-03-28 06:42:48 +00:00
|
|
|
|
2023-04-24 12:46:41 +00:00
|
|
|
// return all clusters
|
2023-03-28 06:42:48 +00:00
|
|
|
return clusters.Payload.Results, nil
|
|
|
|
}
|
2023-04-24 12:46:41 +00:00
|
|
|
|
|
|
|
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.API.Virtualization.VirtualizationClusterGroupsList(¶m, 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
|
|
|
|
}
|