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

Commit 5239257

Browse files
authored
Merge branch 'master' into alexweav/contact-points-by-name
2 parents a254ec2 + f30e403 commit 5239257

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
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+
}`

0 commit comments

Comments
 (0)