|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "net/url" |
| 10 | +) |
| 11 | + |
| 12 | +// Annotation represents a Grafana API Annotation |
| 13 | +type Annotation struct { |
| 14 | + ID int64 `json:"id,omitempty"` |
| 15 | + AlertID int64 `json:"alertId,omitempty"` |
| 16 | + DashboardID int64 `json:"dashboardId"` |
| 17 | + PanelID int64 `json:"panelId"` |
| 18 | + UserID int64 `json:"userId,omitempty"` |
| 19 | + UserName string `json:"userName,omitempty"` |
| 20 | + NewState string `json:"newState,omitempty"` |
| 21 | + PrevState string `json:"prevState,omitempty"` |
| 22 | + Time int64 `json:"time"` |
| 23 | + TimeEnd int64 `json:"timeEnd,omitempty"` |
| 24 | + Text string `json:"text"` |
| 25 | + Metric string `json:"metric,omitempty"` |
| 26 | + RegionID int64 `json:"regionId,omitempty"` |
| 27 | + Type string `json:"type,omitempty"` |
| 28 | + Tags []string `json:"tags,omitempty"` |
| 29 | + IsRegion bool `json:"isRegion,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +// GraphiteAnnotation represents a Grafana API annotation in Graphite format |
| 33 | +type GraphiteAnnotation struct { |
| 34 | + What string `json:"what"` |
| 35 | + When int64 `json:"when"` |
| 36 | + Data string `json:"data"` |
| 37 | + Tags []string `json:"tags,omitempty"` |
| 38 | +} |
| 39 | + |
| 40 | +// Annotations fetches the annotations queried with the params it's passed |
| 41 | +func (c *Client) Annotations(params url.Values) ([]Annotation, error) { |
| 42 | + req, err := c.newRequest("GET", "/api/annotation", params, nil) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + resp, err := c.Do(req) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + if resp.StatusCode != 200 { |
| 52 | + return nil, errors.New(resp.Status) |
| 53 | + } |
| 54 | + |
| 55 | + data, err := ioutil.ReadAll(resp.Body) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + result := []Annotation{} |
| 61 | + err = json.Unmarshal(data, &result) |
| 62 | + return result, err |
| 63 | +} |
| 64 | + |
| 65 | +// NewAnnotation creates a new annotation with the Annotation it is passed |
| 66 | +func (c *Client) NewAnnotation(a *Annotation) (int64, error) { |
| 67 | + data, err := json.Marshal(a) |
| 68 | + if err != nil { |
| 69 | + return 0, err |
| 70 | + } |
| 71 | + req, err := c.newRequest("POST", "/api/annotations", nil, bytes.NewBuffer(data)) |
| 72 | + if err != nil { |
| 73 | + return 0, err |
| 74 | + } |
| 75 | + |
| 76 | + resp, err := c.Do(req) |
| 77 | + if err != nil { |
| 78 | + return 0, err |
| 79 | + } |
| 80 | + if resp.StatusCode != 200 { |
| 81 | + return 0, errors.New(resp.Status) |
| 82 | + } |
| 83 | + |
| 84 | + data, err = ioutil.ReadAll(resp.Body) |
| 85 | + if err != nil { |
| 86 | + return 0, err |
| 87 | + } |
| 88 | + |
| 89 | + result := struct { |
| 90 | + ID int64 `json:"id"` |
| 91 | + }{} |
| 92 | + err = json.Unmarshal(data, &result) |
| 93 | + return result.ID, err |
| 94 | +} |
| 95 | + |
| 96 | +// NewGraphiteAnnotation creates a new annotation with the GraphiteAnnotation it is passed |
| 97 | +func (c *Client) NewGraphiteAnnotation(gfa *GraphiteAnnotation) (int64, error) { |
| 98 | + data, err := json.Marshal(gfa) |
| 99 | + if err != nil { |
| 100 | + return 0, err |
| 101 | + } |
| 102 | + req, err := c.newRequest("POST", "/api/annotations/graphite", nil, bytes.NewBuffer(data)) |
| 103 | + if err != nil { |
| 104 | + return 0, err |
| 105 | + } |
| 106 | + |
| 107 | + resp, err := c.Do(req) |
| 108 | + if err != nil { |
| 109 | + return 0, err |
| 110 | + } |
| 111 | + if resp.StatusCode != 200 { |
| 112 | + return 0, errors.New(resp.Status) |
| 113 | + } |
| 114 | + |
| 115 | + data, err = ioutil.ReadAll(resp.Body) |
| 116 | + if err != nil { |
| 117 | + return 0, err |
| 118 | + } |
| 119 | + |
| 120 | + result := struct { |
| 121 | + ID int64 `json:"id"` |
| 122 | + }{} |
| 123 | + err = json.Unmarshal(data, &result) |
| 124 | + return result.ID, err |
| 125 | +} |
| 126 | + |
| 127 | +// UpdateAnnotation updates all properties an existing annotation with the Annotation it is passed. |
| 128 | +func (c *Client) UpdateAnnotation(id int64, a *Annotation) (string, error) { |
| 129 | + path := fmt.Sprintf("/api/annotations/%d", id) |
| 130 | + data, err := json.Marshal(a) |
| 131 | + if err != nil { |
| 132 | + return "", err |
| 133 | + } |
| 134 | + req, err := c.newRequest("PUT", path, nil, bytes.NewBuffer(data)) |
| 135 | + if err != nil { |
| 136 | + return "", err |
| 137 | + } |
| 138 | + |
| 139 | + resp, err := c.Do(req) |
| 140 | + if err != nil { |
| 141 | + return "", err |
| 142 | + } |
| 143 | + if resp.StatusCode != 200 { |
| 144 | + return "", errors.New(resp.Status) |
| 145 | + } |
| 146 | + |
| 147 | + data, err = ioutil.ReadAll(resp.Body) |
| 148 | + if err != nil { |
| 149 | + return "", err |
| 150 | + } |
| 151 | + |
| 152 | + result := struct { |
| 153 | + Message string `json:"message"` |
| 154 | + }{} |
| 155 | + err = json.Unmarshal(data, &result) |
| 156 | + return result.Message, err |
| 157 | +} |
| 158 | + |
| 159 | +// PatchAnnotation updates one or more properties of an existing annotation that matches the specified ID. |
| 160 | +func (c *Client) PatchAnnotation(id int64, a *Annotation) (string, error) { |
| 161 | + path := fmt.Sprintf("/api/annotations/%d", id) |
| 162 | + data, err := json.Marshal(a) |
| 163 | + if err != nil { |
| 164 | + return "", err |
| 165 | + } |
| 166 | + req, err := c.newRequest("PATCH", path, nil, bytes.NewBuffer(data)) |
| 167 | + if err != nil { |
| 168 | + return "", err |
| 169 | + } |
| 170 | + |
| 171 | + resp, err := c.Do(req) |
| 172 | + if err != nil { |
| 173 | + return "", err |
| 174 | + } |
| 175 | + if resp.StatusCode != 200 { |
| 176 | + return "", errors.New(resp.Status) |
| 177 | + } |
| 178 | + |
| 179 | + data, err = ioutil.ReadAll(resp.Body) |
| 180 | + if err != nil { |
| 181 | + return "", err |
| 182 | + } |
| 183 | + |
| 184 | + result := struct { |
| 185 | + Message string `json:"message"` |
| 186 | + }{} |
| 187 | + err = json.Unmarshal(data, &result) |
| 188 | + return result.Message, err |
| 189 | +} |
| 190 | + |
| 191 | +// DeleteAnnotation deletes the annotation of the ID it is passed |
| 192 | +func (c *Client) DeleteAnnotation(id int64) (string, error) { |
| 193 | + path := fmt.Sprintf("/api/annotations/%d", id) |
| 194 | + req, err := c.newRequest("DELETE", path, nil, bytes.NewBuffer(nil)) |
| 195 | + if err != nil { |
| 196 | + return "", err |
| 197 | + } |
| 198 | + |
| 199 | + resp, err := c.Do(req) |
| 200 | + if err != nil { |
| 201 | + return "", err |
| 202 | + } |
| 203 | + if resp.StatusCode != 200 { |
| 204 | + return "", errors.New(resp.Status) |
| 205 | + } |
| 206 | + |
| 207 | + data, err := ioutil.ReadAll(resp.Body) |
| 208 | + if err != nil { |
| 209 | + return "", err |
| 210 | + } |
| 211 | + |
| 212 | + result := struct { |
| 213 | + Message string `json:"message"` |
| 214 | + }{} |
| 215 | + err = json.Unmarshal(data, &result) |
| 216 | + return result.Message, err |
| 217 | +} |
| 218 | + |
| 219 | +// DeleteAnnotationByRegionID deletes the annotation corresponding to the region ID it is passed |
| 220 | +func (c *Client) DeleteAnnotationByRegionID(id int64) (string, error) { |
| 221 | + path := fmt.Sprintf("/api/annotations/region/%d", id) |
| 222 | + req, err := c.newRequest("DELETE", path, nil, bytes.NewBuffer(nil)) |
| 223 | + if err != nil { |
| 224 | + return "", err |
| 225 | + } |
| 226 | + |
| 227 | + resp, err := c.Do(req) |
| 228 | + if err != nil { |
| 229 | + return "", err |
| 230 | + } |
| 231 | + if resp.StatusCode != 200 { |
| 232 | + return "", errors.New(resp.Status) |
| 233 | + } |
| 234 | + |
| 235 | + data, err := ioutil.ReadAll(resp.Body) |
| 236 | + if err != nil { |
| 237 | + return "", err |
| 238 | + } |
| 239 | + |
| 240 | + result := struct { |
| 241 | + Message string `json:"message"` |
| 242 | + }{} |
| 243 | + err = json.Unmarshal(data, &result) |
| 244 | + return result.Message, err |
| 245 | +} |
0 commit comments