From 1921ba94be2701ba49b2bd1ec03089f360bb3c0d Mon Sep 17 00:00:00 2001 From: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Thu, 6 Apr 2023 13:23:20 +0200 Subject: [PATCH] adding alert_rule fetch function --- alerting_alert_rule.go | 28 +++++++++++++++++++++++ alerting_alert_rule_test.go | 45 ++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/alerting_alert_rule.go b/alerting_alert_rule.go index 87521f28..e1546fc7 100644 --- a/alerting_alert_rule.go +++ b/alerting_alert_rule.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/url" "time" ) @@ -63,6 +64,33 @@ type RelativeTimeRange struct { To time.Duration `json:"to"` } +func (c *Client) AlertRules() ([]AlertRule, error) { + const limit = 1000 + + var ( + page = 0 + newAlertRules []AlertRule + alertRules []AlertRule + query = make(url.Values) + ) + query.Set("limit", fmt.Sprint(limit)) + + for { + page++ + query.Set("page", fmt.Sprint(page)) + + if err := c.request("GET", "/api/v1/provisioning/alert-rules", query, nil, &newAlertRules); err != nil { + return nil, err + } + + alertRules = append(alertRules, newAlertRules...) + + if len(newAlertRules) < limit { + return alertRules, nil + } + } +} + // AlertRule fetches a single alert rule, identified by its UID. func (c *Client) AlertRule(uid string) (AlertRule, error) { path := fmt.Sprintf("/api/v1/provisioning/alert-rules/%s", uid) diff --git a/alerting_alert_rule_test.go b/alerting_alert_rule_test.go index e617eed5..7cae79e3 100644 --- a/alerting_alert_rule_test.go +++ b/alerting_alert_rule_test.go @@ -2,6 +2,7 @@ package gapi import ( "encoding/json" + "strings" "testing" "time" @@ -9,6 +10,37 @@ import ( ) func TestAlertRules(t *testing.T) { + mockData := strings.Repeat(getAlertRulesJSON+",", 1000) // make 1000 folders. + mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list. + + // This creates 1000 + 1000 + 1 (2001, 3 calls) worth of folders. + client := gapiTestToolsFromCalls(t, []mockServerCall{ + {200, mockData}, + {200, mockData}, + {200, "[" + getFolderJSON + "]"}, + }) + + const dashCount = 2001 + + alertRules, err := client.AlertRules() + if err != nil { + t.Fatal(err) + } + + t.Log(pretty.PrettyFormat(alertRules)) + + if len(alertRules) != dashCount { + t.Errorf("Length of returned folders should be %d", dashCount) + } + if alertRules[0].ID != 1 || alertRules[0].Title != "Departmenet ABC" { + t.Error("Not correctly parsing returned folders.") + } + if alertRules[dashCount-1].ID != 1 || alertRules[dashCount-1].Title != "Departmenet ABC" { + t.Error("Not correctly parsing returned folders.") + } +} + +func TestAlertRule(t *testing.T) { t.Run("get alert rule succeeds", func(t *testing.T) { client := gapiTestTools(t, 200, getAlertRuleJSON) @@ -161,7 +193,18 @@ const writeAlertRuleJSON = ` "for": "1m" } ` - +const getAlertRulesJSON = `{ + "conditions": "A", + "data": [{"datasourceUid":"-100","model":{"conditions":[{"evaluator":{"params":[0,0],"type":"gt"},"operator":{"type":"and"},"query":{"params":[]},"reducer":{"params":[],"type":"avg"},"type":"query"}],"datasource":{"type":"__expr__","uid":"__expr__"},"expression":"1 == 1","hide":false,"intervalMs":1000,"maxDataPoints":43200,"refId":"A","type":"math"},"queryType":"","refId":"A","relativeTimeRange":{"from":0,"to":0}}], + "execErrState": "OK", + "folderUID": "project_test", + "noDataState": "OK", + "orgId": 1, + "uid": "123abcd", + "ruleGroup": "eval_group_1", + "title": "Always in alarm", + "for": "1m" +}` const getAlertRuleJSON = ` { "conditions": "A",