Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Add method for listing existing dashboards #38

Merged
merged 2 commits into from
Jun 2, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion dashboard.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
)

Expand All @@ -24,6 +25,22 @@ type DashboardSaveResponse struct {
Version int64 `json:"version"`
}

type DashboardSearchResponse struct {
Id uint `json:"id"`
Uid string `json:"uid"`
Title string `json:"title"`
Uri string `json:"uri"`
Url string `json:"url"`
Slug string `json:"slug"`
Type string `json:"type"`
Tags []string `json:"tags"`
IsStarred bool `json:"isStarred"`
FolderId uint `json:"folderId"`
FolderUid string `json:"folderUid"`
FolderTitle string `json:"folderTitle"`
FolderUrl string `json:"folderUrl"`
}

type Dashboard struct {
Meta DashboardMeta `json:"meta"`
Model map[string]interface{} `json:"dashboard"`
Expand Down Expand Up @@ -93,6 +110,33 @@ func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, erro
return result, err
}

func (c *Client) Dashboards() ([]DashboardSearchResponse, error) {
dashboards := make([]DashboardSearchResponse, 0)
query := url.Values{}
// search only dashboards
query.Add("type", "dash-db")
req, err := c.newRequest("GET", "/api/search", query, nil)
if err != nil {
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return dashboards, err
}
if resp.StatusCode != 200 {
return dashboards, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return dashboards, err
}

err = json.Unmarshal(data, &dashboards)
return dashboards, err
}

// Deprecated: Starting from Grafana v5.0. Please update to use DashboardByUID instead.
func (c *Client) Dashboard(slug string) (*Dashboard, error) {
return c.dashboard(fmt.Sprintf("/api/dashboards/db/%s", slug))
Expand Down Expand Up @@ -154,4 +198,4 @@ func (c *Client) deleteDashboard(path string) error {
}

return nil
}
}
45 changes: 45 additions & 0 deletions dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gapi

import (
"testing"

"github.com/gobs/pretty"
)

const (
getDashboardsJSON = `
[
{
"id": 1,
"uid": "RGAPB1cZz",
"title": "Grafana Stats",
"uri": "db/grafana-stats",
"url": "/dashboards/d/RGAPB1cZz/grafana-stat",
"slug": "",
"type": "dash-db",
"tags": [],
"isStarred": false
}
]
`
)

func TestDashboards(t *testing.T) {
server, client := gapiTestTools(200, getDashboardsJSON)
defer server.Close()

dashboards, err := client.Dashboards()
if err != nil {
t.Error(err)
}

t.Log(pretty.PrettyFormat(dashboards))

if len(dashboards) != 1 {
t.Error("Length of returned dashboards should be 1")
}

if dashboards[0].Id != 1 || dashboards[0].Title != "Grafana Stats" {
t.Error("Not correctly parsing returned dashboards.")
}
}