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" ) // 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 prefixes, err := nb.API.Ipam.IpamPrefixesList(¶m, nil) if err != nil { return nil, e.FormatError("can't get prefixes", err) } // check if there are more prefixes on other pages if prefixes.Payload.Next == nil { // return the results of the first batch, there are no others return prefixes.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.GetPrefixes(param) if err != nil { return nil, e.FormatError("can't get prefixes", err) } // 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 } // 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 addresses, err := nb.API.Ipam.IpamIPAddressesList(¶m, 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 { // return the results of the first batch, there are no others return addresses.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 nextPageIPAddresses, err := nb.GetIPAddresses(param) if err != nil { return nil, e.FormatError("can't get IP addresses", err) } // append the results from next page to our prefixes to return addresses.Payload.Results = append(addresses.Payload.Results, nextPageIPAddresses...) // return all addresses return addresses.Payload.Results, nil }