From 97f1af7f6d6c373bdaacd3b7acbdd34cc4022fa8 Mon Sep 17 00:00:00 2001 From: Nils Stinnesbeck Date: Tue, 28 Mar 2023 08:50:42 +0200 Subject: [PATCH] added GetDevices function --- dcim.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ ipam.go | 4 +-- virtualization.go | 2 +- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 dcim.go diff --git a/dcim.go b/dcim.go new file mode 100644 index 0000000..0bb9568 --- /dev/null +++ b/dcim.go @@ -0,0 +1,62 @@ +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" +) + +// GetDevices returns devices from the api nb filtered by the parameter list param +func (nb NetBoxAPI) GetDevices(param dcim.DcimDevicesListParams) ([]*models.DeviceWithConfigContext, 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 devices + devices, err := nb.API.Dcim.DcimDevicesList(¶m, nil) + if err != nil { + return nil, e.FormatError("can't get devices", err) + } + + // check if there are more prefixes on other pages + if devices.Payload.Next == nil { + // return the results of the first batch, there are no others + return devices.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.GetDevices(param) + if err != nil { + return nil, e.FormatError("can't get prefixes", err) + } + + // append the results from next page to our prefixes to return + devices.Payload.Results = append(devices.Payload.Results, nextPagePrefixes...) + + // return all prefixes + return devices.Payload.Results, nil +} diff --git a/ipam.go b/ipam.go index ea836fc..5235a38 100644 --- a/ipam.go +++ b/ipam.go @@ -8,7 +8,7 @@ import ( "github.com/netbox-community/go-netbox/v3/netbox/models" ) -// GetPrefixes returns prefixes from the api filtered by the parameter list param +// GetPrefixes returns prefixes from the api nb 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 { @@ -61,7 +61,7 @@ func (nb NetBoxAPI) GetPrefixes(param ipam.IpamPrefixesListParams) ([]*models.Pr return prefixes.Payload.Results, nil } -// GetIPAddresses returns IP addresses from the api filtered by the parameter list param +// GetIPAddresses returns IP addresses from the api nb 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 { diff --git a/virtualization.go b/virtualization.go index 5157579..0c4de4b 100644 --- a/virtualization.go +++ b/virtualization.go @@ -8,7 +8,7 @@ import ( "github.com/netbox-community/go-netbox/v3/netbox/models" ) -// GetClusters returns Clusters from the api filtered by the parameter list param +// GetClusters returns Clusters from the api nb 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 {