diff --git a/client.go b/client.go index 82178df..805391b 100644 --- a/client.go +++ b/client.go @@ -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 } diff --git a/dcim.go b/dcim.go index 0bb9568..7e55839 100644 --- a/dcim.go +++ b/dcim.go @@ -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(¶m, 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 diff --git a/go.mod b/go.mod index 401419c..70394d8 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index a770103..4623a10 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/ipam.go b/ipam.go index 5235a38..368d9ef 100644 --- a/ipam.go +++ b/ipam.go @@ -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(¶m, 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(¶m, 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 diff --git a/virtualization.go b/virtualization.go index 0c4de4b..68475bb 100644 --- a/virtualization.go +++ b/virtualization.go @@ -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(¶m, 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(¶m, 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 +}