Update pagination parameters to be consistent with existing parameters

Signed-off-by: Raphael Silva <rapphil@gmail.com>
This commit is contained in:
Raphael Silva 2024-07-15 05:36:45 +00:00
parent 7ceddece17
commit b9c0c847f9
3 changed files with 11 additions and 9 deletions

View file

@ -695,6 +695,8 @@ URL query parameters:
- `file[]=<string>`: only return rules with the given filepath. If the parameter is repeated, rules with any of the provided filepaths are returned. When the parameter is absent or empty, no filtering is done.
- `exclude_alerts=<bool>`: only return rules, do not return active alerts.
- `match[]=<label_selector>`: only return rules that have configured labels that satisfy the label selectors. If the parameter is repeated, rules that match any of the sets of label selectors are returned. Note that matching is on the labels in the definition of each rule, not on the values after template expansion (for alerting rules). Optional.
- `max_groups=<number>`: return up to `max_groups` rule groups. In case there are more groups, a `nextToken` property will be present in the response. The value in that property can be used in subsequent requests in the `next_token` property to paginate over the remaining rule groups. The `nextToken` property will not be present in the last response.
- `next_token`: the pagination token that was returned in previous request when the `max_groups` property is set. The pagination token is used to iteratively paginate over a large number of rule groups.
```json
$ curl http://localhost:9090/api/v1/rules

View file

@ -1589,18 +1589,18 @@ func parseListRulesPaginationRequest(r *http.Request) (*listRulesPaginationReque
err error
)
if r.URL.Query().Get("maxRuleGroups") != "" {
maxRuleGroups, err = strconv.ParseInt(r.URL.Query().Get("maxRuleGroups"), 10, 32)
if r.URL.Query().Get("max_rule_groups") != "" {
maxRuleGroups, err = strconv.ParseInt(r.URL.Query().Get("max_rule_groups"), 10, 32)
if err != nil || maxRuleGroups < 0 {
return nil, &parsePaginationError{
err: fmt.Errorf("maxRuleGroups need to be a valid number greater than or equal to 0: %w", err),
parameter: "maxRuleGroups",
err: fmt.Errorf("max_rule_groups need to be a valid number greater than or equal to 0: %w", err),
parameter: "max_rule_groups",
}
}
}
if r.URL.Query().Get("nextToken") != "" {
nextToken = r.URL.Query().Get("nextToken")
if r.URL.Query().Get("next_token") != "" {
nextToken = r.URL.Query().Get("next_token")
}
if maxRuleGroups >= 0 || nextToken != "" {

View file

@ -2744,7 +2744,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
{
endpoint: api.rules,
query: url.Values{
"maxRuleGroups": []string{"1"},
"max_rule_groups": []string{"1"},
},
response: &RuleDiscovery{
NextToken: getRuleGroupNextToken("/path/to/file", "grp2"),
@ -2840,8 +2840,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
{
endpoint: api.rules,
query: url.Values{
"maxRuleGroups": []string{"1"},
"nextToken": []string{getRuleGroupNextToken("/path/to/file", "grp2")},
"max_rule_groups": []string{"1"},
"next_token": []string{getRuleGroupNextToken("/path/to/file", "grp2")},
},
response: &RuleDiscovery{
RuleGroups: []*RuleGroup{