terraform/vendor/github.com/apparentlymart/go-grafana-api/dashboard.go

101 lines
1.9 KiB
Go
Raw Normal View History

package gapi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
)
type DashboardMeta struct {
IsStarred bool `json:"isStarred"`
Slug string `json:"slug"`
}
type DashboardSaveResponse struct {
Slug string `json:"slug"`
Status string `json:"status"`
Version int64 `json:"version"`
}
type Dashboard struct {
Meta DashboardMeta `json:"meta"`
Model map[string]interface{} `json:"dashboard"`
}
func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*DashboardSaveResponse, error) {
wrapper := map[string]interface{}{
"dashboard": model,
"overwrite": overwrite,
}
data, err := json.Marshal(wrapper)
if err != nil {
return nil, err
}
req, err := c.newRequest("POST", "/api/dashboards/db", bytes.NewBuffer(data))
if err != nil {
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result := &DashboardSaveResponse{}
err = json.Unmarshal(data, &result)
return result, err
}
func (c *Client) Dashboard(slug string) (*Dashboard, error) {
path := fmt.Sprintf("/api/dashboards/db/%s", slug)
req, err := c.newRequest("GET", path, nil)
if err != nil {
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result := &Dashboard{}
err = json.Unmarshal(data, &result)
return result, err
}
func (c *Client) DeleteDashboard(slug string) error {
path := fmt.Sprintf("/api/dashboards/db/%s", slug)
req, err := c.newRequest("DELETE", path, nil)
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return nil
}