@@ -57,6 +57,45 @@ type BoardProperties struct {
57
57
FolderURL string `json:"folderUrl"`
58
58
}
59
59
60
+ //RawBoardRequest struct that wraps Board and parameters being sent
61
+ type RawBoardRequest struct {
62
+ Dashboard []byte
63
+ Parameters SetDashboardParams
64
+ }
65
+
66
+ //MarshalJSON serializes the request to match the expectations of the grafana API.
67
+ // Additionally, if preseveID is false, then the dashboard id is set to 0
68
+ func (d RawBoardRequest ) MarshalJSON () ([]byte , error ) {
69
+ var raw []byte
70
+
71
+ if d .Parameters .PreserveId {
72
+ raw = d .Dashboard
73
+ } else {
74
+ var plain map [string ]interface {} = make (map [string ]interface {})
75
+ if err := json .Unmarshal (d .Dashboard , & plain ); err != nil {
76
+ return nil , err
77
+ }
78
+ plain ["id" ] = 0
79
+ raw , _ = json .Marshal (plain )
80
+ }
81
+
82
+ var output struct {
83
+ Dashboard map [string ]interface {} `json:"dashboard"`
84
+ SetDashboardParams
85
+ }
86
+ err := json .Unmarshal (raw , & output .Dashboard )
87
+ if err != nil {
88
+ return nil , err
89
+ }
90
+ output .SetDashboardParams = d .Parameters
91
+ result , err := json .Marshal (& output )
92
+ if err != nil {
93
+ return nil , err
94
+ }
95
+
96
+ return result , nil
97
+ }
98
+
60
99
// GetDashboardByUID loads a dashboard and its metadata from Grafana by dashboard uid.
61
100
//
62
101
// Reflects GET /api/dashboards/uid/:uid API call.
@@ -219,11 +258,12 @@ func (r *Client) Search(ctx context.Context, params ...SearchParam) ([]FoundBoar
219
258
return boards , err
220
259
}
221
260
222
- // SetDashboardParams contains the extra parameteres
261
+ // SetDashboardParams contains the extra parameters
223
262
// that affects where and how the dashboard will be stored
224
263
type SetDashboardParams struct {
225
- FolderID int
226
- Overwrite bool
264
+ FolderID int
265
+ Overwrite bool
266
+ PreserveId bool `json:"-"`
227
267
}
228
268
229
269
// SetDashboard updates existing dashboard or creates a new one.
@@ -271,31 +311,20 @@ func (r *Client) SetDashboard(ctx context.Context, board Board, params SetDashbo
271
311
return resp , nil
272
312
}
273
313
274
- // SetRawDashboard updates existing dashboard or creates a new one.
275
- // Contrary to SetDashboard() it accepts raw JSON instead of Board structure.
276
- // Grafana only can create or update a dashboard in a database. File dashboards
277
- // may be only loaded with HTTP API but not created or updated.
278
- //
279
- // Reflects POST /api/dashboards/db API call.
280
- func (r * Client ) SetRawDashboard (ctx context.Context , raw []byte ) (StatusMessage , error ) {
314
+ //SetRawDashboardWithParam sends the serialized along with request parameters
315
+ func (r * Client ) SetRawDashboardWithParam (ctx context.Context , request RawBoardRequest ) (StatusMessage , error ) {
281
316
var (
282
317
rawResp []byte
283
318
resp StatusMessage
284
319
code int
285
320
err error
286
- buf bytes.Buffer
287
- plain = make (map [string ]interface {})
288
321
)
289
- if err = json .Unmarshal (raw , & plain ); err != nil {
290
- return StatusMessage {}, err
322
+ raw , err := json .Marshal (request )
323
+
324
+ if err != nil {
325
+ return StatusMessage {}, errors .New (err .Error ())
291
326
}
292
- // TODO(axel) fragile place, refactor it
293
- plain ["id" ] = 0
294
- raw , _ = json .Marshal (plain )
295
- buf .WriteString (`{"dashboard":` )
296
- buf .Write (raw )
297
- buf .WriteString (`, "overwrite": true}` )
298
- if rawResp , code , err = r .post (ctx , "api/dashboards/db" , nil , buf .Bytes ()); err != nil {
327
+ if rawResp , code , err = r .post (ctx , "api/dashboards/db" , nil , raw ); err != nil {
299
328
return StatusMessage {}, err
300
329
}
301
330
if err = json .Unmarshal (rawResp , & resp ); err != nil {
@@ -307,6 +336,24 @@ func (r *Client) SetRawDashboard(ctx context.Context, raw []byte) (StatusMessage
307
336
return resp , nil
308
337
}
309
338
339
+ // SetRawDashboard updates existing dashboard or creates a new one.
340
+ // Contrary to SetDashboard() it accepts raw JSON instead of Board structure.
341
+ // Grafana only can create or update a dashboard in a database. File dashboards
342
+ // may be only loaded with HTTP API but not created or updated.
343
+ //
344
+ // Reflects POST /api/dashboards/db API call.
345
+ func (r * Client ) SetRawDashboard (ctx context.Context , raw []byte ) (StatusMessage , error ) {
346
+ defaultParams := SetDashboardParams {
347
+ Overwrite : true ,
348
+ FolderID : DefaultFolderId ,
349
+ }
350
+ request := RawBoardRequest {
351
+ Parameters : defaultParams ,
352
+ Dashboard : raw ,
353
+ }
354
+ return r .SetRawDashboardWithParam (ctx , request )
355
+ }
356
+
310
357
// DeleteDashboard deletes dashboard that selected by slug string.
311
358
// Grafana only can delete a dashboard in a database. File dashboards
312
359
// may be only loaded with HTTP API but not deteled.
0 commit comments