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

Commit 9bc48c7

Browse files
authored
Merge pull request #98 from grafana/alexweav/notif-policy-reset
Alerting: Add method for resetting notification policies
2 parents a07c689 + 01ad23c commit 9bc48c7

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

alerting_notification_policy.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// Represents a notification routing tree in Grafana Alerting.
10-
type NotificationPolicy struct {
10+
type NotificationPolicyTree struct {
1111
Receiver string `json:"receiver,omitempty"`
1212
GroupBy []string `json:"group_by,omitempty"`
1313
Routes []SpecificPolicy `json:"routes,omitempty"`
@@ -104,17 +104,21 @@ func (m Matchers) MarshalJSON() ([]byte, error) {
104104
}
105105

106106
// NotificationPolicy fetches the notification policy tree.
107-
func (c *Client) NotificationPolicy() (NotificationPolicy, error) {
108-
np := NotificationPolicy{}
107+
func (c *Client) NotificationPolicyTree() (NotificationPolicyTree, error) {
108+
np := NotificationPolicyTree{}
109109
err := c.request("GET", "/api/v1/provisioning/policies", nil, nil, &np)
110110
return np, err
111111
}
112112

113113
// SetNotificationPolicy sets the notification policy tree.
114-
func (c *Client) SetNotificationPolicy(np *NotificationPolicy) error {
114+
func (c *Client) SetNotificationPolicyTree(np *NotificationPolicyTree) error {
115115
req, err := json.Marshal(np)
116116
if err != nil {
117117
return err
118118
}
119119
return c.request("PUT", "/api/v1/provisioning/policies", nil, bytes.NewBuffer(req), nil)
120120
}
121+
122+
func (c *Client) ResetNotificationPolicyTree() error {
123+
return c.request("DELETE", "/api/v1/provisioning/policies", nil, nil, nil)
124+
}

alerting_notification_policy_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestNotificationPolicies(t *testing.T) {
1111
server, client := gapiTestTools(t, 200, notificationPolicyJSON)
1212
defer server.Close()
1313

14-
np, err := client.NotificationPolicy()
14+
np, err := client.NotificationPolicyTree()
1515

1616
if err != nil {
1717
t.Error(err)
@@ -30,16 +30,27 @@ func TestNotificationPolicies(t *testing.T) {
3030
defer server.Close()
3131
np := createNotificationPolicy()
3232

33-
err := client.SetNotificationPolicy(&np)
33+
err := client.SetNotificationPolicyTree(&np)
34+
35+
if err != nil {
36+
t.Error(err)
37+
}
38+
})
39+
40+
t.Run("reset policy tree succeeds", func(t *testing.T) {
41+
server, client := gapiTestTools(t, 200, notificationPolicyJSON)
42+
defer server.Close()
43+
44+
err := client.ResetNotificationPolicyTree()
3445

3546
if err != nil {
3647
t.Error(err)
3748
}
3849
})
3950
}
4051

41-
func createNotificationPolicy() NotificationPolicy {
42-
return NotificationPolicy{
52+
func createNotificationPolicy() NotificationPolicyTree {
53+
return NotificationPolicyTree{
4354
Receiver: "grafana-default-email",
4455
GroupBy: []string{"asdfasdf", "alertname"},
4556
Routes: []SpecificPolicy{

0 commit comments

Comments
 (0)