-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
dashboards.go
222 lines (181 loc) · 5.61 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
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
package grabana
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/K-Phoen/grabana/dashboard"
"github.com/K-Phoen/sdk"
)
// ErrDashboardNotFound is returned when the given dashboard can not be found.
var ErrDashboardNotFound = errors.New("dashboard not found")
// Dashboard represents a Grafana dashboard.
type Dashboard struct {
ID int `json:"id"`
UID string `json:"uid"`
Title string `json:"title"`
URL string `json:"url"`
Tags []string `json:"tags"`
IsStarred bool `json:"isStarred"`
FolderID int `json:"folderId"`
FolderUID string `json:"folderUid"`
FolderTitle string `json:"folderTitle"`
FolderURL string `json:"folderUrl"`
}
// GetDashboardByTitle finds a dashboard, given its title.
func (client *Client) GetDashboardByTitle(ctx context.Context, title string) (*Dashboard, error) {
resp, err := client.get(ctx, "/api/search?type=dash-db&query="+url.QueryEscape(title))
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, client.httpError(resp)
}
var dashboards []Dashboard
if err := decodeJSON(resp.Body, &dashboards); err != nil {
return nil, err
}
if len(dashboards) == 0 {
return nil, ErrDashboardNotFound
}
for i := range dashboards {
if strings.EqualFold(dashboards[i].Title, title) {
return &dashboards[i], nil
}
}
return nil, ErrDashboardNotFound
}
// rawDashboardByUID finds a dashboard, given its UID.
func (client *Client) rawDashboardByUID(ctx context.Context, uid string) (*sdk.Board, error) {
resp, err := client.get(ctx, "/api/dashboards/uid/"+url.PathEscape(uid))
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusNotFound {
return nil, ErrDashboardNotFound
}
if resp.StatusCode != http.StatusOK {
return nil, client.httpError(resp)
}
response := struct {
Board sdk.Board `json:"dashboard"`
}{}
if err := decodeJSON(resp.Body, &response); err != nil {
return nil, err
}
return &response.Board, nil
}
// UpsertDashboard creates or replaces a dashboard, in the given folder.
func (client *Client) UpsertDashboard(ctx context.Context, folder *Folder, builder dashboard.Builder) (*Dashboard, error) {
// first pass: save the new dashboard
dashboardModel, err := client.persistDashboard(ctx, folder, builder)
if err != nil {
return nil, err
}
dashboardFromGrafana, err := client.rawDashboardByUID(ctx, dashboardModel.UID)
if err != nil {
return nil, err
}
// second pass: delete existing alerts associated to that dashboard
alertRefs, err := client.listAlertsForDashboard(ctx, dashboardModel.UID)
if err != nil {
return nil, fmt.Errorf("could not prepare deletion of previous alerts for dashboard: %w", err)
}
for _, ref := range alertRefs {
if err := client.DeleteAlertGroup(ctx, ref.Namespace, ref.RuleGroup); err != nil {
return nil, fmt.Errorf("could not delete previous alerts for dashboard: %w", err)
}
}
// third pass: create new alerts
alerts := builder.Alerts()
// If there are no alerts to create, we can return early
if len(alerts) == 0 {
return dashboardModel, nil
}
datasourcesMap, err := client.datasourcesUIDMap(ctx)
if err != nil {
return nil, err
}
for i := range alerts {
alert := *alerts[i]
alert.HookDashboardUID(dashboardFromGrafana.UID)
alert.HookPanelID(panelIDByTitle(dashboardFromGrafana, alert.Builder.Name))
if err := client.AddAlert(ctx, folder.Title, alert, datasourcesMap); err != nil {
return nil, fmt.Errorf("could not add new alerts for dashboard: %w", err)
}
}
return dashboardModel, nil
}
func (client *Client) persistDashboard(ctx context.Context, folder *Folder, builder dashboard.Builder) (*Dashboard, error) {
buf, err := json.Marshal(struct {
Dashboard *sdk.Board `json:"dashboard"`
FolderID uint `json:"folderId"`
Overwrite bool `json:"overwrite"`
}{
Dashboard: builder.Internal(),
FolderID: folder.ID,
Overwrite: true,
})
if err != nil {
return nil, err
}
resp, err := client.sendJSON(ctx, http.MethodPost, "/api/dashboards/db", buf)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, client.httpError(resp)
}
var model Dashboard
if err := decodeJSON(resp.Body, &model); err != nil {
return nil, err
}
return &model, nil
}
// DeleteDashboard deletes a dashboard given its UID.
func (client *Client) DeleteDashboard(ctx context.Context, uid string) error {
// first: delete existing alerts associated to that dashboard
alertRefs, err := client.listAlertsForDashboard(ctx, uid)
if err != nil {
return fmt.Errorf("could not prepare deletion of alerts for dashboard: %w", err)
}
for _, ref := range alertRefs {
if err := client.DeleteAlertGroup(ctx, ref.Namespace, ref.RuleGroup); err != nil {
return fmt.Errorf("could not delete alerts for dashboard: %w", err)
}
}
// then: delete the dashboard itself
resp, err := client.delete(ctx, "/api/dashboards/uid/"+uid)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusNotFound {
return ErrDashboardNotFound
}
if resp.StatusCode != http.StatusOK {
return client.httpError(resp)
}
return nil
}
func panelIDByTitle(board *sdk.Board, title string) string {
for _, row := range board.Rows {
for _, panel := range row.Panels {
if panel.Title == title {
return fmt.Sprintf("%d", panel.ID)
}
}
}
for _, panel := range board.Panels {
if panel.Title == title {
return fmt.Sprintf("%d", panel.ID)
}
}
return ""
}