forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotation.go
155 lines (128 loc) · 3.95 KB
/
annotation.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package gapi
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
)
// Annotation represents a Grafana API Annotation
type Annotation struct {
ID int64 `json:"id,omitempty"`
AlertID int64 `json:"alertId,omitempty"`
DashboardID int64 `json:"dashboardId"`
PanelID int64 `json:"panelId"`
UserID int64 `json:"userId,omitempty"`
UserName string `json:"userName,omitempty"`
NewState string `json:"newState,omitempty"`
PrevState string `json:"prevState,omitempty"`
Time int64 `json:"time"`
TimeEnd int64 `json:"timeEnd,omitempty"`
Text string `json:"text"`
Metric string `json:"metric,omitempty"`
RegionID int64 `json:"regionId,omitempty"`
Type string `json:"type,omitempty"`
Tags []string `json:"tags,omitempty"`
IsRegion bool `json:"isRegion,omitempty"`
}
// GraphiteAnnotation represents a Grafana API annotation in Graphite format
type GraphiteAnnotation struct {
What string `json:"what"`
When int64 `json:"when"`
Data string `json:"data"`
Tags []string `json:"tags,omitempty"`
}
// Annotations fetches the annotations queried with the params it's passed
func (c *Client) Annotations(params url.Values) ([]Annotation, error) {
result := []Annotation{}
err := c.request("GET", "/api/annotation", params, nil, &result)
if err != nil {
return nil, err
}
return result, err
}
// NewAnnotation creates a new annotation with the Annotation it is passed
func (c *Client) NewAnnotation(a *Annotation) (int64, error) {
data, err := json.Marshal(a)
if err != nil {
return 0, err
}
result := struct {
ID int64 `json:"id"`
}{}
err = c.request("POST", "/api/annotations", nil, bytes.NewBuffer(data), &result)
if err != nil {
return 0, err
}
return result.ID, err
}
// NewGraphiteAnnotation creates a new annotation with the GraphiteAnnotation it is passed
func (c *Client) NewGraphiteAnnotation(gfa *GraphiteAnnotation) (int64, error) {
data, err := json.Marshal(gfa)
if err != nil {
return 0, err
}
result := struct {
ID int64 `json:"id"`
}{}
err = c.request("POST", "/api/annotations/graphite", nil, bytes.NewBuffer(data), &result)
if err != nil {
return 0, err
}
return result.ID, err
}
// UpdateAnnotation updates all properties an existing annotation with the Annotation it is passed.
func (c *Client) UpdateAnnotation(id int64, a *Annotation) (string, error) {
path := fmt.Sprintf("/api/annotations/%d", id)
data, err := json.Marshal(a)
if err != nil {
return "", err
}
result := struct {
Message string `json:"message"`
}{}
err = c.request("PUT", path, nil, bytes.NewBuffer(data), &result)
if err != nil {
return "", err
}
return result.Message, err
}
// PatchAnnotation updates one or more properties of an existing annotation that matches the specified ID.
func (c *Client) PatchAnnotation(id int64, a *Annotation) (string, error) {
path := fmt.Sprintf("/api/annotations/%d", id)
data, err := json.Marshal(a)
if err != nil {
return "", err
}
result := struct {
Message string `json:"message"`
}{}
err = c.request("PATCH", path, nil, bytes.NewBuffer(data), &result)
if err != nil {
return "", err
}
return result.Message, err
}
// DeleteAnnotation deletes the annotation of the ID it is passed
func (c *Client) DeleteAnnotation(id int64) (string, error) {
path := fmt.Sprintf("/api/annotations/%d", id)
result := struct {
Message string `json:"message"`
}{}
err := c.request("DELETE", path, nil, bytes.NewBuffer(nil), &result)
if err != nil {
return "", err
}
return result.Message, err
}
// DeleteAnnotationByRegionID deletes the annotation corresponding to the region ID it is passed
func (c *Client) DeleteAnnotationByRegionID(id int64) (string, error) {
path := fmt.Sprintf("/api/annotations/region/%d", id)
result := struct {
Message string `json:"message"`
}{}
err := c.request("DELETE", path, nil, bytes.NewBuffer(nil), &result)
if err != nil {
return "", err
}
return result.Message, err
}