This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerting_contact_point.go
88 lines (76 loc) · 2.45 KB
/
alerting_contact_point.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
)
// ContactPoint represents a Grafana Alerting contact point.
type ContactPoint struct {
UID string `json:"uid,omitempty"`
Name string `json:"name"`
Type string `json:"Type"`
Settings map[string]interface{} `json:"settings"`
DisableResolveMessage bool `json:"disableResolveMessage,omitempty"`
Provenance string `json:"provenance,omitempty"`
}
// ContactPoints fetches all contact points.
func (c *Client) ContactPoints() ([]ContactPoint, error) {
ps := make([]ContactPoint, 0)
err := c.request("GET", "/api/v1/provisioning/contact-points", nil, nil, &ps)
if err != nil {
return nil, err
}
return ps, nil
}
// ContactPointsByName fetches contact points with the given name.
func (c *Client) ContactPointsByName(name string) ([]ContactPoint, error) {
ps := make([]ContactPoint, 0)
params := url.Values{}
params.Add("name", name)
err := c.request("GET", "/api/v1/provisioning/contact-points", params, nil, &ps)
if err != nil {
return nil, err
}
return ps, nil
}
// ContactPoint fetches a single contact point, identified by its UID.
func (c *Client) ContactPoint(uid string) (ContactPoint, error) {
ps, err := c.ContactPoints()
if err != nil {
return ContactPoint{}, err
}
for _, p := range ps {
if p.UID == uid {
return p, nil
}
}
return ContactPoint{}, fmt.Errorf("contact point with uid %s not found", uid)
}
// NewContactPoint creates a new contact point.
func (c *Client) NewContactPoint(p *ContactPoint) (string, error) {
req, err := json.Marshal(p)
if err != nil {
return "", err
}
result := ContactPoint{}
err = c.request("POST", "/api/v1/provisioning/contact-points", nil, bytes.NewBuffer(req), &result)
if err != nil {
return "", err
}
return result.UID, nil
}
// UpdateContactPoint replaces a contact point, identified by contact point's UID.
func (c *Client) UpdateContactPoint(p *ContactPoint) error {
uri := fmt.Sprintf("/api/v1/provisioning/contact-points/%s", p.UID)
req, err := json.Marshal(p)
if err != nil {
return err
}
return c.request("PUT", uri, nil, bytes.NewBuffer(req), nil)
}
// DeleteContactPoint deletes a contact point.
func (c *Client) DeleteContactPoint(uid string) error {
uri := fmt.Sprintf("/api/v1/provisioning/contact-points/%s", uid)
return c.request("DELETE", uri, nil, nil, nil)
}