Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit d009e67

Browse files
flkhndlrPhilipPenquittfkhejulienduchesnealexweav
authored
Alerting: Add Option to get all alert rules (#144)
* fix: formating fixed * Trigger Build * adding alert_rule fetch function * correcting testcases, add documentation * correcting testcases * adding alert_rule fetch function (#1) Co-authored-by: fkhe <[email protected]> * Trigger Build * Add missing report fields (#143) grafana/terraform-provider-grafana#862 * Folder: Add option to force delete folder (#142) grafana/terraform-provider-grafana#855 * remove whitespace * clear whitespaces * adding back whitespaces * fix: change name of variable * Add dependabot * Folder delete: Fix panic when params are set (#146) * Dashboard: Get folder UID from reads (#147) It's easier to manage folders with UIDs than IDs. This will be used to add that functionality to the Terraform provider * Folder UID: Wrong field type (#148) Doh! * SLO: CRUD API Functionality (#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 * Trigger Build * Trigger Build --------- Co-authored-by: Philip Penquitt <[email protected]> Co-authored-by: fkhe <[email protected]> Co-authored-by: Julien Duchesne <[email protected]> Co-authored-by: Alex Weaver <[email protected]> Co-authored-by: Elaine Vuong <[email protected]>
1 parent 30b5926 commit d009e67

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

alerting_alert_rule.go

+29
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

+46-1
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)