From 88f9345cacaa5a6b49cf76361a0fcc5dbe1545bd Mon Sep 17 00:00:00 2001 From: Nils Stinnesbeck Date: Tue, 28 Mar 2023 08:42:48 +0200 Subject: [PATCH] GetClusters belongs in virtualization, moved it --- ipam.go | 54 ----------------------------------------- virtualization.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 virtualization.go diff --git a/ipam.go b/ipam.go index e914999..ea836fc 100644 --- a/ipam.go +++ b/ipam.go @@ -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(¶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 -} diff --git a/virtualization.go b/virtualization.go new file mode 100644 index 0000000..5157579 --- /dev/null +++ b/virtualization.go @@ -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(¶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 +}