You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the client we are able to find if a folder exists. Likewise, It would be great if we could query whether a dashboard already exists.
It could be accomplished with two functions like this:
// This function queries the /api/search endpoint and returns the dashboard if it exists
func (client *Client) getDashboardByTitle(ctx context.Context, title string) (*DashboardQuery, error) {
// 1. make a request to search for the dash board
query := fmt.Sprintf("/api/search?query=%s", title)
resp, err := client.get(ctx, query)
if err != nil {
err1 := fmt.Errorf("grafput: getDashboardByTitle %s", err)
return nil, err1
}
// 2. check the status code
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
err1 := fmt.Errorf("grafput: getDashboardByTitle: %s", err)
return nil, err1
}
return nil, fmt.Errorf("could not read body after querying dashboard: %s (HTTP status %d)", body, resp.StatusCode)
}
defer func() { _ = resp.Body.Close() }()
// 3. decode json using our Dashboard Query struct
var dashboard []DashboardQuery
if err := decodeJSON(resp.Body, &dashboardQuery); err != nil {
err1 := fmt.Errorf("grafput: getDashboardByTitle: %s", err)
return nil, err1
}
if len(dashboardQuery) == 0 {
return nil, nil
}
// 4. iterate through the list of dashboards until we match title
for i := range dashboardQuery {
if strings.EqualFold(dashboardQuery[i].Title, title) {
return &dashboardQuery[i], nil
}
}
return nil, nil
}
// This function enables us to search Grafana for a dashboard by its title
func (client *Client) CheckIfDashboardExists(ctx context.Context, title string) (string, error) {
dashboard, err := client.getDashboardByTitle(ctx, title)
if err != nil {
err1 := fmt.Errorf("grafput: CheckIfDashboard Exists: %s", err)
return "", err1
}
if dashboard == nil {
return "", nil
}
dashboardUrl := dashboard.Url
// here we could return the dashboard object or the URL
return dashboardUrl, nil
}
type DashboardQuery struct {
Id int `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 int `json:"folderId"`
FolderUid string `json:"folderUid"`
FolderTitle string `json:"folderTitle"`
FolderUrl string `json:"folderUrl"`
SortMeta int `json:"sortMeta"`
}
The text was updated successfully, but these errors were encountered:
To keep the API surface to a minimum, I think grabana should only expose a GetDashboardByTitle (since the existence check can be implemented rather easily by calling GetDashboardByTitle and checking for a well-known error like ErrDashboardNotFound).
Yeah, I agree that it would make more sense for the API to only expose GetDashboardByTitle. And yes, I would be interested in contributing. I can make a pull request sometime this or next week.
Using the client we are able to find if a folder exists. Likewise, It would be great if we could query whether a dashboard already exists.
It could be accomplished with two functions like this:
The text was updated successfully, but these errors were encountered: