Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
15 changes: 15 additions & 0 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ func (c *Client) DashboardByUID(uid string) (*Dashboard, error) {
return c.dashboard(fmt.Sprintf("/api/dashboards/uid/%s", uid))
}

// DashboardsByIDs uses the folder and dashboard search endpoint to find
// dashboards by list of dashboard IDs.
func (c *Client) DashboardsByIDs(ids []int64) ([]FolderDashboardSearchResponse, error) {
dashboardIdsJSON, err := json.Marshal(ids)
if err != nil {
return nil, err
}

params := map[string]string{
"type": "dash-db",
"dashboardIds": string(dashboardIdsJSON),
}
return c.FolderDashboardSearch(params)
}

func (c *Client) dashboard(path string) (*Dashboard, error) {
result := &Dashboard{}
err := c.request("GET", path, nil, nil, &result)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/grafana/grafana-api-golang-client
module github.com/justinTM/grafana-api-golang-client

go 1.14

Expand Down
159 changes: 159 additions & 0 deletions library_panel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package gapi

import (
"bytes"
"encoding/json"
"fmt"
"time"
)

// LibraryPanelMetaUser represents the Grafana library panel createdBy and updatedBy fields
type LibraryPanelMetaUser struct {
ID int64 `json:"id"`
Name string `json:"name"`
AvatarURL string `json:"folderId"`
}

// LibraryPanelMeta represents Grafana library panel metadata.
type LibraryPanelMeta struct {
FolderName string `json:"folderName,,omitempty"`
FolderUID string `json:"folderUid,omitempty"`
ConnectedDashboards int64 `json:"connectedDashboards,omitempty"`
Created time.Time `json:"created,omitempty"`
Updated time.Time `json:"updated,omitempty"`
CreatedBy LibraryPanelMetaUser `json:"createdBy,omitempty"`
UpdatedBy LibraryPanelMetaUser `json:"updatedBy,omitempty"`
}

// LibraryPanel represents a Grafana library panel.
type LibraryPanel struct {
Folder int64 `json:"folderId"`
Name string `json:"name"`
Model map[string]interface{} `json:"model"`
Description string `json:"description,omitempty"`
ID int64 `json:"id,omitempty"`
Kind int64 `json:"kind,omitempty"`
OrgID int64 `json:"orgId,omitempty"`
UID string `json:"uid,omitempty"`
Version int64 `json:"version,omitempty"`
Meta LibraryPanelMeta `json:"meta,omitempty"`
}

// LibraryPanelCreateResponse represents the Grafana API response to creating or saving a library panel.
type LibraryPanelCreateResponse struct {
Result LibraryPanel `json:"result"`
}

// LibraryPanelDeleteResponse represents the Grafana API response to deleting a library panel.
type LibraryPanelDeleteResponse struct {
Message string `json:"message"`
ID int64 `json:"id,omitempty"`
}

// LibraryPanelConnection represents a Grafana connection between a library panel and a dashboard.
type LibraryPanelConnection struct {
ID int64 `json:"id"`
Kind int64 `json:"kind"`
PanelID int64 `json:"elementId"`
DashboardID int64 `json:"connectionId"`
Created time.Time `json:"created"`
CreatedBy LibraryPanelMetaUser `json:"createdBy"`
}

// NewLibraryPanel creates a new Grafana library panel.
func (c *Client) NewLibraryPanel(panel LibraryPanel) (*LibraryPanel, error) {
panel.Kind = int64(1)
data, err := json.Marshal(panel)
if err != nil {
return nil, err
}

resp := &LibraryPanelCreateResponse{}
err = c.request("POST", "/api/library-elements", nil, bytes.NewBuffer(data), &resp)
if err != nil {
return nil, err
}

return &resp.Result, err
}

// 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))
}

// 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))
}

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
}

// PatchLibraryPanel updates one or more properties of an existing panel that matches the specified UID.
func (c *Client) PatchLibraryPanel(uid string, panel *LibraryPanel) (*LibraryPanel, error) {
path := fmt.Sprintf("/api/library-elements/%s", uid)
data, err := json.Marshal(panel)
if err != nil {
return nil, err
}

resp := &LibraryPanelCreateResponse{}
err = c.request("PATCH", path, nil, bytes.NewBuffer(data), &resp)
if err != nil {
return nil, err
}

return &resp.Result, err
}

// DeleteLibraryPanel deletes a panel by UID.
func (c *Client) DeleteLibraryPanel(uid string) (*LibraryPanelDeleteResponse, error) {
path := fmt.Sprintf("/api/library-elements/%s", uid)

resp := &LibraryPanelDeleteResponse{}
err := c.request("DELETE", path, nil, bytes.NewBuffer(nil), &resp)
if err != nil {
return nil, err
}

return resp, err
}

// LibraryPanelConnections gets library panel connections by UID.
func (c *Client) LibraryPanelConnections(uid string) (*[]LibraryPanelConnection, error) {
path := fmt.Sprintf("/api/library-elements/%s/connections", uid)

resp := struct {
Result []LibraryPanelConnection `json:"result"`
}{}

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

return &resp.Result, err
}

// LibraryPanelConnectedDashboards gets Dashboards using this Library Panel.
func (c *Client) LibraryPanelConnectedDashboards(uid string) ([]FolderDashboardSearchResponse, error) {
connections, err := c.LibraryPanelConnections(uid)
if err != nil {
return nil, err
}

var dashboardIds []int64
for _, connection := range *connections {
dashboardIds = append(dashboardIds, connection.DashboardID)
}

return c.DashboardsByIDs(dashboardIds)
}
Loading