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+
2744type 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.
97141func (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+ }
0 commit comments