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

Commit a07c689

Browse files
authored
Merge pull request #97 from grafana/alexweav/contact-points-by-name
Alerting: Add method for querying contact points by name
2 parents f30e403 + 5239257 commit a07c689

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

alerting_contact_point.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"net/url"
78
)
89

910
// ContactPoint represents a Grafana Alerting contact point.
@@ -26,6 +27,18 @@ func (c *Client) ContactPoints() ([]ContactPoint, error) {
2627
return ps, nil
2728
}
2829

30+
// ContactPointsByName fetches contact points with the given name.
31+
func (c *Client) ContactPointsByName(name string) ([]ContactPoint, error) {
32+
ps := make([]ContactPoint, 0)
33+
params := url.Values{}
34+
params.Add("name", name)
35+
err := c.request("GET", "/api/v1/provisioning/contact-points", params, nil, &ps)
36+
if err != nil {
37+
return nil, err
38+
}
39+
return ps, nil
40+
}
41+
2942
// ContactPoint fetches a single contact point, identified by its UID.
3043
func (c *Client) ContactPoint(uid string) (ContactPoint, error) {
3144
ps, err := c.ContactPoints()

alerting_contact_point_test.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,31 @@ func TestContactPoints(t *testing.T) {
1818
}
1919
t.Log(pretty.PrettyFormat(ps))
2020
if len(ps) != 2 {
21-
t.Errorf("wrong number of contact points returned, got %#v", ps)
21+
t.Errorf("wrong number of contact points returned, got %d", len(ps))
2222
}
2323
if ps[0].UID != "" {
24-
t.Errorf("incorrect UID - expected %s on element %d, got %#v", "", 0, ps)
24+
t.Errorf("incorrect UID - expected %s on element %d, got %s", "", 0, ps[0].UID)
2525
}
2626
if ps[1].UID != "rc5r0bjnz" {
27-
t.Errorf("incorrect UID - expected %s on element %d, got %#v", "rc5r0bjnz", 0, ps)
27+
t.Errorf("incorrect UID - expected %s on element %d, got %s", "rc5r0bjnz", 1, ps[1].UID)
28+
}
29+
})
30+
31+
t.Run("get contact points by name succeeds", func(t *testing.T) {
32+
server, client := gapiTestTools(t, 200, getContactPointsQueryJSON)
33+
defer server.Close()
34+
35+
ps, err := client.ContactPointsByName("slack-receiver-1")
36+
37+
if err != nil {
38+
t.Error(err)
39+
}
40+
t.Log(pretty.PrettyFormat(ps))
41+
if len(ps) != 1 {
42+
t.Errorf("wrong number of contact points returned, got %d", len(ps))
43+
}
44+
if ps[0].UID != "rc5r0bjnz" {
45+
t.Errorf("incorrect UID - expected %s on element %d, got %s", "rc5r0bjnz", 0, ps[0].UID)
2846
}
2947
})
3048

@@ -39,7 +57,7 @@ func TestContactPoints(t *testing.T) {
3957
}
4058
t.Log(pretty.PrettyFormat(p))
4159
if p.UID != "rc5r0bjnz" {
42-
t.Errorf("incorrect UID - expected %s got %#v", "rc5r0bjnz", p)
60+
t.Errorf("incorrect UID - expected %s got %s", "rc5r0bjnz", p.UID)
4361
}
4462
})
4563

@@ -132,6 +150,21 @@ const getContactPointsJSON = `
132150
}
133151
]`
134152

153+
const getContactPointsQueryJSON = `
154+
[
155+
{
156+
"uid": "rc5r0bjnz",
157+
"name": "slack-receiver-1",
158+
"type": "slack",
159+
"disableResolveMessage": false,
160+
"settings": {
161+
"recipient": "@foo",
162+
"token": "[REDACTED]",
163+
"url": "[REDACTED]"
164+
}
165+
}
166+
]`
167+
135168
const writeContactPointJSON = `
136169
{
137170
"uid": "rc5r0bjnz",

0 commit comments

Comments
 (0)