7
7
"fmt"
8
8
"io/ioutil"
9
9
"log"
10
+ "net/url"
10
11
"os"
11
12
)
12
13
@@ -24,6 +25,22 @@ type DashboardSaveResponse struct {
24
25
Version int64 `json:"version"`
25
26
}
26
27
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
+
27
44
type Dashboard struct {
28
45
Meta DashboardMeta `json:"meta"`
29
46
Model map [string ]interface {} `json:"dashboard"`
@@ -93,6 +110,33 @@ func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, erro
93
110
return result , err
94
111
}
95
112
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
+
96
140
// Deprecated: Starting from Grafana v5.0. Please update to use DashboardByUID instead.
97
141
func (c * Client ) Dashboard (slug string ) (* Dashboard , error ) {
98
142
return c .dashboard (fmt .Sprintf ("/api/dashboards/db/%s" , slug ))
@@ -154,4 +198,4 @@ func (c *Client) deleteDashboard(path string) error {
154
198
}
155
199
156
200
return nil
157
- }
201
+ }
0 commit comments