added GetClusters function

This commit is contained in:
Nils Stinnesbeck 2023-03-28 00:17:57 +02:00
parent d47c0c8603
commit c3a9f92358
Signed by: nils
GPG Key ID: 9AEBF097A4042590

56
ipam.go
View File

@ -5,6 +5,7 @@ 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"
)
@ -36,7 +37,7 @@ func (nb NetBoxAPI) GetPrefixes(param ipam.IpamPrefixesListParams) ([]*models.Pr
// check if there are more prefixes on other pages
if prefixes.Payload.Next == nil {
// return the results of the first batch, there are no others
// return the results of the first batch, there are no others
return prefixes.Payload.Results, nil
}
@ -113,3 +114,56 @@ 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
}