forked from nytm/go-grafana-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.go
72 lines (59 loc) · 1.61 KB
/
playlist.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
package gapi
import (
"bytes"
"encoding/json"
"fmt"
)
// PlaylistItem represents a Grafana playlist item.
type PlaylistItem struct {
Type string `json:"type"`
Value string `json:"value"`
Order int `json:"order"`
Title string `json:"title"`
}
// Playlist represents a Grafana playlist.
type Playlist struct {
Id int `json:"id"`
Name string `json:"name"`
Interval string `json:"interval"`
Items []PlaylistItem `json:"items"`
}
// Playlist fetches and returns a Grafana playlist.
func (c *Client) Playlist(id int) (*Playlist, error) {
path := fmt.Sprintf("/api/playlists/%d", id)
playlist := &Playlist{}
err := c.request("GET", path, nil, nil, playlist)
if err != nil {
return nil, err
}
return playlist, nil
}
// NewPlaylist creates a new Grafana playlist.
func (c *Client) NewPlaylist(playlist Playlist) (int, error) {
data, err := json.Marshal(playlist)
if err != nil {
return 0, err
}
result := struct {
Id int
}{}
err = c.request("POST", "/api/playlists", nil, bytes.NewBuffer(data), &result)
if err != nil {
return 0, err
}
return result.Id, nil
}
// UpdatePlaylist updates a Grafana playlist.
func (c *Client) UpdatePlaylist(playlist Playlist) error {
path := fmt.Sprintf("/api/playlists/%d", playlist.Id)
data, err := json.Marshal(playlist)
if err != nil {
return err
}
return c.request("PUT", path, nil, bytes.NewBuffer(data), nil)
}
// DeletePlaylist deletes the Grafana playlist whose ID it's passed.
func (c *Client) DeletePlaylist(id int) error {
path := fmt.Sprintf("/api/playlists/%d", id)
return c.request("DELETE", path, nil, nil, nil)
}