forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.go
134 lines (113 loc) · 3.75 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
package gapi
import (
"bytes"
"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"`
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 {
Meta DashboardMeta `json:"meta"`
Model map[string]interface{} `json:"dashboard"`
FolderID int64 `json:"folderId"`
FolderUID string `json:"folderUid"`
Overwrite bool `json:"overwrite"`
// This is only used when creating a new dashboard, it will always be empty when getting a dashboard.
Message string `json:"message"`
}
// 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, bytes.NewBuffer(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, bytes.NewBuffer(data), &result)
if err != nil {
return nil, err
}
return result, err
}
// Dashboards fetches and returns all dashboards.
func (c *Client) Dashboards() ([]FolderDashboardSearchResponse, error) {
params := url.Values{
"type": {"dash-db"},
}
return c.FolderDashboardSearch(params)
}
// 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)
}