@@ -16,15 +16,25 @@ type PlaylistItem struct {
16
16
17
17
// Playlist represents a Grafana playlist.
18
18
type Playlist struct {
19
- ID int `json:"id"`
19
+ ID int `json:"id,omitempty"` // Grafana < 9.0
20
+ UID string `json:"uid,omitempty"` // Grafana >= 9.0
20
21
Name string `json:"name"`
21
22
Interval string `json:"interval"`
22
23
Items []PlaylistItem `json:"items"`
23
24
}
24
25
26
+ // Grafana 9.0+ returns the ID and the UID but uses the UID in the API calls.
27
+ // Grafana <9 only returns the ID.
28
+ func (p * Playlist ) QueryID () string {
29
+ if p .UID != "" {
30
+ return p .UID
31
+ }
32
+ return fmt .Sprintf ("%d" , p .ID )
33
+ }
34
+
25
35
// Playlist fetches and returns a Grafana playlist.
26
- func (c * Client ) Playlist (id int ) (* Playlist , error ) {
27
- path := fmt .Sprintf ("/api/playlists/%d " , id )
36
+ func (c * Client ) Playlist (idOrUID string ) (* Playlist , error ) {
37
+ path := fmt .Sprintf ("/api/playlists/%s " , idOrUID )
28
38
playlist := & Playlist {}
29
39
err := c .request ("GET" , path , nil , nil , playlist )
30
40
if err != nil {
@@ -35,27 +45,25 @@ func (c *Client) Playlist(id int) (*Playlist, error) {
35
45
}
36
46
37
47
// NewPlaylist creates a new Grafana playlist.
38
- func (c * Client ) NewPlaylist (playlist Playlist ) (int , error ) {
48
+ func (c * Client ) NewPlaylist (playlist Playlist ) (string , error ) {
39
49
data , err := json .Marshal (playlist )
40
50
if err != nil {
41
- return 0 , err
51
+ return "" , err
42
52
}
43
53
44
- result := struct {
45
- ID int
46
- }{}
54
+ var result Playlist
47
55
48
56
err = c .request ("POST" , "/api/playlists" , nil , bytes .NewBuffer (data ), & result )
49
57
if err != nil {
50
- return 0 , err
58
+ return "" , err
51
59
}
52
60
53
- return result .ID , nil
61
+ return result .QueryID () , nil
54
62
}
55
63
56
64
// UpdatePlaylist updates a Grafana playlist.
57
65
func (c * Client ) UpdatePlaylist (playlist Playlist ) error {
58
- path := fmt .Sprintf ("/api/playlists/%d " , playlist .ID )
66
+ path := fmt .Sprintf ("/api/playlists/%s " , playlist .QueryID () )
59
67
data , err := json .Marshal (playlist )
60
68
if err != nil {
61
69
return err
@@ -65,8 +73,8 @@ func (c *Client) UpdatePlaylist(playlist Playlist) error {
65
73
}
66
74
67
75
// DeletePlaylist deletes the Grafana playlist whose ID it's passed.
68
- func (c * Client ) DeletePlaylist (id int ) error {
69
- path := fmt .Sprintf ("/api/playlists/%d " , id )
76
+ func (c * Client ) DeletePlaylist (idOrUID string ) error {
77
+ path := fmt .Sprintf ("/api/playlists/%s " , idOrUID )
70
78
71
79
return c .request ("DELETE" , path , nil , nil , nil )
72
80
}
0 commit comments