Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 4b8162b

Browse files
Support UID for playlists (#100)
* Support UID for playlists This is how Grafana 9.0 manages playlists now * Use UID everywhere * Use UID in priority * Fix linting
1 parent 9bc48c7 commit 4b8162b

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

playlist.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@ type PlaylistItem struct {
1616

1717
// Playlist represents a Grafana playlist.
1818
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
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.
5765
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())
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
}

playlist_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66

77
const (
88
createAndUpdatePlaylistResponse = ` {
9-
"id": 1,
9+
"uid": "1",
1010
"name": "my playlist",
1111
"interval": "5m"
1212
}`
1313

1414
getPlaylistResponse = `{
15-
"id" : 2,
15+
"uid": "2",
1616
"name": "my playlist",
1717
"interval": "5m",
1818
"orgId": "my org",
@@ -55,8 +55,8 @@ func TestPlaylistCreateAndUpdate(t *testing.T) {
5555
t.Fatal(err)
5656
}
5757

58-
if id != 1 {
59-
t.Errorf("Invalid id - %d, Expected %d", id, 1)
58+
if id != "1" {
59+
t.Errorf("Invalid id - %s, Expected %s", id, "1")
6060
}
6161

6262
// update
@@ -77,13 +77,13 @@ func TestGetPlaylist(t *testing.T) {
7777
server, client := gapiTestTools(t, 200, getPlaylistResponse)
7878
defer server.Close()
7979

80-
playlist, err := client.Playlist(1)
80+
playlist, err := client.Playlist("2")
8181
if err != nil {
8282
t.Fatal(err)
8383
}
8484

85-
if playlist.ID != 2 {
86-
t.Errorf("Invalid id - %d, Expected %d", playlist.ID, 1)
85+
if playlist.UID != "2" {
86+
t.Errorf("Invalid id - %s, Expected %s", playlist.UID, "2")
8787
}
8888

8989
if len(playlist.Items) != 2 {
@@ -95,7 +95,7 @@ func TestDeletePlaylist(t *testing.T) {
9595
server, client := gapiTestTools(t, 200, "")
9696
defer server.Close()
9797

98-
err := client.DeletePlaylist(1)
98+
err := client.DeletePlaylist("1")
9999
if err != nil {
100100
t.Fatal(err)
101101
}

0 commit comments

Comments
 (0)