removed error formatting, added getClusterGroups()

This commit is contained in:
Nils Stinnesbeck 2023-04-24 14:46:41 +02:00
parent 97f1af7f6d
commit b314297c97
Signed by: nils
GPG Key ID: 9AEBF097A4042590
6 changed files with 68 additions and 22 deletions

View File

@ -18,7 +18,7 @@ type NetBoxAPI struct {
// NewNetBoxClient returns a client to NetBox at the given url,
// using the provided token
func NewNetBoxClient(token string, url string) (NetBoxAPI, error) {
func NewNetBoxClient(token string, url string) NetBoxAPI {
// Construct the API basis and configure authentication
t := transport.New(url, client.DefaultBasePath, []string{"https"})
t.DefaultAuthentication = transport.APIKeyAuth(
@ -36,5 +36,5 @@ func NewNetBoxClient(token string, url string) (NetBoxAPI, error) {
Token: token,
URL: url,
}
return nb, nil
return nb
}

View File

@ -3,7 +3,6 @@ package netboxapi
import (
"context"
e "git.stinnesbeck.com/nils/errorhandler"
"github.com/netbox-community/go-netbox/v3/netbox/client/dcim"
"github.com/netbox-community/go-netbox/v3/netbox/models"
)
@ -31,7 +30,7 @@ func (nb NetBoxAPI) GetDevices(param dcim.DcimDevicesListParams) ([]*models.Devi
// get devices
devices, err := nb.API.Dcim.DcimDevicesList(&param, nil)
if err != nil {
return nil, e.FormatError("can't get devices", err)
return nil, err
}
// check if there are more prefixes on other pages
@ -51,7 +50,7 @@ func (nb NetBoxAPI) GetDevices(param dcim.DcimDevicesListParams) ([]*models.Devi
// get prefixes from next page
nextPagePrefixes, err := nb.GetDevices(param)
if err != nil {
return nil, e.FormatError("can't get prefixes", err)
return nil, err
}
// append the results from next page to our prefixes to return

1
go.mod
View File

@ -3,7 +3,6 @@ module git.stinnesbeck.com/nils/netboxapi
go 1.20
require (
git.stinnesbeck.com/nils/errorhandler v0.0.2
github.com/go-openapi/runtime v0.23.3
github.com/netbox-community/go-netbox/v3 v3.4.5
)

2
go.sum
View File

@ -1,5 +1,3 @@
git.stinnesbeck.com/nils/errorhandler v0.0.2 h1:1Mnbr1qArCWled9P1nTbzH9QBEWj9XsDXX5Wt2rO6NI=
git.stinnesbeck.com/nils/errorhandler v0.0.2/go.mod h1:9JEU3mS79yISI0qPVEP3e3Gg4ggD/OxLrPOPiqWQR3A=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=

View File

@ -3,7 +3,6 @@ package netboxapi
import (
"context"
e "git.stinnesbeck.com/nils/errorhandler"
"github.com/netbox-community/go-netbox/v3/netbox/client/ipam"
"github.com/netbox-community/go-netbox/v3/netbox/models"
)
@ -31,7 +30,7 @@ func (nb NetBoxAPI) GetPrefixes(param ipam.IpamPrefixesListParams) ([]*models.Pr
// get prefixes
prefixes, err := nb.API.Ipam.IpamPrefixesList(&param, nil)
if err != nil {
return nil, e.FormatError("can't get prefixes", err)
return nil, err
}
// check if there are more prefixes on other pages
@ -51,7 +50,7 @@ func (nb NetBoxAPI) GetPrefixes(param ipam.IpamPrefixesListParams) ([]*models.Pr
// get prefixes from next page
nextPagePrefixes, err := nb.GetPrefixes(param)
if err != nil {
return nil, e.FormatError("can't get prefixes", err)
return nil, err
}
// append the results from next page to our prefixes to return
@ -84,7 +83,7 @@ func (nb NetBoxAPI) GetIPAddresses(param ipam.IpamIPAddressesListParams) ([]*mod
// get addresses
addresses, err := nb.API.Ipam.IpamIPAddressesList(&param, nil)
if err != nil {
return nil, e.FormatError("can't get prefixes", err)
return nil, err
}
// check if there are no more prefixes on other pages
@ -104,7 +103,7 @@ func (nb NetBoxAPI) GetIPAddresses(param ipam.IpamIPAddressesListParams) ([]*mod
// get prefixes from next page
nextPageIPAddresses, err := nb.GetIPAddresses(param)
if err != nil {
return nil, e.FormatError("can't get IP addresses", err)
return nil, err
}
// append the results from next page to our prefixes to return

View File

@ -3,7 +3,6 @@ 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"
)
@ -31,10 +30,10 @@ func (nb NetBoxAPI) GetClusters(param virtualization.VirtualizationClustersListP
// get clusters
clusters, err := nb.API.Virtualization.VirtualizationClustersList(&param, nil)
if err != nil {
return nil, e.FormatError("can't get clusters", err)
return nil, err
}
// check if there are more prefixes on other pages
// check if there are more clusters on other pages
if clusters.Payload.Next == nil {
// return the results of the first batch, there are no others
return clusters.Payload.Results, nil
@ -48,15 +47,67 @@ func (nb NetBoxAPI) GetClusters(param virtualization.VirtualizationClustersListP
// set new offset
param.Offset = &newOffset
// get prefixes from next page
nextPagePrefixes, err := nb.GetClusters(param)
// get clusters from next page
nextPageClusters, err := nb.GetClusters(param)
if err != nil {
return nil, e.FormatError("can't get prefixes", err)
return nil, err
}
// append the results from next page to our prefixes to return
clusters.Payload.Results = append(clusters.Payload.Results, nextPagePrefixes...)
// append the results from next page to our clusters to return
clusters.Payload.Results = append(clusters.Payload.Results, nextPageClusters...)
// return all prefixes
// return all clusters
return clusters.Payload.Results, nil
}
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(&param, 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
}