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

Commit 01ad23c

Browse files
authored
Merge branch 'master' into alexweav/notif-policy-reset
2 parents 158ee7b + a07c689 commit 01ad23c

File tree

4 files changed

+177
-5
lines changed

4 files changed

+177
-5
lines changed

alerting_alert_rule.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ type AlertRule struct {
2626
Provenance string `json:"provenance"`
2727
}
2828

29+
// RuleGroup represents a group of rules in Grafana Alerting.
30+
type RuleGroup struct {
31+
Title string `json:"title"`
32+
FolderUID string `json:"folderUid"`
33+
Interval int64 `json:"interval"`
34+
Rules []AlertRule `json:"rules"`
35+
}
36+
2937
// AlertQuery represents a single query stage associated with an alert definition.
3038
type AlertQuery struct {
3139
DatasourceUID string `json:"datasourceUid,omitempty"`
@@ -64,6 +72,14 @@ func (c *Client) AlertRule(uid string) (AlertRule, error) {
6472
return result, err
6573
}
6674

75+
// AlertRuleGroup fetches a group of alert rules, identified by its name and the UID of its folder.
76+
func (c *Client) AlertRuleGroup(folderUID string, name string) (RuleGroup, error) {
77+
path := fmt.Sprintf("/api/v1/provisioning/folder/%s/rule-groups/%s", folderUID, name)
78+
result := RuleGroup{}
79+
err := c.request("GET", path, nil, nil, &result)
80+
return result, err
81+
}
82+
6783
// NewAlertRule creates a new alert rule and returns its UID.
6884
func (c *Client) NewAlertRule(ar *AlertRule) (string, error) {
6985
req, err := json.Marshal(ar)

alerting_alert_rule_test.go

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,28 @@ func TestAlertRules(t *testing.T) {
1818
t.Error(err)
1919
}
2020
if alertRule.UID != "123abcd" {
21-
t.Errorf("incorrect UID - expected %s got %#v", "123abcd", alertRule.UID)
21+
t.Errorf("incorrect UID - expected %s got %s", "123abcd", alertRule.UID)
22+
}
23+
})
24+
25+
t.Run("get alert rule group succeeds", func(t *testing.T) {
26+
server, client := gapiTestTools(t, 200, getAlertRuleGroupJSON)
27+
defer server.Close()
28+
29+
group, err := client.AlertRuleGroup("d8-gk06nz", "test")
30+
31+
if err != nil {
32+
t.Error(err)
33+
}
34+
t.Log(pretty.PrettyFormat(group))
35+
if group.Title != "test" {
36+
t.Errorf("incorrect title - expected %s got %s", "test", group.Title)
37+
}
38+
if group.FolderUID != "d8-gk06nz" {
39+
t.Errorf("incorrect folderUID - expected %s got %s", "d8-gk06nz", group.FolderUID)
40+
}
41+
if len(group.Rules) != 1 {
42+
t.Errorf("wrong number of rules, got %d", len(group.Rules))
2243
}
2344
})
2445

@@ -128,3 +149,92 @@ const getAlertRuleJSON = `
128149
"for": 0
129150
}
130151
`
152+
153+
const getAlertRuleGroupJSON = `
154+
{
155+
"title": "test",
156+
"folderUid": "d8-gk06nz",
157+
"interval": 60,
158+
"rules": [
159+
{
160+
"ID": 1,
161+
"OrgID": 1,
162+
"Title": "abc",
163+
"Condition": "B",
164+
"Data": [
165+
{
166+
"refId": "A",
167+
"queryType": "",
168+
"relativeTimeRange": {
169+
"from": 600,
170+
"to": 0
171+
},
172+
"datasourceUid": "PD8C576611E62080A",
173+
"model": {
174+
"hide": false,
175+
"intervalMs": 1000,
176+
"maxDataPoints": 43200,
177+
"refId": "A"
178+
}
179+
},
180+
{
181+
"refId": "B",
182+
"queryType": "",
183+
"relativeTimeRange": {
184+
"from": 0,
185+
"to": 0
186+
},
187+
"datasourceUid": "-100",
188+
"model": {
189+
"conditions": [
190+
{
191+
"evaluator": {
192+
"params": [
193+
3
194+
],
195+
"type": "gt"
196+
},
197+
"operator": {
198+
"type": "and"
199+
},
200+
"query": {
201+
"params": [
202+
"A"
203+
]
204+
},
205+
"reducer": {
206+
"params": [],
207+
"type": "last"
208+
},
209+
"type": "query"
210+
}
211+
],
212+
"datasource": {
213+
"type": "__expr__",
214+
"uid": "-100"
215+
},
216+
"hide": false,
217+
"intervalMs": 1000,
218+
"maxDataPoints": 43200,
219+
"refId": "B",
220+
"type": "classic_conditions"
221+
}
222+
}
223+
],
224+
"Updated": "2022-07-07T16:23:56-05:00",
225+
"IntervalSeconds": 60,
226+
"Version": 1,
227+
"UID": "hsXgz0enz",
228+
"NamespaceUID": "d8-gk06nz",
229+
"DashboardUID": null,
230+
"PanelID": null,
231+
"RuleGroup": "test",
232+
"RuleGroupIndex": 1,
233+
"NoDataState": "NoData",
234+
"ExecErrState": "Alerting",
235+
"For": 300000000000,
236+
"Annotations": {},
237+
"Labels": {}
238+
}
239+
]
240+
}`

alerting_contact_point.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"net/url"
78
)
89

910
// ContactPoint represents a Grafana Alerting contact point.
@@ -26,6 +27,18 @@ func (c *Client) ContactPoints() ([]ContactPoint, error) {
2627
return ps, nil
2728
}
2829

30+
// ContactPointsByName fetches contact points with the given name.
31+
func (c *Client) ContactPointsByName(name string) ([]ContactPoint, error) {
32+
ps := make([]ContactPoint, 0)
33+
params := url.Values{}
34+
params.Add("name", name)
35+
err := c.request("GET", "/api/v1/provisioning/contact-points", params, nil, &ps)
36+
if err != nil {
37+
return nil, err
38+
}
39+
return ps, nil
40+
}
41+
2942
// ContactPoint fetches a single contact point, identified by its UID.
3043
func (c *Client) ContactPoint(uid string) (ContactPoint, error) {
3144
ps, err := c.ContactPoints()

alerting_contact_point_test.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,31 @@ func TestContactPoints(t *testing.T) {
1818
}
1919
t.Log(pretty.PrettyFormat(ps))
2020
if len(ps) != 2 {
21-
t.Errorf("wrong number of contact points returned, got %#v", ps)
21+
t.Errorf("wrong number of contact points returned, got %d", len(ps))
2222
}
2323
if ps[0].UID != "" {
24-
t.Errorf("incorrect UID - expected %s on element %d, got %#v", "", 0, ps)
24+
t.Errorf("incorrect UID - expected %s on element %d, got %s", "", 0, ps[0].UID)
2525
}
2626
if ps[1].UID != "rc5r0bjnz" {
27-
t.Errorf("incorrect UID - expected %s on element %d, got %#v", "rc5r0bjnz", 0, ps)
27+
t.Errorf("incorrect UID - expected %s on element %d, got %s", "rc5r0bjnz", 1, ps[1].UID)
28+
}
29+
})
30+
31+
t.Run("get contact points by name succeeds", func(t *testing.T) {
32+
server, client := gapiTestTools(t, 200, getContactPointsQueryJSON)
33+
defer server.Close()
34+
35+
ps, err := client.ContactPointsByName("slack-receiver-1")
36+
37+
if err != nil {
38+
t.Error(err)
39+
}
40+
t.Log(pretty.PrettyFormat(ps))
41+
if len(ps) != 1 {
42+
t.Errorf("wrong number of contact points returned, got %d", len(ps))
43+
}
44+
if ps[0].UID != "rc5r0bjnz" {
45+
t.Errorf("incorrect UID - expected %s on element %d, got %s", "rc5r0bjnz", 0, ps[0].UID)
2846
}
2947
})
3048

@@ -39,7 +57,7 @@ func TestContactPoints(t *testing.T) {
3957
}
4058
t.Log(pretty.PrettyFormat(p))
4159
if p.UID != "rc5r0bjnz" {
42-
t.Errorf("incorrect UID - expected %s got %#v", "rc5r0bjnz", p)
60+
t.Errorf("incorrect UID - expected %s got %s", "rc5r0bjnz", p.UID)
4361
}
4462
})
4563

@@ -132,6 +150,21 @@ const getContactPointsJSON = `
132150
}
133151
]`
134152

153+
const getContactPointsQueryJSON = `
154+
[
155+
{
156+
"uid": "rc5r0bjnz",
157+
"name": "slack-receiver-1",
158+
"type": "slack",
159+
"disableResolveMessage": false,
160+
"settings": {
161+
"recipient": "@foo",
162+
"token": "[REDACTED]",
163+
"url": "[REDACTED]"
164+
}
165+
}
166+
]`
167+
135168
const writeContactPointJSON = `
136169
{
137170
"uid": "rc5r0bjnz",

0 commit comments

Comments
 (0)