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

Commit b5a29e7

Browse files
committed
Method for setting policies and tests
1 parent 466e338 commit b5a29e7

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

alerting_notification_policy.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package gapi
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
6-
"regexp"
77
)
88

99
// Represents a notification routing tree in Grafana Alerting.
@@ -36,7 +36,6 @@ type Matcher struct {
3636
Type MatchType
3737
Name string
3838
Value string
39-
re *regexp.Regexp
4039
}
4140

4241
type MatchType int
@@ -87,13 +86,6 @@ func (m *Matchers) UnmarshalJSON(data []byte) error {
8786
Name: rawMatcher[0],
8887
Value: rawMatcher[2],
8988
}
90-
if matchType == MatchRegexp || matchType == MatchNotRegexp {
91-
re, err := regexp.Compile("^(?:" + rawMatcher[2] + ")$")
92-
if err != nil {
93-
return err
94-
}
95-
matcher.re = re
96-
}
9789
*m = append(*m, matcher)
9890
}
9991
return nil
@@ -111,8 +103,18 @@ func (m Matchers) MarshalJSON() ([]byte, error) {
111103
return json.Marshal(result)
112104
}
113105

106+
// NotificationPolicy fetches the notification policy tree.
114107
func (c *Client) NotificationPolicy() (NotificationPolicy, error) {
115108
np := NotificationPolicy{}
116109
err := c.request("GET", "/api/v1/provisioning/policies", nil, nil, &np)
117110
return np, err
118111
}
112+
113+
// SetNotificationPolicy sets the notification policy tree.
114+
func (c *Client) SetNotificationPolicy(np *NotificationPolicy) error {
115+
req, err := json.Marshal(np)
116+
if err != nil {
117+
return err
118+
}
119+
return c.request("PUT", "/api/v1/provisioning/policies", nil, bytes.NewBuffer(req), nil)
120+
}

alerting_notification_policy_test.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ func TestNotificationPolicies(t *testing.T) {
1010
t.Run("get policy tree succeeds", func(t *testing.T) {
1111
server, client := gapiTestTools(t, 200, notificationPolicyJSON)
1212
defer server.Close()
13-
/*cfg := Config{
14-
BasicAuth: url.UserPassword("admin", "admin"),
15-
}
16-
client, _ := New("http://localhost:3000", cfg)*/
1713

1814
np, err := client.NotificationPolicy()
1915

@@ -28,6 +24,52 @@ func TestNotificationPolicies(t *testing.T) {
2824
t.Errorf("wrong number of routes returned, got %#v", np)
2925
}
3026
})
27+
28+
t.Run("set policy tree succeeds", func(t *testing.T) {
29+
server, client := gapiTestTools(t, 202, `{"message":"created"}`)
30+
defer server.Close()
31+
np := createNotificationPolicy()
32+
33+
err := client.SetNotificationPolicy(&np)
34+
35+
if err != nil {
36+
t.Error(err)
37+
}
38+
})
39+
}
40+
41+
func createNotificationPolicy() NotificationPolicy {
42+
return NotificationPolicy{
43+
Receiver: "grafana-default-email",
44+
GroupBy: []string{"asdfasdf", "alertname"},
45+
Routes: []SpecificPolicy{
46+
{
47+
Receiver: "grafana-default-email",
48+
ObjectMatchers: Matchers{
49+
{
50+
Type: MatchNotEqual,
51+
Name: "abc",
52+
Value: "def",
53+
},
54+
},
55+
Continue: true,
56+
},
57+
{
58+
Receiver: "grafana-default-email",
59+
ObjectMatchers: Matchers{
60+
{
61+
Type: MatchRegexp,
62+
Name: "jkl",
63+
Value: "something.*",
64+
},
65+
},
66+
Continue: false,
67+
},
68+
},
69+
GroupWait: "10s",
70+
GroupInterval: "5m",
71+
RepeatInterval: "4h",
72+
}
3173
}
3274

3375
const notificationPolicyJSON = `

0 commit comments

Comments
 (0)