This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
dashboard_public.go
106 lines (90 loc) · 3.72 KB
/
dashboard_public.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 gapi
import (
"encoding/json"
"fmt"
"time"
)
// PublicDashboardPayload represents a public dashboard payload.
type PublicDashboardPayload struct {
UID string `json:"uid"`
AccessToken string `json:"accessToken"`
TimeSelectionEnabled bool `json:"timeSelectionEnabled"`
IsEnabled bool `json:"isEnabled"`
AnnotationsEnabled bool `json:"annotationsEnabled"`
Share string `json:"share"`
}
// PublicDashboard represents a public dashboard.
type PublicDashboard struct {
UID string `json:"uid"`
DashboardUID string `json:"dashboardUid"`
AccessToken string `json:"accessToken"`
TimeSelectionEnabled bool `json:"timeSelectionEnabled"`
IsEnabled bool `json:"isEnabled"`
AnnotationsEnabled bool `json:"annotationsEnabled"`
Share string `json:"share"`
CreatedBy int64 `json:"createdBy"`
UpdatedBy int64 `json:"updatedBy"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type PublicDashboardListResponseWithPagination struct {
PublicDashboards []*PublicDashboardListResponse `json:"publicDashboards"`
TotalCount int64 `json:"totalCount"`
Page int `json:"page"`
PerPage int `json:"perPage"`
}
type PublicDashboardListResponse struct {
UID string `json:"uid"`
AccessToken string `json:"accessToken"`
Title string `json:"title"`
DashboardUID string `json:"dashboardUid"`
IsEnabled bool `json:"isEnabled"`
}
// NewPublicDashboard creates a new Grafana public dashboard.
func (c *Client) NewPublicDashboard(dashboardUID string, publicDashboard PublicDashboardPayload) (*PublicDashboard, error) {
data, err := json.Marshal(publicDashboard)
if err != nil {
return nil, err
}
result := &PublicDashboard{}
err = c.request("POST", fmt.Sprintf("/api/dashboards/uid/%s/public-dashboards", dashboardUID), nil, data, &result)
if err != nil {
return nil, err
}
return result, err
}
// DeletePublicDashboard deletes a Grafana public dashboard.
func (c *Client) DeletePublicDashboard(dashboardUID string, publicDashboardUID string) error {
return c.request("DELETE", fmt.Sprintf("/api/dashboards/uid/%s/public-dashboards/%s", dashboardUID, publicDashboardUID), nil, nil, nil)
}
// PublicDashboards fetches and returns the Grafana public dashboards.
func (c *Client) PublicDashboards() (*PublicDashboardListResponseWithPagination, error) {
publicdashboards := &PublicDashboardListResponseWithPagination{}
err := c.request("GET", "/api/dashboards/public-dashboards", nil, nil, &publicdashboards)
if err != nil {
return publicdashboards, err
}
return publicdashboards, err
}
// PublicDashboardbyUID fetches and returns a Grafana public dashboard by uid.
func (c *Client) PublicDashboardbyUID(dashboardUID string) (*PublicDashboard, error) {
publicDashboard := &PublicDashboard{}
err := c.request("GET", fmt.Sprintf("/api/dashboards/uid/%s/public-dashboards", dashboardUID), nil, nil, &publicDashboard)
if err != nil {
return publicDashboard, err
}
return publicDashboard, err
}
// UpdatePublicDashboard updates a Grafana public dashboard.
func (c *Client) UpdatePublicDashboard(dashboardUID string, publicDashboardUID string, publicDashboard PublicDashboardPayload) (*PublicDashboard, error) {
data, err := json.Marshal(publicDashboard)
if err != nil {
return nil, err
}
result := &PublicDashboard{}
err = c.request("PATCH", fmt.Sprintf("/api/dashboards/uid/%s/public-dashboards/%s", dashboardUID, publicDashboardUID), nil, data, &result)
if err != nil {
return nil, err
}
return result, err
}