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

Commit 466e338

Browse files
committed
Implement read policy tree and tests
1 parent c2e825a commit 466e338

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

alerting_notification_policy.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import (
44
"encoding/json"
55
"fmt"
66
"regexp"
7-
"time"
87
)
98

109
// Represents a notification routing tree in Grafana Alerting.
1110
type NotificationPolicy struct {
1211
Receiver string `json:"receiver,omitempty"`
1312
GroupBy []string `json:"group_by,omitempty"`
1413
Routes []SpecificPolicy `json:"routes,omitempty"`
15-
GroupWait time.Duration `json:"group_wait,omitempty"`
16-
GroupInterval time.Duration `json:"group_interval,omitempty"`
17-
RepeatInterval time.Duration `json:"repeat_interval,omitempty"`
14+
GroupWait string `json:"group_wait,omitempty"`
15+
GroupInterval string `json:"group_interval,omitempty"`
16+
RepeatInterval string `json:"repeat_interval,omitempty"`
1817
Provenance string `json:"provenance,omitempty"`
1918
}
2019

@@ -26,9 +25,9 @@ type SpecificPolicy struct {
2625
MuteTimeIntervals []string `json:"mute_time_intervals,omitempty"`
2726
Continue bool `json:"continue"`
2827
Routes []SpecificPolicy `json:"routes,omitempty"`
29-
GroupWait time.Duration `json:"group_wait,omitempty"`
30-
GroupInterval time.Duration `json:"group_interval,omitempty"`
31-
RepeatInterval time.Duration `json:"repeat_interval,omitempty"`
28+
GroupWait string `json:"group_wait,omitempty"`
29+
GroupInterval string `json:"group_interval,omitempty"`
30+
RepeatInterval string `json:"repeat_interval,omitempty"`
3231
}
3332

3433
type Matchers []Matcher
@@ -111,3 +110,9 @@ func (m Matchers) MarshalJSON() ([]byte, error) {
111110
}
112111
return json.Marshal(result)
113112
}
113+
114+
func (c *Client) NotificationPolicy() (NotificationPolicy, error) {
115+
np := NotificationPolicy{}
116+
err := c.request("GET", "/api/v1/provisioning/policies", nil, nil, &np)
117+
return np, err
118+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gobs/pretty"
7+
)
8+
9+
func TestNotificationPolicies(t *testing.T) {
10+
t.Run("get policy tree succeeds", func(t *testing.T) {
11+
server, client := gapiTestTools(t, 200, notificationPolicyJSON)
12+
defer server.Close()
13+
/*cfg := Config{
14+
BasicAuth: url.UserPassword("admin", "admin"),
15+
}
16+
client, _ := New("http://localhost:3000", cfg)*/
17+
18+
np, err := client.NotificationPolicy()
19+
20+
if err != nil {
21+
t.Error(err)
22+
}
23+
t.Log(pretty.PrettyFormat(np))
24+
if np.Receiver != "grafana-default-email" {
25+
t.Errorf("wrong receiver, got %#v", np.Receiver)
26+
}
27+
if len(np.Routes) != 1 {
28+
t.Errorf("wrong number of routes returned, got %#v", np)
29+
}
30+
})
31+
}
32+
33+
const notificationPolicyJSON = `
34+
{
35+
"receiver": "grafana-default-email",
36+
"group_by": [
37+
"..."
38+
],
39+
"routes": [
40+
{
41+
"receiver": "grafana-default-email",
42+
"object_matchers": [
43+
[
44+
"a",
45+
"=",
46+
"b"
47+
],
48+
[
49+
"asdf",
50+
"!=",
51+
"jk"
52+
]
53+
]
54+
}
55+
],
56+
"group_wait": "5s",
57+
"group_interval": "1m",
58+
"repeat_interval": "1h"
59+
}`

0 commit comments

Comments
 (0)