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

Commit

Permalink
Support UID for playlists (#100)
Browse files Browse the repository at this point in the history
* Support UID for playlists
This is how Grafana 9.0 manages playlists now

* Use UID everywhere

* Use UID in priority

* Fix linting
  • Loading branch information
julienduchesne authored Jul 15, 2022
1 parent 9bc48c7 commit 4b8162b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
34 changes: 21 additions & 13 deletions playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@ type PlaylistItem struct {

// Playlist represents a Grafana playlist.
type Playlist struct {
ID int `json:"id"`
ID int `json:"id,omitempty"` // Grafana < 9.0
UID string `json:"uid,omitempty"` // Grafana >= 9.0
Name string `json:"name"`
Interval string `json:"interval"`
Items []PlaylistItem `json:"items"`
}

// Grafana 9.0+ returns the ID and the UID but uses the UID in the API calls.
// Grafana <9 only returns the ID.
func (p *Playlist) QueryID() string {
if p.UID != "" {
return p.UID
}
return fmt.Sprintf("%d", p.ID)
}

// Playlist fetches and returns a Grafana playlist.
func (c *Client) Playlist(id int) (*Playlist, error) {
path := fmt.Sprintf("/api/playlists/%d", id)
func (c *Client) Playlist(idOrUID string) (*Playlist, error) {
path := fmt.Sprintf("/api/playlists/%s", idOrUID)
playlist := &Playlist{}
err := c.request("GET", path, nil, nil, playlist)
if err != nil {
Expand All @@ -35,27 +45,25 @@ func (c *Client) Playlist(id int) (*Playlist, error) {
}

// NewPlaylist creates a new Grafana playlist.
func (c *Client) NewPlaylist(playlist Playlist) (int, error) {
func (c *Client) NewPlaylist(playlist Playlist) (string, error) {
data, err := json.Marshal(playlist)
if err != nil {
return 0, err
return "", err
}

result := struct {
ID int
}{}
var result Playlist

err = c.request("POST", "/api/playlists", nil, bytes.NewBuffer(data), &result)
if err != nil {
return 0, err
return "", err
}

return result.ID, nil
return result.QueryID(), nil
}

// UpdatePlaylist updates a Grafana playlist.
func (c *Client) UpdatePlaylist(playlist Playlist) error {
path := fmt.Sprintf("/api/playlists/%d", playlist.ID)
path := fmt.Sprintf("/api/playlists/%s", playlist.QueryID())
data, err := json.Marshal(playlist)
if err != nil {
return err
Expand All @@ -65,8 +73,8 @@ func (c *Client) UpdatePlaylist(playlist Playlist) error {
}

// DeletePlaylist deletes the Grafana playlist whose ID it's passed.
func (c *Client) DeletePlaylist(id int) error {
path := fmt.Sprintf("/api/playlists/%d", id)
func (c *Client) DeletePlaylist(idOrUID string) error {
path := fmt.Sprintf("/api/playlists/%s", idOrUID)

return c.request("DELETE", path, nil, nil, nil)
}
16 changes: 8 additions & 8 deletions playlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (

const (
createAndUpdatePlaylistResponse = ` {
"id": 1,
"uid": "1",
"name": "my playlist",
"interval": "5m"
}`

getPlaylistResponse = `{
"id" : 2,
"uid": "2",
"name": "my playlist",
"interval": "5m",
"orgId": "my org",
Expand Down Expand Up @@ -55,8 +55,8 @@ func TestPlaylistCreateAndUpdate(t *testing.T) {
t.Fatal(err)
}

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

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

playlist, err := client.Playlist(1)
playlist, err := client.Playlist("2")
if err != nil {
t.Fatal(err)
}

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

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

err := client.DeletePlaylist(1)
err := client.DeletePlaylist("1")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 4b8162b

Please sign in to comment.