/
dashboards.go
90 lines (80 loc) · 2.56 KB
/
dashboards.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
package grafana
import (
"fmt"
"net/http"
)
// ListDashboards returns a list of dashboards.
func (c *Client) ListDashboards() ([]Dashboard, error) {
resp, err := c.doRequest(&request{
method: http.MethodGet,
endpoint: SearchEndpoint,
})
if err != nil {
return nil, err
}
var unf []Dashboard
if err := decodeResponse(resp, &unf); err != nil {
// Read the body of the request, ignore the error since we are already in the error state.
return nil, fmt.Errorf("decoding response from request to failed, err -> %v", err)
}
dashboards := unf[:0]
for _, u := range unf {
if u.Type == "dash-db" {
dashboards = append(dashboards, u)
}
}
// Check if we didn't get a result and return an error if true.
if dashboards == nil || len(dashboards) <= 0 {
return make([]Dashboard, 0), nil
}
return dashboards, nil
}
// GetDashboard returns a byte array representation of the dashboard
func (c *Client) GetDashboard(uid string) (DashboardExport, error) {
resp, err := c.doRequest(&request{
method: http.MethodGet,
endpoint: fmt.Sprintf("%s/%s", DashboardsUIDEndpoint, uid),
})
if err != nil {
return DashboardExport{}, err
}
var de DashboardExport
if err := decodeResponse(resp, &de); err != nil {
// Read the body of the request, ignore the error since we are already in the error state.
return DashboardExport{}, fmt.Errorf("decoding response from request to failed, err -> %v", err)
}
return de, nil
}
// CreateDashboard creates a dashboard.
func (c *Client) CreateDashboard(db DashboardRequest) (DashboardResponse, error) {
resp, err := c.doRequest(&request{
method: http.MethodPost,
endpoint: DashboardsImportEndpoint,
data: db,
})
if err != nil {
return DashboardResponse{}, err
}
var dr DashboardResponse
if err := decodeResponse(resp, &dr); err != nil {
// Read the body of the request, ignore the error since we are already in the error state.
return DashboardResponse{}, fmt.Errorf("decoding response from request to failed, err -> %v", err)
}
return dr, nil
}
//DeleteDashboard deletes a dashboard by uid
func (c *Client) DeleteDashboard(uid string) (string, error) {
resp, err := c.doRequest(&request{
method: http.MethodDelete,
endpoint: fmt.Sprintf("%s/%s", DashboardsUIDEndpoint, uid),
})
if err != nil {
return "", err
}
var m map[string]interface{}
if err := decodeResponse(resp, &m); err != nil {
// Read the body of the request, ignore the error since we are already in the error state.
return "", fmt.Errorf("decoding response from request to failed, err -> %v", err)
}
return m["title"].(string), nil
}