diff --git a/library_panel.go b/library_panel.go index df3fcd4d..21cf2af6 100644 --- a/library_panel.go +++ b/library_panel.go @@ -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. @@ -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 } diff --git a/library_panel_test.go b/library_panel_test.go index 81ecb03a..fcfacdd5 100644 --- a/library_panel_test.go +++ b/library_panel_test.go @@ -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, @@ -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{ @@ -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") @@ -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) } @@ -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)