Skip to content

Commit

Permalink
Requests by uid (#69)
Browse files Browse the repository at this point in the history
* Requests by uid

* Integration tests for dahsboards

* added delete action and more error messages on tests

* backward compatibility for use func GetDashboard without prefix

* more integration tests for getDashboard
  • Loading branch information
GiedriusS committed Mar 5, 2020
1 parent 066e3fb commit 47f19f1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
26 changes: 23 additions & 3 deletions rest-dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,29 @@ type BoardProperties struct {
Version int `json:"version"`
}

// GetDashboardByUID loads a dashboard and its metadata from Grafana by dashboard uid.
func (r *Client) GetDashboardByUID(uid string) (Board, BoardProperties, error) {
return r.GetDashboard("uid/" + uid)
}

// GetDashboardBySlug loads a dashboard and its metadata from Grafana by dashboard slug.
// Deprecated since Grafana v5
//
// For dashboards from a filesystem set "file/" prefix for slug. By default dashboards from
// a database assumed. Database dashboards may have "db/" prefix or may have not, it will
// be appended automatically.
func (r *Client) GetDashboardBySlug(slug string) (Board, BoardProperties, error) {
path, _ := setPrefix(slug)
return r.GetDashboard(path)
}

// GetDashboard loads a dashboard from Grafana instance along with metadata for a dashboard.
// For dashboards from a filesystem set "file/" prefix for slug. By default dashboards from
// a database assumed. Database dashboards may have "db/" prefix or may have not, it will
// be appended automatically.
//
// Reflects GET /api/dashboards/db/:slug API call.
func (r *Client) GetDashboard(slug string) (Board, BoardProperties, error) {
func (r *Client) GetDashboard(path string) (Board, BoardProperties, error) {
var (
raw []byte
result struct {
Expand All @@ -63,8 +79,8 @@ func (r *Client) GetDashboard(slug string) (Board, BoardProperties, error) {
code int
err error
)
slug, _ = setPrefix(slug)
if raw, code, err = r.get(fmt.Sprintf("api/dashboards/%s", slug), nil); err != nil {
path, _ = setPrefix(path)
if raw, code, err = r.get(fmt.Sprintf("api/dashboards/%s", path), nil); err != nil {
return Board{}, BoardProperties{}, err
}
if code != 200 {
Expand Down Expand Up @@ -118,6 +134,7 @@ func (r *Client) GetRawDashboard(slug string) ([]byte, BoardProperties, error) {
// FoundBoard keeps result of search with metadata of a dashboard.
type FoundBoard struct {
ID uint `json:"id"`
UID string `json:"uid"`
Title string `json:"title"`
URI string `json:"uri"`
Type string `json:"type"`
Expand Down Expand Up @@ -266,6 +283,9 @@ func setPrefix(slug string) (string, bool) {
if strings.HasPrefix(slug, "file") {
return slug, false
}
if strings.HasPrefix(slug, "uid") {
return slug, false
}
return fmt.Sprintf("db/%s", slug), true
}

Expand Down
53 changes: 53 additions & 0 deletions rest-dashboard_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package sdk_test

import (
"encoding/json"
"io/ioutil"
"testing"

"github.com/grafana-tools/sdk"
)

func Test_Dashboard_CRUD(t *testing.T) {
var (
boardLinks []sdk.FoundBoard
err error
)

shouldSkip(t)

client := getClient()

var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/new-empty-dashboard-2.6.json")

if err = json.Unmarshal(raw, &board); err != nil {
t.Error(err)
}

client.DeleteDashboard(board.UpdateSlug())
if _, err = client.SetDashboard(board, false); err != nil {
t.Error(err)
}

if boardLinks, err = client.SearchDashboards("", false); err != nil {
t.Error(err)
}

for _, link := range boardLinks {
_, _, err = client.GetDashboardByUID(link.UID)
if err != nil {
t.Error(err)
}

_, _, err = client.GetDashboard(link.URI)
if err != nil {
t.Error(err)
}

_, _, err = client.GetDashboardBySlug(link.URI)
if err != nil {
t.Error(err)
}
}
}

0 comments on commit 47f19f1

Please sign in to comment.