Skip to content

Commit 5260930

Browse files
committed
Alerting: Add Option to get all alert rules grafana#144
Squashed commit of the following: commit b384168 Merge: bc342a8 3f84a92 Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Tue May 9 08:44:32 2023 +0200 Merge branch 'grafana:master' into master commit bc342a8 Author: Philip Penquitt <30809710+PhilipPenquitt@users.noreply.github.com> Date: Thu Apr 6 16:49:11 2023 +0200 Trigger Build commit 2a88c53 Author: phpe <30809710+PhilipPenquitt@users.noreply.github.com> Date: Thu Apr 6 16:46:06 2023 +0200 Trigger Build commit 2471267 Author: Elaine Vuong <elaine.yt.vuong@gmail.com> Date: Thu May 4 14:57:04 2023 -0400 SLO: CRUD API Functionality (grafana#145) * ListSLOs functionality retrives all SLOs * CreateSLO and GetSLO Functionality implemented * DeleteSLO Functionality implemented * UpdateSLO Functionality implemented * UpdateSLO Functionality corrected * Update Linting Errors * Removes Unnecessary Comments * Update Variable Naming * Lint Checker * Update Tests for SLOs * Updating Slo Types with a comment to original source files * Updated SLO Types and Tests commit 836a3db Author: Julien Duchesne <julien.duchesne@grafana.com> Date: Thu Apr 27 09:15:11 2023 -0400 Folder UID: Wrong field type (grafana#148) Doh! commit ca95730 Author: Julien Duchesne <julien.duchesne@grafana.com> Date: Thu Apr 27 08:35:50 2023 -0400 Dashboard: Get folder UID from reads (grafana#147) It's easier to manage folders with UIDs than IDs. This will be used to add that functionality to the Terraform provider commit fcca53a Author: Julien Duchesne <julien.duchesne@grafana.com> Date: Tue Apr 25 13:02:16 2023 -0400 Folder delete: Fix panic when params are set (grafana#146) commit 65edf1b Author: Alex Weaver <weaver.alex.d@gmail.com> Date: Tue May 2 15:02:11 2023 -0500 Add dependabot commit 3a31f6f Merge: 0252eaa 6b31ab5 Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Tue Apr 18 10:58:34 2023 +0200 Merge branch 'master' of https://github.com/flkhndlr/grafana-api-golang-client commit 0252eaa Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Tue Apr 18 10:57:39 2023 +0200 fix: change name of variable commit 6b31ab5 Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Tue Apr 18 10:44:29 2023 +0200 adding back whitespaces commit b1311f0 Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Tue Apr 18 10:41:25 2023 +0200 clear whitespaces commit 17398b1 Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Tue Apr 18 10:29:34 2023 +0200 remove whitespace commit 10b4067 Author: Julien Duchesne <julien.duchesne@grafana.com> Date: Mon Apr 10 18:58:06 2023 -0400 Folder: Add option to force delete folder (grafana#142) grafana/terraform-provider-grafana#855 commit e73fa1f Author: Julien Duchesne <julien.duchesne@grafana.com> Date: Mon Apr 10 18:57:49 2023 -0400 Add missing report fields (grafana#143) grafana/terraform-provider-grafana#862 commit 62aa037 Author: Philip Penquitt <30809710+PhilipPenquitt@users.noreply.github.com> Date: Thu Apr 6 16:49:11 2023 +0200 Trigger Build commit 2cb0ad5 Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Thu Apr 6 15:01:20 2023 +0200 adding alert_rule fetch function (grafana#1) Co-authored-by: fkhe <Falk.Haendler@t-systems.com> commit c27c52e Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Thu Apr 6 14:59:04 2023 +0200 correcting testcases commit 4732c3e Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Thu Apr 6 14:49:25 2023 +0200 correcting testcases, add documentation commit 1921ba9 Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com> Date: Thu Apr 6 13:23:20 2023 +0200 adding alert_rule fetch function commit 64ddabb Author: phpe <30809710+PhilipPenquitt@users.noreply.github.com> Date: Thu Apr 6 16:46:06 2023 +0200 Trigger Build commit e009637 Author: Philip Penquitt <30809710+PhilipPenquitt@users.noreply.github.com> Date: Thu Apr 6 14:37:21 2023 +0200 fix: formating fixed
1 parent b3a3c20 commit 5260930

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

alerting_alert_rule.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gapi
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/url"
67
"time"
78
)
89

@@ -62,6 +63,34 @@ type RelativeTimeRange struct {
6263
To time.Duration `json:"to"`
6364
}
6465

66+
// AlertRules fetches and returns Grafana alertRules.
67+
func (c *Client) AlertRules() ([]AlertRule, error) {
68+
const limit = 1000
69+
70+
var (
71+
page = 0
72+
newAlertRules []AlertRule
73+
alertRules []AlertRule
74+
query = make(url.Values)
75+
)
76+
query.Set("limit", fmt.Sprint(limit))
77+
78+
for {
79+
page++
80+
query.Set("page", fmt.Sprint(page))
81+
82+
if err := c.request("GET", "/api/v1/provisioning/alert-rules", query, nil, &newAlertRules); err != nil {
83+
return nil, err
84+
}
85+
86+
alertRules = append(alertRules, newAlertRules...)
87+
88+
if len(newAlertRules) < limit {
89+
return alertRules, nil
90+
}
91+
}
92+
}
93+
6594
// AlertRule fetches a single alert rule, identified by its UID.
6695
func (c *Client) AlertRule(uid string) (AlertRule, error) {
6796
path := fmt.Sprintf("/api/v1/provisioning/alert-rules/%s", uid)

alerting_alert_rule_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,47 @@ package gapi
22

33
import (
44
"encoding/json"
5+
"strings"
56
"testing"
67
"time"
78

89
"github.com/gobs/pretty"
910
)
1011

1112
func TestAlertRules(t *testing.T) {
13+
mockData := strings.Repeat(getAlertRulesJSON+",", 1000) // make 1000 alertRules.
14+
mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list.
15+
16+
// This creates 1000 + 1000 + 1 (2001, 3 calls) worth of alertRules.
17+
18+
client := gapiTestToolsFromCalls(t, []mockServerCall{
19+
{200, mockData},
20+
{200, mockData},
21+
{200, "[" + getAlertRulesJSON + "]"},
22+
})
23+
24+
const dashCount = 2001
25+
26+
alertRules, err := client.AlertRules()
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
31+
t.Log(pretty.PrettyFormat(alertRules))
32+
33+
if len(alertRules) != dashCount {
34+
t.Errorf("Length of returned folders should be %d", dashCount)
35+
}
36+
37+
if alertRules[0].UID != "123abcd" || alertRules[0].Title != "Always in alarm" {
38+
t.Error("Not correctly parsing returned alertRules.")
39+
}
40+
if alertRules[dashCount-1].UID != "123abcd" || alertRules[dashCount-1].Title != "Always in alarm" {
41+
t.Error("Not correctly parsing returned alertRules.")
42+
}
43+
}
44+
45+
func TestAlertRule(t *testing.T) {
1246
t.Run("get alert rule succeeds", func(t *testing.T) {
1347
client := gapiTestTools(t, 200, getAlertRuleJSON)
1448

@@ -161,7 +195,18 @@ const writeAlertRuleJSON = `
161195
"for": "1m"
162196
}
163197
`
164-
198+
const getAlertRulesJSON = `{
199+
"conditions": "A",
200+
"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}}],
201+
"execErrState": "OK",
202+
"folderUID": "project_test",
203+
"noDataState": "OK",
204+
"orgId": 1,
205+
"uid": "123abcd",
206+
"ruleGroup": "eval_group_1",
207+
"title": "Always in alarm",
208+
"for": "1m"
209+
}`
165210
const getAlertRuleJSON = `
166211
{
167212
"conditions": "A",

0 commit comments

Comments
 (0)