This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
dashboards.go
106 lines (84 loc) · 2.28 KB
/
dashboards.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package api
import (
"fmt"
"net/url"
)
func (c *Client) queryDashboards() ([]Dashboard, error) {
dashboards := []Dashboard{}
reqURL, err := url.Parse("/dashboards.json")
if err != nil {
return nil, err
}
nextPath := reqURL.String()
for nextPath != "" {
resp := struct {
Dashboards []Dashboard `json:"dashboards,omitempty"`
}{}
nextPath, err = c.Do("GET", nextPath, nil, &resp)
if err != nil {
return nil, err
}
dashboards = append(dashboards, resp.Dashboards...)
}
return dashboards, nil
}
// GetDashboard returns a specific dashboard for the account.
func (c *Client) GetDashboard(id int) (*Dashboard, error) {
reqURL, err := url.Parse(fmt.Sprintf("/dashboards/%v.json", id))
if err != nil {
return nil, err
}
resp := struct {
Dashboard Dashboard `json:"dashboard,omitempty"`
}{}
_, err = c.Do("GET", reqURL.String(), nil, &resp)
if err != nil {
return nil, err
}
return &resp.Dashboard, nil
}
// ListDashboards returns all dashboards for the account.
func (c *Client) ListDashboards() ([]Dashboard, error) {
return c.queryDashboards()
}
// CreateDashboard creates dashboard given the passed configuration.
func (c *Client) CreateDashboard(dashboard Dashboard) (*Dashboard, error) {
req := struct {
Dashboard Dashboard `json:"dashboard"`
}{
Dashboard: dashboard,
}
resp := struct {
Dashboard Dashboard `json:"dashboard,omitempty"`
}{}
u := &url.URL{Path: "/dashboards.json"}
_, err := c.Do("POST", u.String(), req, &resp)
if err != nil {
return nil, err
}
return &resp.Dashboard, nil
}
// UpdateDashboard updates a dashboard given the passed configuration
func (c *Client) UpdateDashboard(dashboard Dashboard) (*Dashboard, error) {
id := dashboard.ID
req := struct {
Dashboard Dashboard `json:"dashboard"`
}{
Dashboard: dashboard,
}
resp := struct {
Dashboard Dashboard `json:"dashboard,omitempty"`
}{}
u := &url.URL{Path: fmt.Sprintf("/dashboards/%v.json", id)}
_, err := c.Do("PUT", u.String(), req, &resp)
if err != nil {
return nil, err
}
return &resp.Dashboard, nil
}
// DeleteDashboard deletes an existing dashboard given the passed configuration
func (c *Client) DeleteDashboard(id int) error {
u := &url.URL{Path: fmt.Sprintf("/dashboards/%v.json", id)}
_, err := c.Do("DELETE", u.String(), nil, nil)
return err
}