Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit b3a3c20

Browse files
authored
Add public dashboards support (#161)
* Add public dashboards support * fix lint issues * remove bool pointers * gofmt file
1 parent c093bc7 commit b3a3c20

File tree

2 files changed

+264
-0
lines changed

2 files changed

+264
-0
lines changed

dashboard_public.go

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package gapi
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"time"
7+
)
8+
9+
// PublicDashboardPayload represents a public dashboard payload.
10+
type PublicDashboardPayload struct {
11+
UID string `json:"uid"`
12+
AccessToken string `json:"accessToken"`
13+
TimeSelectionEnabled bool `json:"timeSelectionEnabled"`
14+
IsEnabled bool `json:"isEnabled"`
15+
AnnotationsEnabled bool `json:"annotationsEnabled"`
16+
Share string `json:"share"`
17+
}
18+
19+
// PublicDashboard represents a public dashboard.
20+
type PublicDashboard struct {
21+
UID string `json:"uid"`
22+
DashboardUID string `json:"dashboardUid"`
23+
AccessToken string `json:"accessToken"`
24+
TimeSelectionEnabled bool `json:"timeSelectionEnabled"`
25+
IsEnabled bool `json:"isEnabled"`
26+
AnnotationsEnabled bool `json:"annotationsEnabled"`
27+
Share string `json:"share"`
28+
CreatedBy int64 `json:"createdBy"`
29+
UpdatedBy int64 `json:"updatedBy"`
30+
CreatedAt time.Time `json:"createdAt"`
31+
UpdatedAt time.Time `json:"updatedAt"`
32+
}
33+
34+
type PublicDashboardListResponseWithPagination struct {
35+
PublicDashboards []*PublicDashboardListResponse `json:"publicDashboards"`
36+
TotalCount int64 `json:"totalCount"`
37+
Page int `json:"page"`
38+
PerPage int `json:"perPage"`
39+
}
40+
41+
type PublicDashboardListResponse struct {
42+
UID string `json:"uid"`
43+
AccessToken string `json:"accessToken"`
44+
Title string `json:"title"`
45+
DashboardUID string `json:"dashboardUid"`
46+
IsEnabled bool `json:"isEnabled"`
47+
}
48+
49+
// NewPublicDashboard creates a new Grafana public dashboard.
50+
func (c *Client) NewPublicDashboard(dashboardUID string, publicDashboard PublicDashboardPayload) (*PublicDashboard, error) {
51+
data, err := json.Marshal(publicDashboard)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
result := &PublicDashboard{}
57+
err = c.request("POST", fmt.Sprintf("/api/dashboards/uid/%s/public-dashboards", dashboardUID), nil, data, &result)
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
return result, err
63+
}
64+
65+
// DeletePublicDashboard deletes a Grafana public dashboard.
66+
func (c *Client) DeletePublicDashboard(dashboardUID string, publicDashboardUID string) error {
67+
return c.request("DELETE", fmt.Sprintf("/api/dashboards/uid/%s/public-dashboards/%s", dashboardUID, publicDashboardUID), nil, nil, nil)
68+
}
69+
70+
// PublicDashboards fetches and returns the Grafana public dashboards.
71+
func (c *Client) PublicDashboards() (*PublicDashboardListResponseWithPagination, error) {
72+
publicdashboards := &PublicDashboardListResponseWithPagination{}
73+
err := c.request("GET", "/api/dashboards/public-dashboards", nil, nil, &publicdashboards)
74+
if err != nil {
75+
return publicdashboards, err
76+
}
77+
78+
return publicdashboards, err
79+
}
80+
81+
// PublicDashboardbyUID fetches and returns a Grafana public dashboard by uid.
82+
func (c *Client) PublicDashboardbyUID(dashboardUID string) (*PublicDashboard, error) {
83+
publicDashboard := &PublicDashboard{}
84+
err := c.request("GET", fmt.Sprintf("/api/dashboards/uid/%s/public-dashboards", dashboardUID), nil, nil, &publicDashboard)
85+
if err != nil {
86+
return publicDashboard, err
87+
}
88+
89+
return publicDashboard, err
90+
}
91+
92+
// UpdatePublicDashboard updates a Grafana public dashboard.
93+
func (c *Client) UpdatePublicDashboard(dashboardUID string, publicDashboardUID string, publicDashboard PublicDashboardPayload) (*PublicDashboard, error) {
94+
data, err := json.Marshal(publicDashboard)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
result := &PublicDashboard{}
100+
err = c.request("PATCH", fmt.Sprintf("/api/dashboards/uid/%s/public-dashboards/%s", dashboardUID, publicDashboardUID), nil, data, &result)
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
return result, err
106+
}

dashboard_public_test.go

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gobs/pretty"
7+
)
8+
9+
const (
10+
createPublicDashboard = `{
11+
"uid": "fdc8b8fd-72cb-45d2-927a-75900e4f6e70",
12+
"dashboardUid": "nErXDvCkzz",
13+
"isEnabled": true,
14+
"share": "public"
15+
}`
16+
updatePublicDashboard = `{
17+
"timeSelectionEnabled": true,
18+
"isEnabled": true,
19+
"annotationsEnabled": true
20+
}`
21+
publicDashboardByUID = `{
22+
"uid": "cd56d9fd-f3d4-486d-afba-a21760e2acbe",
23+
"dashboardUid": "xCpsVuc4z",
24+
"accessToken": "5c948bf96e6a4b13bd91975f9a2028b7",
25+
"createdBy": 1,
26+
"updatedBy": 1,
27+
"createdAt": "2023-09-05T11:41:14-03:00",
28+
"updatedAt": "2023-09-05T11:41:14-03:00",
29+
"timeSelectionEnabled": false,
30+
"isEnabled": true,
31+
"annotationsEnabled": false,
32+
"share": "public"
33+
}`
34+
publicDashboardList = `{
35+
"publicDashboards": [
36+
{
37+
"uid": "e9f29a3c-fcc3-4fc5-a690-ae39c97d24ba",
38+
"accessToken": "6c13ec1997ba48c5af8c9c5079049692",
39+
"title": "A Datasource not found query",
40+
"dashboardUid": "d2f21d0a-76c7-47ec-b5f3-9dda16e5a996",
41+
"isEnabled": true
42+
},
43+
{
44+
"uid": "a174f604-6fe7-47de-97b4-48b7e401b540",
45+
"accessToken": "d1fcff345c0f45e8a78c096c9696034a",
46+
"title": "A Issue heatmap bargauge panel",
47+
"dashboardUid": "51DiOw0Vz",
48+
"isEnabled": true
49+
}
50+
],
51+
"totalCount": 2,
52+
"page": 1,
53+
"perPage": 1000
54+
}`
55+
)
56+
57+
func TestNewPublicDashboard(t *testing.T) {
58+
const dashboardUID = "nErXDvCkzz"
59+
60+
client := gapiTestTools(t, 200, createPublicDashboard)
61+
62+
publicDashboard := PublicDashboardPayload{
63+
UID: "fdc8b8fd-72cb-45d2-927a-75900e4f6e70",
64+
AccessToken: "b1d5f3f534d84375a897f3969b6157f3",
65+
IsEnabled: true,
66+
Share: "public",
67+
}
68+
69+
resp, err := client.NewPublicDashboard(dashboardUID, publicDashboard)
70+
if err != nil {
71+
t.Fatal(err)
72+
}
73+
74+
t.Log(pretty.PrettyFormat(resp))
75+
76+
if resp.UID != "fdc8b8fd-72cb-45d2-927a-75900e4f6e70" {
77+
t.Errorf("Invalid uid - %s, Expected %s", resp.UID, "fdc8b8fd-72cb-45d2-927a-75900e4f6e70")
78+
}
79+
80+
if resp.DashboardUID != dashboardUID {
81+
t.Errorf("Invalid dashboard uid - %s, Expected %s", resp.DashboardUID, dashboardUID)
82+
}
83+
}
84+
85+
func TestDeletePublicDashboard(t *testing.T) {
86+
client := gapiTestTools(t, 200, "")
87+
88+
err := client.DeletePublicDashboard("nErXDvCkza", "fdc8b8fd-72cb-45d2-927a-75900e4f6e70")
89+
if err != nil {
90+
t.Error(err)
91+
}
92+
}
93+
94+
func TestPublicDashboards(t *testing.T) {
95+
client := gapiTestTools(t, 200, publicDashboardList)
96+
97+
resp, err := client.PublicDashboards()
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
102+
t.Log(pretty.PrettyFormat(resp))
103+
104+
if len(resp.PublicDashboards) != 2 || resp.TotalCount != 2 {
105+
t.Error("Length of returned public dashboards should be 2")
106+
}
107+
if resp.PublicDashboards[0].UID != "e9f29a3c-fcc3-4fc5-a690-ae39c97d24ba" || resp.PublicDashboards[0].AccessToken != "6c13ec1997ba48c5af8c9c5079049692" {
108+
t.Error("Not correctly parsing returned public dashboards.")
109+
}
110+
}
111+
112+
func TestPublicDashboardByUID(t *testing.T) {
113+
client := gapiTestTools(t, 200, publicDashboardByUID)
114+
115+
resp, err := client.PublicDashboardbyUID("xCpsVuc4z")
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
120+
t.Log(pretty.PrettyFormat(resp))
121+
122+
if resp.UID != "cd56d9fd-f3d4-486d-afba-a21760e2acbe" {
123+
t.Errorf("Invalid uid - %s, Expected %s", resp.UID, "cd56d9fd-f3d4-486d-afba-a21760e2acbe")
124+
}
125+
126+
if resp.DashboardUID != "xCpsVuc4z" {
127+
t.Errorf("Invalid dashboard uid - %s, Expected %s", resp.DashboardUID, "xCpsVuc4z")
128+
}
129+
}
130+
131+
func TestUpdatePublicDashboard(t *testing.T) {
132+
client := gapiTestTools(t, 200, updatePublicDashboard)
133+
134+
publicDashboard := PublicDashboardPayload{
135+
IsEnabled: true,
136+
TimeSelectionEnabled: true,
137+
AnnotationsEnabled: true,
138+
}
139+
140+
resp, err := client.UpdatePublicDashboard("xCpsVuc4z", "cd56d9fd-f3d4-486d-afba-a21760e2acbe", publicDashboard)
141+
if err != nil {
142+
t.Fatal(err)
143+
}
144+
145+
t.Log(pretty.PrettyFormat(resp))
146+
147+
if !resp.IsEnabled {
148+
t.Errorf("Invalid IsEnabled - %t, Expected %t", resp.IsEnabled, true)
149+
}
150+
151+
if !resp.TimeSelectionEnabled {
152+
t.Errorf("Invalid TimeSelectionEnabled - %t, Expected %t", resp.TimeSelectionEnabled, true)
153+
}
154+
155+
if !resp.AnnotationsEnabled {
156+
t.Errorf("Invalid AnnotationsEnabled - %t, Expected %t", resp.AnnotationsEnabled, true)
157+
}
158+
}

0 commit comments

Comments
 (0)