mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-03-13 01:00:50 -04:00
* 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.
101 lines
1.9 KiB
Go
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
|
|
}
|