@@ -16,15 +16,25 @@ type PlaylistItem struct {
1616
1717// Playlist represents a Grafana playlist.
1818type 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
2021 Name string `json:"name"`
2122 Interval string `json:"interval"`
2223 Items []PlaylistItem `json:"items"`
2324}
2425
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+
2535// 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 )
2838 playlist := & Playlist {}
2939 err := c .request ("GET" , path , nil , nil , playlist )
3040 if err != nil {
@@ -35,27 +45,25 @@ func (c *Client) Playlist(id int) (*Playlist, error) {
3545}
3646
3747// NewPlaylist creates a new Grafana playlist.
38- func (c * Client ) NewPlaylist (playlist Playlist ) (int , error ) {
48+ func (c * Client ) NewPlaylist (playlist Playlist ) (string , error ) {
3949 data , err := json .Marshal (playlist )
4050 if err != nil {
41- return 0 , err
51+ return "" , err
4252 }
4353
44- result := struct {
45- ID int
46- }{}
54+ var result Playlist
4755
4856 err = c .request ("POST" , "/api/playlists" , nil , bytes .NewBuffer (data ), & result )
4957 if err != nil {
50- return 0 , err
58+ return "" , err
5159 }
5260
53- return result .ID , nil
61+ return result .QueryID () , nil
5462}
5563
5664// UpdatePlaylist updates a Grafana playlist.
5765func (c * Client ) UpdatePlaylist (playlist Playlist ) error {
58- path := fmt .Sprintf ("/api/playlists/%d " , playlist .ID )
66+ path := fmt .Sprintf ("/api/playlists/%s " , playlist .QueryID () )
5967 data , err := json .Marshal (playlist )
6068 if err != nil {
6169 return err
@@ -65,8 +73,8 @@ func (c *Client) UpdatePlaylist(playlist Playlist) error {
6573}
6674
6775// 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 )
7078
7179 return c .request ("DELETE" , path , nil , nil , nil )
7280}
0 commit comments