go-netbox/v4 - custom fields filtering request
Hello,
I'm actually trying to filter IpAddresses using IpamAPI.IpamIpAddressesList, I can't find a way to filter my results using custom fields.
How can I do that ?
Regards, Guillaume
I didn't saw issue that #210, but, I finally manage to filter witn custom fields by using Q() method :
func (r *NetboxClient) GetPrefixWithQuery(Q string, ctx context.Context) (*netbox.PaginatedPrefixList, error) {
getPrefix, _, err := r.client.IpamAPI.IpamPrefixesList(ctx).Q(Q).Execute()
if err != nil {
return nil, err
}
return getPrefix, nil
}
You can call a filter with a custom field :
req, err := NetboxClient.GetPrefixWithQuery("&cf_customfield1=randomvalue", ctx)
But in the current release there is an issue on Q(), all special chars are HTTP escaped, and will result with a HTTP request like this :
http://netbox.central.sandbox.airnity.private/api/ipam/prefixes/?q=%26cf_customfield1%3Drandomvalue
I have made some change on my side on client.go file on go-netbox/v4 :
original from package :
query := url.Query()
for k, v := range queryParams {
for _, iv := range v {
query.Add(k, iv)
}
}
// Encode the parameters.
url.RawQuery = queryParamSplit.ReplaceAllStringFunc(query.Encode(), func(s string) string {
pieces := strings.Split(s, "=")
pieces[0] = queryDescape.Replace(pieces[0])
return strings.Join(pieces, "=")
})
To :
// Adding Query Param with special handling for 'q' parameter
query := url.Query()
var qParam string
var hasQParam bool
for k, v := range queryParams {
for _, iv := range v {
if k == "q" {
// Special handling for 'q' parameter to avoid double URL encoding
qParam = iv
hasQParam = true
} else {
query.Add(k, iv)
}
}
}
// Encode the normal parameters
encodedQuery := queryParamSplit.ReplaceAllStringFunc(query.Encode(), func(s string) string {
pieces := strings.Split(s, "=")
pieces[0] = queryDescape.Replace(pieces[0])
return strings.Join(pieces, "=")
})
// Build final query string with special handling for 'q'
if hasQParam {
if encodedQuery != "" {
url.RawQuery = encodedQuery + "&q=" + qParam
} else {
url.RawQuery = "q=" + qParam
}
} else {
url.RawQuery = encodedQuery
}
I have made change on the client.mustache template and regenerate library on my side. It works like a charm, but I don't found them on that repository, so can't PR for that. Any idea how I can push and PR that change @nutgood ?
3 weeks since my last message, any mainteners of that repository can answer ?