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

Commit 4467425

Browse files
authored
Merge pull request #103 from grafana/alexweav/alerting-write-rule
Alerting: Add method for PUTting a whole rule group
2 parents 9c38b6f + c68a60a commit 4467425

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

alerting_alert_rule.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ func (c *Client) AlertRuleGroup(folderUID string, name string) (RuleGroup, error
8080
return result, err
8181
}
8282

83+
// SetAlertRuleGroup overwrites an existing rule group on the server.
84+
func (c *Client) SetAlertRuleGroup(group RuleGroup) error {
85+
folderUID := group.FolderUID
86+
name := group.Title
87+
req, err := json.Marshal(group)
88+
if err != nil {
89+
return err
90+
}
91+
92+
uri := fmt.Sprintf("/api/v1/provisioning/folder/%s/rule-groups/%s", folderUID, name)
93+
return c.request("PUT", uri, nil, bytes.NewBuffer(req), nil)
94+
}
95+
8396
// NewAlertRule creates a new alert rule and returns its UID.
8497
func (c *Client) NewAlertRule(ar *AlertRule) (string, error) {
8598
req, err := json.Marshal(ar)

alerting_alert_rule_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,23 @@ func TestAlertRules(t *testing.T) {
5555
}
5656
})
5757

58+
t.Run("get non-existent rule group fails", func(t *testing.T) {
59+
server, client := gapiTestTools(t, 404, "")
60+
defer server.Close()
61+
62+
group, err := client.AlertRuleGroup("d8-gk06nz", "does not exist")
63+
64+
if err == nil {
65+
t.Errorf("expected error but got nil")
66+
t.Log(pretty.PrettyFormat(group))
67+
}
68+
})
69+
5870
t.Run("create alert rule succeeds", func(t *testing.T) {
5971
server, client := gapiTestTools(t, 201, writeAlertRuleJSON)
6072
defer server.Close()
6173
alertRule := createAlertRule()
74+
6275
uid, err := client.NewAlertRule(&alertRule)
6376

6477
if err != nil {
@@ -69,6 +82,18 @@ func TestAlertRules(t *testing.T) {
6982
}
7083
})
7184

85+
t.Run("set alert rule group succeeds", func(t *testing.T) {
86+
server, client := gapiTestTools(t, 200, getAlertRuleGroupJSON)
87+
defer server.Close()
88+
group := createAlertRuleGroup()
89+
90+
err := client.SetAlertRuleGroup(group)
91+
92+
if err != nil {
93+
t.Error(err)
94+
}
95+
})
96+
7297
t.Run("update alert rule succeeds", func(t *testing.T) {
7398
server, client := gapiTestTools(t, 200, writeAlertRuleJSON)
7499
defer server.Close()
@@ -94,6 +119,15 @@ func TestAlertRules(t *testing.T) {
94119
})
95120
}
96121

122+
func createAlertRuleGroup() RuleGroup {
123+
return RuleGroup{
124+
Title: "eval_group_1",
125+
FolderUID: "project_test",
126+
Interval: 120,
127+
Rules: []AlertRule{createAlertRule()},
128+
}
129+
}
130+
97131
func createAlertRule() AlertRule {
98132
return AlertRule{
99133
Condition: "A",

0 commit comments

Comments
 (0)