Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions library_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,34 @@ func (c *Client) LibraryPanels() ([]LibraryPanel, error) {

// LibraryPanelByUID gets a library panel by UID.
func (c *Client) LibraryPanelByUID(uid string) (*LibraryPanel, error) {
return c.panel(fmt.Sprintf("/api/library-elements/%s", uid))
resp := &LibraryPanelCreateResponse{}
path := fmt.Sprintf("/api/library-elements/%s", uid)

err := c.request("GET", path, nil, nil, &resp)
if err != nil {
return nil, err
}

return &resp.Result, nil
}

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

func (c *Client) panel(path string) (*LibraryPanel, error) {
resp := &LibraryPanelCreateResponse{}
err := c.request("GET", path, nil, nil, &resp)
if err != nil {
return nil, err
}

return &resp.Result, err
if len(resp.Result) != 1 {
return nil, fmt.Errorf("expected 1 panel from GET library panel by name, got: %v", resp.Result)
}

return &resp.Result[0], err
}

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

err := c.request("POST", path, nil, bytes.NewBuffer(nil), &resp)
err := c.request("GET", path, nil, bytes.NewBuffer(nil), &resp)
if err != nil {
return nil, err
}
Expand Down
63 changes: 53 additions & 10 deletions library_panel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,42 @@ import (
)

const (
getLibraryPanelResponse = `{
getLibraryPanelNameResponse = `{
"result": [
{
"id": 25,
"orgId": 1,
"folderId": 0,
"uid": "V--OrYHnz",
"name": "API docs Example",
"kind": 1,
"model": {
"description": "",
"type": ""
},
"version": 1,
"meta": {
"folderName": "General",
"folderUid": "",
"connectedDashboards": 1,
"created": "2021-09-27T09:56:17+02:00",
"updated": "2021-09-27T09:56:17+02:00",
"createdBy": {
"id": 1,
"name": "admin",
"avatarUrl": "/avatar/46d229b033af06a191ff2267bca9ae56"
},
"updatedBy": {
"id": 1,
"name": "admin",
"avatarUrl": "/avatar/46d229b033af06a191ff2267bca9ae56"
}
}
}
]
}`

getLibraryPanelUIDResponse = `{
"result": {
"id": 25,
"orgId": 1,
Expand Down Expand Up @@ -120,7 +155,7 @@ const (
)

func TestLibraryPanelCreate(t *testing.T) {
server, client := gapiTestTools(t, 200, getLibraryPanelResponse)
server, client := gapiTestTools(t, 200, getLibraryPanelUIDResponse)
defer server.Close()

panel := LibraryPanel{
Expand Down Expand Up @@ -149,8 +184,8 @@ func TestLibraryPanelCreate(t *testing.T) {
}
}

func TestLibraryPanelGet(t *testing.T) {
server, client := gapiTestTools(t, 200, getLibraryPanelResponse)
func TestLibraryPanelGetByName(t *testing.T) {
server, client := gapiTestTools(t, 200, getLibraryPanelNameResponse)
defer server.Close()

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

resp, err = client.LibraryPanelByUID("V--OrYHnz")
for _, code := range []int{401, 403, 404} {
server.code = code
_, err = client.LibraryPanelByName("test")
if err == nil {
t.Errorf("%d not detected", code)
}
}
}

func TestLibraryPanelGetByUID(t *testing.T) {
server, client := gapiTestTools(t, 200, getLibraryPanelUIDResponse)
defer server.Close()

resp, err := client.LibraryPanelByUID("V--OrYHnz")
if err != nil {
t.Fatal(err)
}
Expand All @@ -171,11 +219,6 @@ func TestLibraryPanelGet(t *testing.T) {

for _, code := range []int{401, 403, 404} {
server.code = code
_, err = client.LibraryPanelByName("test")
if err == nil {
t.Errorf("%d not detected", code)
}

_, err = client.LibraryPanelByUID("V--OrYHnz")
if err == nil {
t.Errorf("%d not detected", code)
Expand Down