-
Notifications
You must be signed in to change notification settings - Fork 7
/
client_5.x.go
460 lines (428 loc) · 14.1 KB
/
client_5.x.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package gografana
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
const grafanaOK string = "success"
type GrafanaClient_5_0 struct {
basicAddress string
client *http.Client
authenticator Authenticator
httpProxy string
}
type NewDashboardError struct {
Err error
Status string
}
func (e NewDashboardError) Error() string {
return fmt.Sprintf("Internal Error: %s, Status: %s", e.Err.Error(), e.Status)
}
func (gc *GrafanaClient_5_0) initClient() error {
if gc.client != nil {
return nil
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
if gc.httpProxy != "" {
proxyUrl, err := url.Parse(gc.httpProxy)
if err != nil {
return err
}
tr.Proxy = http.ProxyURL(proxyUrl)
}
gc.client = &http.Client{Transport: tr}
return nil
}
func (gc *GrafanaClient_5_0) GetAllDashboards() ([]Board, error) {
urlPath := fmt.Sprintf("%s/api/search?type=dash-db", gc.basicAddress)
req, err := http.NewRequest("GET", urlPath, nil)
if err != nil {
return nil, err
}
bodyData, err := gc.getHTTPResponse(req, "GetAllDashboards(api/search?type=dash-db)")
if err != nil {
return nil, err
}
var boards []Board
err = json.Unmarshal(bodyData, &boards)
if err != nil {
return nil, fmt.Errorf("Unmarshal response body failed while calling to API GetAllDashboards(api/search?type=dash-db), error: %s", err.Error())
}
return boards, nil
}
func (gc *GrafanaClient_5_0) GetDashboardsByTitleAndFolderId(title string, folderId int) ([]Board, error) {
urlPath := fmt.Sprintf("%s/api/search?query=%s&folderIds=%s", gc.basicAddress, title, strconv.Itoa(folderId))
req, err := http.NewRequest("GET", urlPath, nil)
if err != nil {
return nil, err
}
bodyData, err := gc.getHTTPResponse(req, "GetDashboardsByTitleAndFolderId(api/search?query=&folderIds=)")
if err != nil {
return nil, err
}
var boards []Board
err = json.Unmarshal(bodyData, &boards)
if err != nil {
return nil, fmt.Errorf("Unmarshal response body failed while calling to API GetDashboardsByTitleAndFolderId(api/search?query=&folerIds=), error: %s", err.Error())
}
return boards, nil
}
func (gc *GrafanaClient_5_0) GetDashboardsByFolderId(folderId int) ([]Board, error) {
urlPath := fmt.Sprintf("%s/api/search?folderIds=%d", gc.basicAddress, folderId)
req, err := http.NewRequest("GET", urlPath, nil)
if err != nil {
return nil, err
}
bodyData, err := gc.getHTTPResponse(req, "GetAllDashboards(api/search?type=folderIds)")
if err != nil {
return nil, err
}
var boards []Board
err = json.Unmarshal(bodyData, &boards)
if err != nil {
return nil, fmt.Errorf("Unmarshal response body failed while calling to API GetAllDashboards(api/search?type=folderIds), error: %s", err.Error())
}
return boards, nil
}
func (gc *GrafanaClient_5_0) IsBoardExists(title string) (bool, *Board, error) {
boards, err := gc.GetAllDashboards()
if err != nil {
return false, nil, err
}
for _, v := range boards {
if v.Title == title {
return true, &v, nil
}
}
return false, nil, nil
}
func (gc *GrafanaClient_5_0) NewDashboard(board *Board, folderId uint, overwrite bool) (*Board, error) {
if board.Timezone == "" {
board.Timezone = "browser"
}
bodyReq := CreateDashboardRequest{Board: *board, Overwrite: overwrite, FolderId: folderId}
bodyStr, err := json.Marshal(bodyReq)
if err != nil {
return board, err
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/dashboards/db", gc.basicAddress), strings.NewReader(string(bodyStr)))
if err != nil {
return board, err
}
rspBody, err := gc.getHTTPResponse(req, "NewDashboard(api/dashboards/db)")
if err != nil {
return board, err
}
var rsp CreateDashboardResponse
err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return board, fmt.Errorf("Unmarshal response body failed while calling to API NewDashboard(api/dashboards/db), error: %s", err.Error())
}
if rsp.Status != grafanaOK {
return board, &NewDashboardError{Err: fmt.Errorf("Grafana operation failed while calling to API NewDashboard(api/dashboards/db), error: %s", rsp.Message), Status: rsp.Status}
}
board.ID = rsp.ID
board.UID = rsp.UID
board.Url = rsp.Url //TODO: ??
board.Version = rsp.Version
return board, nil
}
func (gc *GrafanaClient_5_0) CreateAPIKey(name string, role string, secondsToLive int) (string, error) {
body := map[string]interface{}{
"name": name,
"role": role,
"secondsToLive": secondsToLive,
}
b, err := json.Marshal(body)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/auth/keys", gc.basicAddress), strings.NewReader(string(b)))
rspBody, err := gc.getHTTPResponse(req, "CreateAPIKey(api/auth/keys)")
if err != nil {
return "", err
}
var rsp CreateAPIKeyResponse
err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return "", fmt.Errorf("unmarshal response body failed while calling to API CreateAPIKey(api/auth/keys), error: %s", err.Error())
}
return rsp.Key, nil
}
func (gc *GrafanaClient_5_0) FindAllAPIKeys() ([]APIKey, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/auth/keys", gc.basicAddress), nil)
rspBody, err := gc.getHTTPResponse(req, "GetAllAPIKeys(api/auth/keys)")
if err != nil {
return nil, err
}
var apiKeys []APIKey
err = json.Unmarshal(rspBody, &apiKeys)
if err != nil {
return nil, fmt.Errorf("Unmarshal response body failed while calling to API GetAllAPIKeys(api/auth/keys), error: %s", err.Error())
}
return apiKeys, nil
}
func (gc *GrafanaClient_5_0) DeleteAPIKey(id int) (bool, error) {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/api/auth/keys/%d", gc.basicAddress, id), nil)
if err != nil {
return false, err
}
_, err = gc.getHTTPResponse(req, "DeleteAPIKey(api/auth/keys/[ID])")
if err != nil {
return false, err
}
return true, nil
}
// Status Codes:
//-------------------
// 200 – Deleted
// 401 – Unauthorized
// 403 – Access denied
// 404 – Not found
func (gc *GrafanaClient_5_0) DeleteDashboard(uid string) (bool, error) {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/api/dashboards/uid/%s", gc.basicAddress, uid), nil)
if err != nil {
return false, err
}
_, err = gc.getHTTPResponse(req, "DeleteDashboard(/api/dashboards/uid/[UID])")
if err != nil {
return false, err
}
return true, nil
}
// Status Codes:
//-------------------
// 200 – Found
// 401 – Unauthorized
// 403 – Access denied
// 404 – Not found
func (gc *GrafanaClient_5_0) GetDashboardDetails(uid string) (*Board, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/dashboards/uid/%s", gc.basicAddress, uid), nil)
if err != nil {
return nil, err
}
bodyData, err := gc.getHTTPResponse(req, "GetDashboardDetails(/api/dashboards/uid/[UID])")
if err != nil {
return nil, err
}
var rsp GetDashboardByUIdResponse
err = json.Unmarshal(bodyData, &rsp)
if err != nil {
return nil, fmt.Errorf("Unmarshal response body failed while calling to API GetDashboardDetails(/api/dashboards/uid/[UID]), error: %s", err.Error())
}
return &rsp.Dashboard, nil
}
func (gc *GrafanaClient_5_0) EnsureFolderExists(folderId int, uid, title string) (int, bool, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/folders/id/%d", gc.basicAddress, folderId), nil)
if err != nil {
return -1, false, err
}
_, statusCode, err := gc.getHTTPResponseWithStatusCode(req, "GetFolderById(/api/folders/id/[FOLDER-ID])")
if err != nil {
return -1, false, err
}
//folder existed.
if statusCode == 200 {
return -1, false, nil
}
//try to create a new folder.
bodyReq := CreateFolderRequest{UID: uid, Title: title}
bodyStr, err := json.Marshal(bodyReq)
if err != nil {
return -1, false, err
}
var bodyData []byte
req, err = http.NewRequest("POST", fmt.Sprintf("%s/api/folders", gc.basicAddress), strings.NewReader(string(bodyStr)))
if err != nil {
return -1, false, err
}
bodyData, err = gc.getHTTPResponse(req, "CreateFolder(/api/folders)")
if err != nil {
return -1, false, err
}
var rsp CreateFolderResponse
err = json.Unmarshal(bodyData, &rsp)
if err != nil {
return -1, false, fmt.Errorf("Unmarshal response body failed while calling to API CreateFolder(/api/folders), error: %s", err.Error())
}
return rsp.ID, true, nil
}
func (gc *GrafanaClient_5_0) getHTTPResponse(req *http.Request, flag string) ([]byte, error) {
err := gc.initClient()
if err != nil {
return nil, err
}
//加入统一授权
gc.authenticator.SetAuthentication(req)
req.Header.Add("Content-Type", "application/json")
rsp, err := gc.client.Do(req)
if err != nil {
return nil, err
}
defer rsp.Body.Close()
bodyData, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, fmt.Errorf("Read response body failed while calling to API %s, error: %s", flag, err.Error())
}
if rsp.StatusCode != 200 {
return nil, fmt.Errorf("Remote API returned Non 200/OK status code in the %s response(%d), body: %s", flag, rsp.StatusCode, string(bodyData))
}
return bodyData, nil
}
func (gc *GrafanaClient_5_0) getHTTPResponseWithStatusCode(req *http.Request, flag string) ([]byte, int, error) {
err := gc.initClient()
if err != nil {
return nil, -1, err
}
//加入统一授权
gc.authenticator.SetAuthentication(req)
req.Header.Add("Content-Type", "application/json")
rsp, err := gc.client.Do(req)
if err != nil {
return nil, -1, err
}
defer rsp.Body.Close()
bodyData, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, -1, fmt.Errorf("Read response body failed while calling to API %s, error: %s", flag, err.Error())
}
if rsp.StatusCode == 404 {
return nil, 404, nil
}
if rsp.StatusCode != 200 {
return nil, rsp.StatusCode, fmt.Errorf("Remote API returned Non 200/OK status code in the %s response(%d), body: %s", flag, rsp.StatusCode, string(bodyData))
}
return bodyData, rsp.StatusCode, nil
}
func (gc *GrafanaClient_5_0) GetAllDataSources() ([]*DataSource, error) {
urlPath := fmt.Sprintf("%s/api/datasources", gc.basicAddress)
req, err := http.NewRequest("GET", urlPath, nil)
if err != nil {
return nil, err
}
bodyData, err := gc.getHTTPResponse(req, "GetAllDataSources(api/datasources)")
if err != nil {
return nil, err
}
var ds []*DataSource
err = json.Unmarshal(bodyData, &ds)
if err != nil {
return nil, fmt.Errorf("Unmarshal response body failed while calling to API GetAllDataSources(api/datasources), error: %s", err.Error())
}
return ds, nil
}
func (gc *GrafanaClient_5_0) GetDashSourceById(id int) (*DataSource, error) {
urlPath := fmt.Sprintf("%s/api/datasources/%d", gc.basicAddress, id)
req, err := http.NewRequest("GET", urlPath, nil)
if err != nil {
return nil, err
}
bodyData, err := gc.getHTTPResponse(req, "GetDashSourceById(api/datasources)")
if err != nil {
return nil, err
}
var ds DataSource
err = json.Unmarshal(bodyData, &ds)
if err != nil {
return nil, fmt.Errorf("Unmarshal response body failed while calling to API GetDashSourceById(api/datasources), error: %s", err.Error())
}
return &ds, nil
}
func (gc *GrafanaClient_5_0) DeleteDashSource(id int) error {
urlPath := fmt.Sprintf("%s/api/datasources/%d", gc.basicAddress, id)
req, err := http.NewRequest("DELETE", urlPath, nil)
if err != nil {
return err
}
_, err = gc.getHTTPResponse(req, "DeleteDashSource(api/datasources)")
return err
}
func (gc *GrafanaClient_5_0) CreateDashSource(ds *DataSource) error {
bodyStr, err := json.Marshal(ds)
if err != nil {
return err
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/datasources", gc.basicAddress), strings.NewReader(string(bodyStr)))
if err != nil {
return err
}
rspBody, statusCode, err := gc.getHTTPResponseWithStatusCode(req, "CreateDashSource(api/datasources)")
if err != nil {
return err
}
if statusCode != 200 {
return fmt.Errorf("HTTP Response != 200 while calling to API CreateDashSource(api/datasources), error: %s", err.Error())
}
var rsp CreateDataSourceResponse
err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return fmt.Errorf("Unmarshal response body failed while calling to API CreateDashSource(api/datasources), error: %s", err.Error())
}
ds.ID = rsp.ID
return nil
}
func (gc *GrafanaClient_5_0) GetAllFolders() ([]Folder, error) {
urlPath := fmt.Sprintf("%s/api/folders?limit=10000", gc.basicAddress)
req, err := http.NewRequest("GET", urlPath, nil)
if err != nil {
return nil, err
}
bodyData, err := gc.getHTTPResponse(req, "GetAllFolders(api/folders?limit=10000)")
if err != nil {
return nil, err
}
var folders []Folder
err = json.Unmarshal(bodyData, &folders)
if err != nil {
return nil, fmt.Errorf("Unmarshal response body failed while calling to API GetAllFolders(api/folders?limit=10000), error: %s", err.Error())
}
return folders, nil
}
func (gc *GrafanaClient_5_0) GetAllNotificationChannels() ([]NotificationChannel, error) {
urlPath := fmt.Sprintf("%s/api/alert-notifications", gc.basicAddress)
req, err := http.NewRequest("GET", urlPath, nil)
if err != nil {
return nil, err
}
bodyData, err := gc.getHTTPResponse(req, "GetAllNotificationChannels(api/alert-notifications)")
if err != nil {
return nil, err
}
var channels []NotificationChannel
err = json.Unmarshal(bodyData, &channels)
if err != nil {
return nil, fmt.Errorf("Unmarshal response body failed while calling to API GetAllNotificationChannels(api/alert-notifications), error: %s", err.Error())
}
return channels, nil
}
func (gc *GrafanaClient_5_0) CreateNotificationChannel(nc *NotificationChannel) error {
bodyStr, err := json.Marshal(nc)
if err != nil {
return err
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/alert-notifications", gc.basicAddress), strings.NewReader(string(bodyStr)))
if err != nil {
return err
}
rspBody, statusCode, err := gc.getHTTPResponseWithStatusCode(req, "CreateNotificationChannel(api/alert-notifications)")
if err != nil {
return err
}
if statusCode != 200 {
return fmt.Errorf("HTTP Response != 200 while calling to API CreateNotificationChannel(api/alert-notifications), error: %s", err.Error())
}
var rsp CreateNotificationChannelResponse
err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return fmt.Errorf("Unmarshal response body failed while calling to API CreateNotificationChannel(api/alert-notifications), error: %s", err.Error())
}
nc.ID = rsp.ID
return nil
}