|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// LibraryPanelMetaUser represents the Grafana library panel createdBy and updatedBy fields |
| 11 | +type LibraryPanelMetaUser struct { |
| 12 | + ID int64 `json:"id"` |
| 13 | + Name string `json:"name"` |
| 14 | + AvatarURL string `json:"folderId"` |
| 15 | +} |
| 16 | + |
| 17 | +// LibraryPanelMeta represents Grafana library panel metadata. |
| 18 | +type LibraryPanelMeta struct { |
| 19 | + FolderName string `json:"folderName,,omitempty"` |
| 20 | + FolderUID string `json:"folderUid,omitempty"` |
| 21 | + ConnectedDashboards int64 `json:"connectedDashboards,omitempty"` |
| 22 | + Created time.Time `json:"created,omitempty"` |
| 23 | + Updated time.Time `json:"updated,omitempty"` |
| 24 | + CreatedBy LibraryPanelMetaUser `json:"createdBy,omitempty"` |
| 25 | + UpdatedBy LibraryPanelMetaUser `json:"updatedBy,omitempty"` |
| 26 | +} |
| 27 | + |
| 28 | +// LibraryPanel represents a Grafana library panel. |
| 29 | +type LibraryPanel struct { |
| 30 | + Folder int64 `json:"folderId,omitempty"` |
| 31 | + Name string `json:"name"` |
| 32 | + Model map[string]interface{} `json:"model"` |
| 33 | + Type string `json:"type,omitempty"` |
| 34 | + Description string `json:"description,omitempty"` |
| 35 | + ID int64 `json:"id,omitempty"` |
| 36 | + Kind int64 `json:"kind,omitempty"` |
| 37 | + OrgID int64 `json:"orgId,omitempty"` |
| 38 | + UID string `json:"uid,omitempty"` |
| 39 | + Version int64 `json:"version,omitempty"` |
| 40 | + Meta LibraryPanelMeta `json:"meta,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +// LibraryPanelCreateResponse represents the Grafana API response to creating or saving a library panel. |
| 44 | +type LibraryPanelCreateResponse struct { |
| 45 | + Result LibraryPanel `json:"result"` |
| 46 | +} |
| 47 | + |
| 48 | +// LibraryPanelGetAllResponse represents the Grafana API response to getting all library panels. |
| 49 | +type LibraryPanelGetAllResponse struct { |
| 50 | + TotalCount int64 `json:"totalCount"` |
| 51 | + Page int64 `json:"page"` |
| 52 | + PerPage int64 `json:"perPage"` |
| 53 | + Elements []LibraryPanel `json:"elements"` |
| 54 | +} |
| 55 | + |
| 56 | +// LibraryPanelDeleteResponse represents the Grafana API response to deleting a library panel. |
| 57 | +type LibraryPanelDeleteResponse struct { |
| 58 | + Message string `json:"message"` |
| 59 | + ID int64 `json:"id,omitempty"` |
| 60 | +} |
| 61 | + |
| 62 | +// LibraryPanelConnection represents a Grafana connection between a library panel and a dashboard. |
| 63 | +type LibraryPanelConnection struct { |
| 64 | + ID int64 `json:"id"` |
| 65 | + Kind int64 `json:"kind"` |
| 66 | + PanelID int64 `json:"elementId"` |
| 67 | + DashboardID int64 `json:"connectionId"` |
| 68 | + Created time.Time `json:"created"` |
| 69 | + CreatedBy LibraryPanelMetaUser `json:"createdBy"` |
| 70 | +} |
| 71 | + |
| 72 | +// NewLibraryPanel creates a new Grafana library panel. |
| 73 | +func (c *Client) NewLibraryPanel(panel LibraryPanel) (*LibraryPanel, error) { |
| 74 | + panel.Kind = int64(1) |
| 75 | + data, err := json.Marshal(panel) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + resp := &LibraryPanelCreateResponse{} |
| 81 | + err = c.request("POST", "/api/library-elements", nil, bytes.NewBuffer(data), &resp) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + return &resp.Result, err |
| 87 | +} |
| 88 | + |
| 89 | +// Dashboards fetches and returns all dashboards. |
| 90 | +func (c *Client) LibraryPanels() ([]LibraryPanel, error) { |
| 91 | + resp := &struct { |
| 92 | + Result LibraryPanelGetAllResponse `json:"result"` |
| 93 | + }{} |
| 94 | + err := c.request("GET", "/api/library-elements", nil, nil, &resp) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + return resp.Result.Elements, err |
| 100 | +} |
| 101 | + |
| 102 | +// LibraryPanelByUID gets a library panel by UID. |
| 103 | +func (c *Client) LibraryPanelByUID(uid string) (*LibraryPanel, error) { |
| 104 | + return c.panel(fmt.Sprintf("/api/library-elements/%s", uid)) |
| 105 | +} |
| 106 | + |
| 107 | +// LibraryPanelByName gets a library panel by name. |
| 108 | +func (c *Client) LibraryPanelByName(name string) (*LibraryPanel, error) { |
| 109 | + return c.panel(fmt.Sprintf("/api/library-elements/name/%s", name)) |
| 110 | +} |
| 111 | + |
| 112 | +func (c *Client) panel(path string) (*LibraryPanel, error) { |
| 113 | + resp := &LibraryPanelCreateResponse{} |
| 114 | + err := c.request("GET", path, nil, nil, &resp) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + return &resp.Result, err |
| 120 | +} |
| 121 | + |
| 122 | +// PatchLibraryPanel updates one or more properties of an existing panel that matches the specified UID. |
| 123 | +func (c *Client) PatchLibraryPanel(uid string, panel LibraryPanel) (*LibraryPanel, error) { |
| 124 | + path := fmt.Sprintf("/api/library-elements/%s", uid) |
| 125 | + panel.Kind = int64(1) |
| 126 | + |
| 127 | + // if Version not specified, get current version from API |
| 128 | + if panel.Version == int64(0) { |
| 129 | + remotePanel, err := c.LibraryPanelByUID(panel.UID) |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + panel.Version = remotePanel.Version |
| 134 | + } |
| 135 | + |
| 136 | + data, err := json.Marshal(panel) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + resp := &LibraryPanelCreateResponse{} |
| 142 | + err = c.request("PATCH", path, nil, bytes.NewBuffer(data), &resp) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + return &resp.Result, err |
| 148 | +} |
| 149 | + |
| 150 | +// DeleteLibraryPanel deletes a panel by UID. |
| 151 | +func (c *Client) DeleteLibraryPanel(uid string) (*LibraryPanelDeleteResponse, error) { |
| 152 | + path := fmt.Sprintf("/api/library-elements/%s", uid) |
| 153 | + |
| 154 | + resp := &LibraryPanelDeleteResponse{} |
| 155 | + err := c.request("DELETE", path, nil, bytes.NewBuffer(nil), &resp) |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + |
| 160 | + return resp, err |
| 161 | +} |
| 162 | + |
| 163 | +// LibraryPanelConnections gets library panel connections by UID. |
| 164 | +func (c *Client) LibraryPanelConnections(uid string) (*[]LibraryPanelConnection, error) { |
| 165 | + path := fmt.Sprintf("/api/library-elements/%s/connections", uid) |
| 166 | + |
| 167 | + resp := struct { |
| 168 | + Result []LibraryPanelConnection `json:"result"` |
| 169 | + }{} |
| 170 | + |
| 171 | + err := c.request("POST", path, nil, bytes.NewBuffer(nil), &resp) |
| 172 | + if err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + |
| 176 | + return &resp.Result, err |
| 177 | +} |
| 178 | + |
| 179 | +// LibraryPanelConnectedDashboards gets Dashboards using this Library Panel. |
| 180 | +func (c *Client) LibraryPanelConnectedDashboards(uid string) ([]FolderDashboardSearchResponse, error) { |
| 181 | + connections, err := c.LibraryPanelConnections(uid) |
| 182 | + if err != nil { |
| 183 | + return nil, err |
| 184 | + } |
| 185 | + |
| 186 | + var dashboardIds []int64 |
| 187 | + for _, connection := range *connections { |
| 188 | + dashboardIds = append(dashboardIds, connection.DashboardID) |
| 189 | + } |
| 190 | + |
| 191 | + return c.DashboardsByIDs(dashboardIds) |
| 192 | +} |
0 commit comments