Files
opentf/vendor/github.com/apparentlymart/go-grafana-api/dashboard.go
Martin Atkins 158a90b25b Grafana Provider, with Data Source and Dashboard resources (#6206)
* Grafana provider

* grafana_data_source resource.

Allows data sources to be created in Grafana. Supports all data source
types that are accepted in the current version of Grafana, and will
support any future ones that fit into the existing structure.

* Vendoring of apparentlymart/go-grafana-api

This is in anticipation of adding a Grafana provider plugin.

* grafana_dashboard resource

* Website documentation for the Grafana provider.
2016-05-20 10:20:17 +01:00

101 lines
1.9 KiB
Go

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
}