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

Commit b012274

Browse files
authored
Merge pull request #55 from justinTM/Library-Panels-fix
Library Elements API returns array for GET by name
2 parents bdffb25 + c45f9c2 commit b012274

File tree

2 files changed

+72
-17
lines changed

2 files changed

+72
-17
lines changed

library_panel.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,34 @@ func (c *Client) LibraryPanels() ([]LibraryPanel, error) {
101101

102102
// LibraryPanelByUID gets a library panel by UID.
103103
func (c *Client) LibraryPanelByUID(uid string) (*LibraryPanel, error) {
104-
return c.panel(fmt.Sprintf("/api/library-elements/%s", uid))
104+
resp := &LibraryPanelCreateResponse{}
105+
path := fmt.Sprintf("/api/library-elements/%s", uid)
106+
107+
err := c.request("GET", path, nil, nil, &resp)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
return &resp.Result, nil
105113
}
106114

107115
// LibraryPanelByName gets a library panel by name.
108116
func (c *Client) LibraryPanelByName(name string) (*LibraryPanel, error) {
109-
return c.panel(fmt.Sprintf("/api/library-elements/name/%s", name))
110-
}
117+
var resp struct {
118+
Result []LibraryPanel `json:"result"`
119+
}
120+
path := fmt.Sprintf("/api/library-elements/name/%s", name)
111121

112-
func (c *Client) panel(path string) (*LibraryPanel, error) {
113-
resp := &LibraryPanelCreateResponse{}
114122
err := c.request("GET", path, nil, nil, &resp)
115123
if err != nil {
116124
return nil, err
117125
}
118126

119-
return &resp.Result, err
127+
if len(resp.Result) != 1 {
128+
return nil, fmt.Errorf("expected 1 panel from GET library panel by name, got: %v", resp.Result)
129+
}
130+
131+
return &resp.Result[0], err
120132
}
121133

122134
// PatchLibraryPanel updates one or more properties of an existing panel that matches the specified UID.
@@ -168,7 +180,7 @@ func (c *Client) LibraryPanelConnections(uid string) (*[]LibraryPanelConnection,
168180
Result []LibraryPanelConnection `json:"result"`
169181
}{}
170182

171-
err := c.request("POST", path, nil, bytes.NewBuffer(nil), &resp)
183+
err := c.request("GET", path, nil, bytes.NewBuffer(nil), &resp)
172184
if err != nil {
173185
return nil, err
174186
}

library_panel_test.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,42 @@ import (
77
)
88

99
const (
10-
getLibraryPanelResponse = `{
10+
getLibraryPanelNameResponse = `{
11+
"result": [
12+
{
13+
"id": 25,
14+
"orgId": 1,
15+
"folderId": 0,
16+
"uid": "V--OrYHnz",
17+
"name": "API docs Example",
18+
"kind": 1,
19+
"model": {
20+
"description": "",
21+
"type": ""
22+
},
23+
"version": 1,
24+
"meta": {
25+
"folderName": "General",
26+
"folderUid": "",
27+
"connectedDashboards": 1,
28+
"created": "2021-09-27T09:56:17+02:00",
29+
"updated": "2021-09-27T09:56:17+02:00",
30+
"createdBy": {
31+
"id": 1,
32+
"name": "admin",
33+
"avatarUrl": "/avatar/46d229b033af06a191ff2267bca9ae56"
34+
},
35+
"updatedBy": {
36+
"id": 1,
37+
"name": "admin",
38+
"avatarUrl": "/avatar/46d229b033af06a191ff2267bca9ae56"
39+
}
40+
}
41+
}
42+
]
43+
}`
44+
45+
getLibraryPanelUIDResponse = `{
1146
"result": {
1247
"id": 25,
1348
"orgId": 1,
@@ -120,7 +155,7 @@ const (
120155
)
121156

122157
func TestLibraryPanelCreate(t *testing.T) {
123-
server, client := gapiTestTools(t, 200, getLibraryPanelResponse)
158+
server, client := gapiTestTools(t, 200, getLibraryPanelUIDResponse)
124159
defer server.Close()
125160

126161
panel := LibraryPanel{
@@ -149,8 +184,8 @@ func TestLibraryPanelCreate(t *testing.T) {
149184
}
150185
}
151186

152-
func TestLibraryPanelGet(t *testing.T) {
153-
server, client := gapiTestTools(t, 200, getLibraryPanelResponse)
187+
func TestLibraryPanelGetByName(t *testing.T) {
188+
server, client := gapiTestTools(t, 200, getLibraryPanelNameResponse)
154189
defer server.Close()
155190

156191
resp, err := client.LibraryPanelByName("API docs Example")
@@ -161,7 +196,20 @@ func TestLibraryPanelGet(t *testing.T) {
161196
t.Errorf("Invalid uid - %s, Expected %s", resp.UID, "V--OrYHnz")
162197
}
163198

164-
resp, err = client.LibraryPanelByUID("V--OrYHnz")
199+
for _, code := range []int{401, 403, 404} {
200+
server.code = code
201+
_, err = client.LibraryPanelByName("test")
202+
if err == nil {
203+
t.Errorf("%d not detected", code)
204+
}
205+
}
206+
}
207+
208+
func TestLibraryPanelGetByUID(t *testing.T) {
209+
server, client := gapiTestTools(t, 200, getLibraryPanelUIDResponse)
210+
defer server.Close()
211+
212+
resp, err := client.LibraryPanelByUID("V--OrYHnz")
165213
if err != nil {
166214
t.Fatal(err)
167215
}
@@ -171,11 +219,6 @@ func TestLibraryPanelGet(t *testing.T) {
171219

172220
for _, code := range []int{401, 403, 404} {
173221
server.code = code
174-
_, err = client.LibraryPanelByName("test")
175-
if err == nil {
176-
t.Errorf("%d not detected", code)
177-
}
178-
179222
_, err = client.LibraryPanelByUID("V--OrYHnz")
180223
if err == nil {
181224
t.Errorf("%d not detected", code)

0 commit comments

Comments
 (0)