2024-02-15 01:12:57 +01:00
|
|
|
package grafana
|
|
|
|
|
|
|
|
import (
|
2024-02-16 21:56:27 +01:00
|
|
|
"bytes"
|
2024-02-15 01:12:57 +01:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-06-23 02:37:22 +02:00
|
|
|
"errors"
|
2024-02-15 01:12:57 +01:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2024-06-23 02:37:22 +02:00
|
|
|
|
|
|
|
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana/schema"
|
2024-02-15 01:12:57 +01:00
|
|
|
)
|
|
|
|
|
2024-07-27 18:53:38 +02:00
|
|
|
type Dashboard struct {
|
2024-06-23 02:37:22 +02:00
|
|
|
IsStarred bool
|
|
|
|
Type string
|
|
|
|
CanSave bool
|
|
|
|
CanEdit bool
|
|
|
|
CanAdmin bool
|
|
|
|
CanStar bool
|
|
|
|
CanDelete bool
|
|
|
|
Slug string
|
|
|
|
URL string
|
|
|
|
Expires time.Time
|
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
|
|
|
UpdatedBy string
|
|
|
|
CreatedBy string
|
|
|
|
Version uint
|
|
|
|
HasACL bool
|
|
|
|
IsFolder bool
|
|
|
|
FolderID uint
|
|
|
|
FolderUID string
|
|
|
|
FolderTitle string
|
|
|
|
FolderURL string
|
|
|
|
Provisioned bool
|
|
|
|
ProvisionedExternalID string
|
|
|
|
AnnotationsPermissions AnnotationsPermissions
|
|
|
|
Dashboard any
|
2024-02-16 21:56:27 +01:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:37:22 +02:00
|
|
|
type AnnotationsPermissions struct {
|
|
|
|
Dashboard AnnotationPermissions
|
|
|
|
Organization AnnotationPermissions
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:37:22 +02:00
|
|
|
type AnnotationPermissions struct {
|
|
|
|
CanAdd bool
|
|
|
|
CanEdit bool
|
|
|
|
CanDelete bool
|
2024-02-16 21:56:27 +01:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:37:22 +02:00
|
|
|
type DashboardClient struct {
|
|
|
|
client *Client
|
|
|
|
}
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-07-27 18:53:38 +02:00
|
|
|
func (c *DashboardClient) Get(ctx context.Context, uid string) (*Dashboard, *Response, error) {
|
|
|
|
req, err := c.client.newRequest(ctx, "GET", fmt.Sprintf("/dashboards/uid/%s", uid), nil)
|
2024-02-16 21:56:27 +01:00
|
|
|
if err != nil {
|
2024-06-23 02:37:22 +02:00
|
|
|
return nil, nil, err
|
2024-02-16 21:56:27 +01:00
|
|
|
}
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-07-27 18:53:38 +02:00
|
|
|
var body schema.Dashboard
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-07-27 18:53:38 +02:00
|
|
|
resp, err := c.client.do(req, &body)
|
2024-06-23 02:37:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
2024-02-16 21:56:27 +01:00
|
|
|
}
|
2024-06-19 22:07:51 +02:00
|
|
|
|
2024-07-27 18:53:38 +02:00
|
|
|
return DashboardFromSchema(body), resp, nil
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:37:22 +02:00
|
|
|
type DashboardCreateOpts struct {
|
|
|
|
Dashboard any
|
|
|
|
FolderID uint
|
|
|
|
FolderUID string
|
|
|
|
Message string
|
|
|
|
Overwrite bool
|
|
|
|
}
|
2024-02-15 01:12:57 +01:00
|
|
|
|
2024-06-23 02:37:22 +02:00
|
|
|
func (o DashboardCreateOpts) Validate() error {
|
|
|
|
if o.FolderUID == "" && o.FolderID == 0 {
|
|
|
|
return errors.New("folder ID or UID missing")
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
2024-06-23 02:37:22 +02:00
|
|
|
if o.Dashboard == nil {
|
|
|
|
return errors.New("dashboard is nil")
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
2024-06-23 02:37:22 +02:00
|
|
|
return nil
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:37:22 +02:00
|
|
|
type DashboardCreateResponse struct {
|
|
|
|
DashboardID uint
|
|
|
|
DashboardUID string
|
|
|
|
URL string
|
|
|
|
Status string
|
|
|
|
Version uint
|
|
|
|
Slug string
|
2024-02-16 21:56:27 +01:00
|
|
|
}
|
|
|
|
|
2024-06-23 02:37:22 +02:00
|
|
|
func (c *DashboardClient) Create(
|
|
|
|
ctx context.Context,
|
|
|
|
opts DashboardCreateOpts,
|
|
|
|
) (*DashboardCreateResponse, *Response, error) {
|
|
|
|
if err := opts.Validate(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2024-02-16 21:56:27 +01:00
|
|
|
|
2024-06-23 02:37:22 +02:00
|
|
|
var reqBody schema.DashboardCreateRequest
|
|
|
|
reqBody.Dashboard = opts.Dashboard
|
|
|
|
reqBody.Overwrite = opts.Overwrite
|
|
|
|
reqBody.Message = opts.Message
|
|
|
|
if opts.FolderUID != "" {
|
|
|
|
reqBody.FolderUID = opts.FolderUID
|
|
|
|
} else {
|
|
|
|
reqBody.FolderID = opts.FolderID
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
2024-06-23 02:37:22 +02:00
|
|
|
|
|
|
|
reqBodyData, err := json.Marshal(reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2024-02-15 01:12:57 +01:00
|
|
|
}
|
2024-02-16 21:56:27 +01:00
|
|
|
|
2024-07-27 18:53:38 +02:00
|
|
|
req, err := c.client.newRequest(ctx, "POST", "/dashboards/db", bytes.NewReader(reqBodyData))
|
2024-06-23 02:37:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var respBody schema.DashboardCreateResponse
|
2024-07-27 18:53:38 +02:00
|
|
|
resp, err := c.client.do(req, &respBody)
|
2024-06-23 02:37:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return DashboardCreateResponseFromSchema(respBody), resp, nil
|
|
|
|
}
|
2024-02-16 21:56:27 +01:00
|
|
|
|
2024-06-23 02:37:22 +02:00
|
|
|
func (c *DashboardClient) Delete(ctx context.Context, uid string) (*Response, error) {
|
2024-07-27 18:53:38 +02:00
|
|
|
req, err := c.client.newRequest(ctx, "DELETE", fmt.Sprintf("/dashboards/uid/%s", uid), nil)
|
2024-02-16 21:56:27 +01:00
|
|
|
if err != nil {
|
2024-06-23 02:37:22 +02:00
|
|
|
return nil, err
|
2024-02-16 21:56:27 +01:00
|
|
|
}
|
|
|
|
|
2024-07-27 18:53:38 +02:00
|
|
|
return c.client.do(req, nil)
|
2024-02-16 21:56:27 +01:00
|
|
|
}
|