forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dashboard.go
158 lines (130 loc) · 4.25 KB
/
dashboard.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
package gapi
import (
"encoding/json"
"fmt"
"net/url"
)
// DashboardMeta represents Grafana dashboard meta.
type DashboardMeta struct {
IsStarred bool `json:"isStarred"`
Slug string `json:"slug"`
Folder int64 `json:"folderId"`
FolderUID string `json:"folderUid"`
URL string `json:"url"`
}
// DashboardSaveResponse represents the Grafana API response to creating or saving a dashboard.
type DashboardSaveResponse struct {
Slug string `json:"slug"`
ID int64 `json:"id"`
UID string `json:"uid"`
Status string `json:"status"`
Version int64 `json:"version"`
}
// Dashboard represents a Grafana dashboard.
type Dashboard struct {
Model map[string]interface{} `json:"dashboard"`
FolderID int64 `json:"folderId"`
// This field is read-only. It is not used when creating a new dashboard.
Meta DashboardMeta `json:"meta"`
// These fields are only used when creating a new dashboard, they will always be empty when getting a dashboard.
Overwrite bool `json:"overwrite,omitempty"`
Message string `json:"message,omitempty"`
FolderUID string `json:"folderUid,omitempty"`
}
// SaveDashboard is a deprecated method for saving a Grafana dashboard. Use NewDashboard.
// Deprecated: Use NewDashboard instead.
func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*DashboardSaveResponse, error) {
wrapper := map[string]interface{}{
"dashboard": model,
"overwrite": overwrite,
}
data, err := json.Marshal(wrapper)
if err != nil {
return nil, err
}
result := &DashboardSaveResponse{}
err = c.request("POST", "/api/dashboards/db", nil, data, &result)
if err != nil {
return nil, err
}
return result, err
}
// NewDashboard creates a new Grafana dashboard.
func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, error) {
data, err := json.Marshal(dashboard)
if err != nil {
return nil, err
}
result := &DashboardSaveResponse{}
err = c.request("POST", "/api/dashboards/db", nil, data, &result)
if err != nil {
return nil, err
}
return result, err
}
// Dashboards fetches and returns all dashboards.
func (c *Client) Dashboards() ([]FolderDashboardSearchResponse, error) {
const limit = 1000
var (
page = 0
newDashboards []FolderDashboardSearchResponse
dashboards []FolderDashboardSearchResponse
query = make(url.Values)
)
query.Set("type", "dash-db")
query.Set("limit", fmt.Sprint(limit))
for {
page++
query.Set("page", fmt.Sprint(page))
if err := c.request("GET", "/api/search", query, nil, &newDashboards); err != nil {
return nil, err
}
dashboards = append(dashboards, newDashboards...)
if len(newDashboards) < limit {
return dashboards, nil
}
}
}
// Dashboard will be removed.
// Deprecated: Starting from Grafana v5.0. Use DashboardByUID instead.
func (c *Client) Dashboard(slug string) (*Dashboard, error) {
return c.dashboard(fmt.Sprintf("/api/dashboards/db/%s", slug))
}
// DashboardByUID gets a dashboard by UID.
func (c *Client) DashboardByUID(uid string) (*Dashboard, error) {
return c.dashboard(fmt.Sprintf("/api/dashboards/uid/%s", uid))
}
// DashboardsByIDs uses the folder and dashboard search endpoint to find
// dashboards by list of dashboard IDs.
func (c *Client) DashboardsByIDs(ids []int64) ([]FolderDashboardSearchResponse, error) {
dashboardIdsJSON, err := json.Marshal(ids)
if err != nil {
return nil, err
}
params := url.Values{
"type": {"dash-db"},
"dashboardIds": {string(dashboardIdsJSON)},
}
return c.FolderDashboardSearch(params)
}
func (c *Client) dashboard(path string) (*Dashboard, error) {
result := &Dashboard{}
err := c.request("GET", path, nil, nil, &result)
if err != nil {
return nil, err
}
result.FolderID = result.Meta.Folder
return result, err
}
// DeleteDashboard will be removed.
// Deprecated: Starting from Grafana v5.0. Use DeleteDashboardByUID instead.
func (c *Client) DeleteDashboard(slug string) error {
return c.deleteDashboard(fmt.Sprintf("/api/dashboards/db/%s", slug))
}
// DeleteDashboardByUID deletes a dashboard by UID.
func (c *Client) DeleteDashboardByUID(uid string) error {
return c.deleteDashboard(fmt.Sprintf("/api/dashboards/uid/%s", uid))
}
func (c *Client) deleteDashboard(path string) error {
return c.request("DELETE", path, nil, nil, nil)
}