forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.go
138 lines (117 loc) · 3.87 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
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"`
}
// 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"`
}
// DashboardSearchResponse represents the Grafana API dashboard search response.
type DashboardSearchResponse struct {
ID uint `json:"id"`
UID string `json:"uid"`
Title string `json:"title"`
URI string `json:"uri"`
URL string `json:"url"`
Slug string `json:"slug"`
Type string `json:"type"`
Tags []string `json:"tags"`
IsStarred bool `json:"isStarred"`
FolderID uint `json:"folderId"`
FolderUID string `json:"folderUid"`
FolderTitle string `json:"folderTitle"`
FolderURL string `json:"folderUrl"`
}
// Dashboard represents a Grafana dashboard.
type Dashboard struct {
Meta DashboardMeta `json:"meta"`
Model map[string]interface{} `json:"dashboard"`
Folder int64 `json:"folderId"`
Overwrite bool `json:"overwrite"`
}
// 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 Grafana dashboards.
func (c *Client) Dashboards() ([]DashboardSearchResponse, error) {
dashboards := make([]DashboardSearchResponse, 0)
query := url.Values{}
// search only dashboards
query.Add("type", "dash-db")
err := c.request("GET", "/api/search", query, nil, &dashboards)
if err != nil {
return nil, err
}
return dashboards, err
}
// 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))
}
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.Folder = 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)
}