Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requests by uid #69

Merged
merged 5 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
UndeadJoe marked this conversation as resolved.
Show resolved Hide resolved
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)
UndeadJoe marked this conversation as resolved.
Show resolved Hide resolved
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)
UndeadJoe marked this conversation as resolved.
Show resolved Hide resolved
}

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)
}
}
}