netboxapi/ipam.go

170 lines
4.5 KiB
Go
Raw Permalink Normal View History

2023-03-26 07:33:41 +00:00
package netboxapi
import (
"context"
e "git.stinnesbeck.com/nils/errorhandler"
"github.com/netbox-community/go-netbox/v3/netbox/client/ipam"
2023-03-27 22:17:57 +00:00
"github.com/netbox-community/go-netbox/v3/netbox/client/virtualization"
2023-03-26 07:33:41 +00:00
"github.com/netbox-community/go-netbox/v3/netbox/models"
)
// GetPrefixes returns prefixes from the api filtered by the parameter list param
func (nb NetBoxAPI) GetPrefixes(param ipam.IpamPrefixesListParams) ([]*models.Prefix, 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 prefixes
2023-03-26 15:34:41 +00:00
prefixes, err := nb.API.Ipam.IpamPrefixesList(&param, nil)
2023-03-26 07:33:41 +00:00
if err != nil {
return nil, e.FormatError("can't get prefixes", err)
}
2023-03-26 17:57:06 +00:00
// check if there are more prefixes on other pages
if prefixes.Payload.Next == nil {
2023-03-27 22:17:57 +00:00
// return the results of the first batch, there are no others
2023-03-26 17:57:06 +00:00
return prefixes.Payload.Results, nil
}
2023-03-26 07:33:41 +00:00
2023-03-26 17:57:06 +00:00
// there are more pages to get
2023-03-26 07:33:41 +00:00
2023-03-26 17:57:06 +00:00
// calculate new offset
newOffset := *param.Offset + *param.Limit
// set new offset
param.Offset = &newOffset
// get prefixes from next page
nextPagePrefixes, err := nb.GetPrefixes(param)
if err != nil {
return nil, e.FormatError("can't get prefixes", err)
2023-03-26 07:33:41 +00:00
}
2023-03-26 17:57:06 +00:00
// append the results from next page to our prefixes to return
prefixes.Payload.Results = append(prefixes.Payload.Results, nextPagePrefixes...)
// return all prefixes
return prefixes.Payload.Results, nil
2023-03-26 07:33:41 +00:00
}
// GetIPAddresses returns IP addresses from the api filtered by the parameter list param
func (nb NetBoxAPI) GetIPAddresses(param ipam.IpamIPAddressesListParams) ([]*models.IPAddress, 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 addresses
2023-03-26 15:34:41 +00:00
addresses, err := nb.API.Ipam.IpamIPAddressesList(&param, nil)
if err != nil {
return nil, e.FormatError("can't get prefixes", err)
}
// check if there are no more prefixes on other pages
if addresses.Payload.Next == nil {
2023-03-26 17:57:06 +00:00
// return the results of the first batch, there are no others
return addresses.Payload.Results, nil
}
2023-03-26 17:57:06 +00:00
// there are more pages to get
// calculate new offset
newOffset := *param.Offset + *param.Limit
2023-03-26 17:57:06 +00:00
// set new offset
param.Offset = &newOffset
// get prefixes from next page
nextPageIPAddresses, err := nb.GetIPAddresses(param)
if err != nil {
return nil, e.FormatError("can't get IP addresses", err)
}
2023-03-26 17:57:06 +00:00
// append the results from next page to our prefixes to return
addresses.Payload.Results = append(addresses.Payload.Results, nextPageIPAddresses...)
// return all addresses
2023-03-26 17:57:06 +00:00
return addresses.Payload.Results, nil
}
2023-03-27 22:17:57 +00:00
// 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
}