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

Commit c9c958b

Browse files
authored
Add method for listing existing dashboards (#38)
* Add method for listing existing dashboards * Remove executable bit from source file
1 parent e8cf2f1 commit c9c958b

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

dashboard.go

100755100644
+45-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io/ioutil"
99
"log"
10+
"net/url"
1011
"os"
1112
)
1213

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

28+
type DashboardSearchResponse struct {
29+
Id uint `json:"id"`
30+
Uid string `json:"uid"`
31+
Title string `json:"title"`
32+
Uri string `json:"uri"`
33+
Url string `json:"url"`
34+
Slug string `json:"slug"`
35+
Type string `json:"type"`
36+
Tags []string `json:"tags"`
37+
IsStarred bool `json:"isStarred"`
38+
FolderId uint `json:"folderId"`
39+
FolderUid string `json:"folderUid"`
40+
FolderTitle string `json:"folderTitle"`
41+
FolderUrl string `json:"folderUrl"`
42+
}
43+
2744
type Dashboard struct {
2845
Meta DashboardMeta `json:"meta"`
2946
Model map[string]interface{} `json:"dashboard"`
@@ -93,6 +110,33 @@ func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, erro
93110
return result, err
94111
}
95112

113+
func (c *Client) Dashboards() ([]DashboardSearchResponse, error) {
114+
dashboards := make([]DashboardSearchResponse, 0)
115+
query := url.Values{}
116+
// search only dashboards
117+
query.Add("type", "dash-db")
118+
req, err := c.newRequest("GET", "/api/search", query, nil)
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
resp, err := c.Do(req)
124+
if err != nil {
125+
return dashboards, err
126+
}
127+
if resp.StatusCode != 200 {
128+
return dashboards, errors.New(resp.Status)
129+
}
130+
131+
data, err := ioutil.ReadAll(resp.Body)
132+
if err != nil {
133+
return dashboards, err
134+
}
135+
136+
err = json.Unmarshal(data, &dashboards)
137+
return dashboards, err
138+
}
139+
96140
// Deprecated: Starting from Grafana v5.0. Please update to use DashboardByUID instead.
97141
func (c *Client) Dashboard(slug string) (*Dashboard, error) {
98142
return c.dashboard(fmt.Sprintf("/api/dashboards/db/%s", slug))
@@ -154,4 +198,4 @@ func (c *Client) deleteDashboard(path string) error {
154198
}
155199

156200
return nil
157-
}
201+
}

dashboard_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gobs/pretty"
7+
)
8+
9+
const (
10+
getDashboardsJSON = `
11+
[
12+
{
13+
"id": 1,
14+
"uid": "RGAPB1cZz",
15+
"title": "Grafana Stats",
16+
"uri": "db/grafana-stats",
17+
"url": "/dashboards/d/RGAPB1cZz/grafana-stat",
18+
"slug": "",
19+
"type": "dash-db",
20+
"tags": [],
21+
"isStarred": false
22+
}
23+
]
24+
`
25+
)
26+
27+
func TestDashboards(t *testing.T) {
28+
server, client := gapiTestTools(200, getDashboardsJSON)
29+
defer server.Close()
30+
31+
dashboards, err := client.Dashboards()
32+
if err != nil {
33+
t.Error(err)
34+
}
35+
36+
t.Log(pretty.PrettyFormat(dashboards))
37+
38+
if len(dashboards) != 1 {
39+
t.Error("Length of returned dashboards should be 1")
40+
}
41+
42+
if dashboards[0].Id != 1 || dashboards[0].Title != "Grafana Stats" {
43+
t.Error("Not correctly parsing returned dashboards.")
44+
}
45+
}

0 commit comments

Comments
 (0)