GetClusters belongs in virtualization, moved it

This commit is contained in:
Nils Stinnesbeck 2023-03-28 08:42:48 +02:00
parent c3a9f92358
commit 88f9345cac
Signed by: nils
GPG Key ID: 9AEBF097A4042590
2 changed files with 62 additions and 54 deletions

54
ipam.go
View File

@ -5,7 +5,6 @@ import (
e "git.stinnesbeck.com/nils/errorhandler"
"github.com/netbox-community/go-netbox/v3/netbox/client/ipam"
"github.com/netbox-community/go-netbox/v3/netbox/client/virtualization"
"github.com/netbox-community/go-netbox/v3/netbox/models"
)
@ -114,56 +113,3 @@ func (nb NetBoxAPI) GetIPAddresses(param ipam.IpamIPAddressesListParams) ([]*mod
// return all addresses
return addresses.Payload.Results, nil
}
// GetClusters returns Clusters from the api 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(&param, 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
}

62
virtualization.go Normal file
View File

@ -0,0 +1,62 @@
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 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(&param, 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
}