refactor(grafana): replace dashboard info with import compatible layout

This commit is contained in:
Tom Neuber 2024-07-27 18:53:38 +02:00
parent 4aad919153
commit 577f821b9d
Signed by: tom
GPG key ID: F17EFE4272D89FF6
8 changed files with 173 additions and 110 deletions

View file

@ -67,8 +67,8 @@ func NewClient(endpoint string, options ...ClientOption) *Client {
return client return client
} }
func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) { func (c *Client) newRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
url := fmt.Sprintf("%s/%s", c.endpoint, path) url := fmt.Sprintf("%s%s", c.endpoint, path)
req, err := http.NewRequest(method, url, body) req, err := http.NewRequest(method, url, body)
if err != nil { if err != nil {
return nil, err return nil, err
@ -102,7 +102,7 @@ type ErrorResponse struct {
TraceID uint TraceID uint
} }
func (c *Client) Do(req *http.Request, v any) (*Response, error) { func (c *Client) do(req *http.Request, v any) (*Response, error) {
httpResp, err := c.httpClient.Do(req) httpResp, err := c.httpClient.Do(req)
resp := &Response{Response: httpResp} resp := &Response{Response: httpResp}
if err != nil { if err != nil {

View file

@ -11,7 +11,7 @@ import (
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana/schema" "git.ar21.de/yolokube/grafana-backuper/pkg/grafana/schema"
) )
type DashboardMeta struct { type Dashboard struct {
IsStarred bool IsStarred bool
Type string Type string
CanSave bool CanSave bool
@ -54,20 +54,20 @@ type DashboardClient struct {
client *Client client *Client
} }
func (c *DashboardClient) Get(ctx context.Context, uid string) (*DashboardMeta, *Response, error) { 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) req, err := c.client.newRequest(ctx, "GET", fmt.Sprintf("/dashboards/uid/%s", uid), nil)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
var body schema.DashboardMeta var body schema.Dashboard
resp, err := c.client.Do(req, &body) resp, err := c.client.do(req, &body)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
return DashboardMetaFromSchema(body), resp, nil return DashboardFromSchema(body), resp, nil
} }
type DashboardCreateOpts struct { type DashboardCreateOpts struct {
@ -120,13 +120,13 @@ func (c *DashboardClient) Create(
return nil, nil, err return nil, nil, err
} }
req, err := c.client.NewRequest(ctx, "POST", "/dashboards/db", bytes.NewReader(reqBodyData)) req, err := c.client.newRequest(ctx, "POST", "/dashboards/db", bytes.NewReader(reqBodyData))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
var respBody schema.DashboardCreateResponse var respBody schema.DashboardCreateResponse
resp, err := c.client.Do(req, &respBody) resp, err := c.client.do(req, &respBody)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
@ -135,10 +135,10 @@ func (c *DashboardClient) Create(
} }
func (c *DashboardClient) Delete(ctx context.Context, uid string) (*Response, error) { func (c *DashboardClient) Delete(ctx context.Context, uid string) (*Response, error) {
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/dashboards/uid/%s", uid), nil) req, err := c.client.newRequest(ctx, "DELETE", fmt.Sprintf("/dashboards/uid/%s", uid), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return c.client.Do(req, nil) return c.client.do(req, nil)
} }

View file

@ -51,12 +51,7 @@ func (c *DashboardVersionClient) GetByID(
return nil, nil, err return nil, nil, err
} }
dashboards, resp, err := c.get(ctx, dashboardVersionURL, params...) return c.get(ctx, dashboardVersionURL, params...)
if err != nil || len(dashboards) < 1 {
return nil, resp, err
}
return dashboards[0], resp, nil
} }
func (c *DashboardVersionClient) GetByUID( func (c *DashboardVersionClient) GetByUID(
@ -65,17 +60,12 @@ func (c *DashboardVersionClient) GetByUID(
version uint, version uint,
params ...DashboardVersionParam, params ...DashboardVersionParam,
) (*DashboardVersion, *Response, error) { ) (*DashboardVersion, *Response, error) {
dashboardVersionURL, err := url.Parse(fmt.Sprintf("/dashboards/id/%s/versions/%d", uid, version)) dashboardVersionURL, err := url.Parse(fmt.Sprintf("/dashboards/uid/%s/versions/%d", uid, version))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
dashboards, resp, err := c.get(ctx, dashboardVersionURL, params...) return c.get(ctx, dashboardVersionURL, params...)
if err != nil || len(dashboards) < 1 {
return nil, resp, err
}
return dashboards[0], resp, nil
} }
func (c *DashboardVersionClient) Get( func (c *DashboardVersionClient) Get(
@ -100,7 +90,7 @@ func (c *DashboardVersionClient) ListByID(
return nil, nil, err return nil, nil, err
} }
return c.get(ctx, dashboardVersionURL, params...) return c.list(ctx, dashboardVersionURL, params...)
} }
func (c *DashboardVersionClient) ListByUID( func (c *DashboardVersionClient) ListByUID(
@ -113,7 +103,7 @@ func (c *DashboardVersionClient) ListByUID(
return nil, nil, err return nil, nil, err
} }
return c.get(ctx, dashboardVersionURL, params...) return c.list(ctx, dashboardVersionURL, params...)
} }
func (c *DashboardVersionClient) List( func (c *DashboardVersionClient) List(
@ -131,6 +121,36 @@ func (c *DashboardVersionClient) get(
ctx context.Context, ctx context.Context,
dashboardVersionURL *url.URL, dashboardVersionURL *url.URL,
params ...DashboardVersionParam, params ...DashboardVersionParam,
) (*DashboardVersion, *Response, error) {
if len(params) > 0 {
query := dashboardVersionURL.Query()
for _, param := range params {
param(&query)
}
dashboardVersionURL.RawQuery = query.Encode()
}
req, err := c.client.newRequest(ctx, "GET", dashboardVersionURL.String(), nil)
if err != nil {
return nil, nil, err
}
var body schema.DashboardVersion
resp, err := c.client.do(req, &body)
if err != nil {
return nil, resp, err
}
return DashboardVersionFromSchema(body), resp, nil
}
func (c *DashboardVersionClient) list(
ctx context.Context,
dashboardVersionURL *url.URL,
params ...DashboardVersionParam,
) ([]*DashboardVersion, *Response, error) { ) ([]*DashboardVersion, *Response, error) {
if len(params) > 0 { if len(params) > 0 {
query := dashboardVersionURL.Query() query := dashboardVersionURL.Query()
@ -142,20 +162,20 @@ func (c *DashboardVersionClient) get(
dashboardVersionURL.RawQuery = query.Encode() dashboardVersionURL.RawQuery = query.Encode()
} }
req, err := c.client.NewRequest(ctx, "GET", dashboardVersionURL.String(), nil) req, err := c.client.newRequest(ctx, "GET", dashboardVersionURL.String(), nil)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
var body schema.DashboardVersionListResponse var body schema.DashboardVersionListResponse
resp, err := c.client.Do(req, &body) resp, err := c.client.do(req, &body)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
dashboardVersions := make([]*DashboardVersion, 0, len(body.DashboardVersions)) dashboardVersions := make([]*DashboardVersion, 0, len(body))
for _, dashboardVersion := range body.DashboardVersions { for _, dashboardVersion := range body {
dashboardVersions = append(dashboardVersions, DashboardVersionFromSchema(dashboardVersion)) dashboardVersions = append(dashboardVersions, DashboardVersionFromSchema(dashboardVersion))
} }

View file

@ -15,41 +15,41 @@ func DashboardCreateResponseFromSchema(source schema.DashboardCreateResponse) *D
} }
} }
func DashboardMetaFromSchema(source schema.DashboardMeta) *DashboardMeta { func DashboardFromSchema(source schema.Dashboard) *Dashboard {
return &DashboardMeta{ return &Dashboard{
IsStarred: source.IsStarred, IsStarred: source.Meta.IsStarred,
Type: source.Type, Type: source.Meta.Type,
CanSave: source.CanSave, CanSave: source.Meta.CanSave,
CanEdit: source.CanEdit, CanEdit: source.Meta.CanEdit,
CanAdmin: source.CanAdmin, CanAdmin: source.Meta.CanAdmin,
CanStar: source.CanStar, CanStar: source.Meta.CanStar,
CanDelete: source.CanDelete, CanDelete: source.Meta.CanDelete,
Slug: source.Slug, Slug: source.Meta.Slug,
URL: source.URL, URL: source.Meta.URL,
Expires: source.Expires, Expires: source.Meta.Expires,
Created: source.Created, Created: source.Meta.Created,
Updated: source.Updated, Updated: source.Meta.Updated,
UpdatedBy: source.UpdatedBy, UpdatedBy: source.Meta.UpdatedBy,
CreatedBy: source.CreatedBy, CreatedBy: source.Meta.CreatedBy,
Version: source.Version, Version: source.Meta.Version,
HasACL: source.HasACL, HasACL: source.Meta.HasACL,
IsFolder: source.IsFolder, IsFolder: source.Meta.IsFolder,
FolderID: source.FolderID, FolderID: source.Meta.FolderID,
FolderUID: source.FolderUID, FolderUID: source.Meta.FolderUID,
FolderTitle: source.FolderTitle, FolderTitle: source.Meta.FolderTitle,
FolderURL: source.FolderURL, FolderURL: source.Meta.FolderURL,
Provisioned: source.Provisioned, Provisioned: source.Meta.Provisioned,
ProvisionedExternalID: source.ProvisionedExternalID, ProvisionedExternalID: source.Meta.ProvisionedExternalID,
AnnotationsPermissions: AnnotationsPermissions{ AnnotationsPermissions: AnnotationsPermissions{
Dashboard: AnnotationPermissions{ Dashboard: AnnotationPermissions{
CanAdd: source.AnnotationsPermissions.Dashboard.CanAdd, CanAdd: source.Meta.AnnotationsPermissions.Dashboard.CanAdd,
CanEdit: source.AnnotationsPermissions.Dashboard.CanEdit, CanEdit: source.Meta.AnnotationsPermissions.Dashboard.CanEdit,
CanDelete: source.AnnotationsPermissions.Dashboard.CanDelete, CanDelete: source.Meta.AnnotationsPermissions.Dashboard.CanDelete,
}, },
Organization: AnnotationPermissions{ Organization: AnnotationPermissions{
CanAdd: source.AnnotationsPermissions.Organization.CanAdd, CanAdd: source.Meta.AnnotationsPermissions.Organization.CanAdd,
CanEdit: source.AnnotationsPermissions.Organization.CanEdit, CanEdit: source.Meta.AnnotationsPermissions.Organization.CanEdit,
CanDelete: source.AnnotationsPermissions.Organization.CanDelete, CanDelete: source.Meta.AnnotationsPermissions.Organization.CanDelete,
}, },
}, },
Dashboard: source.Dashboard, Dashboard: source.Dashboard,
@ -96,3 +96,46 @@ func SearchResultFromSchema(source schema.SearchResult) *SearchResult {
FolderURL: source.FolderURL, FolderURL: source.FolderURL,
} }
} }
func SchemaFromDashboardMeta(dm *Dashboard) schema.Dashboard {
return schema.Dashboard{
Meta: schema.DashboardMeta{
IsStarred: dm.IsStarred,
Type: dm.Type,
CanSave: dm.CanSave,
CanEdit: dm.CanEdit,
CanAdmin: dm.CanAdmin,
CanStar: dm.CanStar,
CanDelete: dm.CanDelete,
Slug: dm.Slug,
URL: dm.URL,
Expires: dm.Expires,
Created: dm.Created,
Updated: dm.Updated,
UpdatedBy: dm.UpdatedBy,
CreatedBy: dm.CreatedBy,
Version: dm.Version,
HasACL: dm.HasACL,
IsFolder: dm.IsFolder,
FolderID: dm.FolderID,
FolderUID: dm.FolderUID,
FolderTitle: dm.FolderTitle,
FolderURL: dm.FolderURL,
Provisioned: dm.Provisioned,
ProvisionedExternalID: dm.ProvisionedExternalID,
AnnotationsPermissions: schema.AnnotationsPermissions{
Dashboard: schema.AnnotationPermissions{
CanAdd: dm.AnnotationsPermissions.Dashboard.CanAdd,
CanEdit: dm.AnnotationsPermissions.Dashboard.CanEdit,
CanDelete: dm.AnnotationsPermissions.Dashboard.CanDelete,
},
Organization: schema.AnnotationPermissions{
CanAdd: dm.AnnotationsPermissions.Organization.CanAdd,
CanEdit: dm.AnnotationsPermissions.Organization.CanEdit,
CanDelete: dm.AnnotationsPermissions.Organization.CanDelete,
},
},
},
Dashboard: dm.Dashboard,
}
}

View file

@ -19,41 +19,45 @@ type DashboardCreateResponse struct {
Slug string `json:"slug"` Slug string `json:"slug"`
} }
type DashboardMeta struct { type Dashboard struct {
IsStarred bool `json:"isStarred"` Meta DashboardMeta `json:"meta"`
Type string `json:"type"` Dashboard any `json:"dashboard"`
CanSave bool `json:"canSave"` }
CanEdit bool `json:"canEdit"`
CanAdmin bool `json:"canAdmin"` type DashboardMeta struct {
CanStar bool `json:"canStar"` IsStarred bool `json:"isStarred"`
CanDelete bool `json:"canDelete"` Type string `json:"type"`
Slug string `json:"slug"` CanSave bool `json:"canSave"`
URL string `json:"url"` CanEdit bool `json:"canEdit"`
Expires time.Time `json:"expires"` CanAdmin bool `json:"canAdmin"`
Created time.Time `json:"created"` CanStar bool `json:"canStar"`
Updated time.Time `json:"updated"` CanDelete bool `json:"canDelete"`
UpdatedBy string `json:"updatedBy"` Slug string `json:"slug"`
CreatedBy string `json:"createdBy"` URL string `json:"url"`
Version uint `json:"version"` Expires time.Time `json:"expires"`
HasACL bool `json:"hasAcl"` Created time.Time `json:"created"`
IsFolder bool `json:"isFolder"` Updated time.Time `json:"updated"`
FolderID uint `json:"folderId"` UpdatedBy string `json:"updatedBy"`
FolderUID string `json:"folderUid"` CreatedBy string `json:"createdBy"`
FolderTitle string `json:"folderTitle"` Version uint `json:"version"`
FolderURL string `json:"folderUrl"` HasACL bool `json:"hasAcl"`
Provisioned bool `json:"provisioned"` IsFolder bool `json:"isFolder"`
ProvisionedExternalID string `json:"provisionedExternalId"` FolderID uint `json:"folderId"`
AnnotationsPermissions struct { FolderUID string `json:"folderUid"`
Dashboard struct { FolderTitle string `json:"folderTitle"`
CanAdd bool `json:"canAdd"` FolderURL string `json:"folderUrl"`
CanEdit bool `json:"canEdit"` Provisioned bool `json:"provisioned"`
CanDelete bool `json:"canDelete"` ProvisionedExternalID string `json:"provisionedExternalId"`
} `json:"dashboard"` AnnotationsPermissions AnnotationsPermissions `json:"annotationsPermissions"`
Organization struct { }
CanAdd bool `json:"canAdd"`
CanEdit bool `json:"canEdit"` type AnnotationsPermissions struct {
CanDelete bool `json:"canDelete"` Dashboard AnnotationPermissions `json:"dashboard"`
} `json:"organization"` Organization AnnotationPermissions `json:"organization"`
} `json:"annotationsPermissions"` }
Dashboard any `json:"dashboard"`
type AnnotationPermissions struct {
CanAdd bool `json:"canAdd"`
CanEdit bool `json:"canEdit"`
CanDelete bool `json:"canDelete"`
} }

View file

@ -15,6 +15,4 @@ type DashboardVersion struct {
Data any `json:"data"` Data any `json:"data"`
} }
type DashboardVersionListResponse struct { type DashboardVersionListResponse []DashboardVersion
DashboardVersions []DashboardVersion
}

View file

@ -17,6 +17,4 @@ type SearchResult struct {
FolderURL string `json:"folderUrl"` FolderURL string `json:"folderUrl"`
} }
type SearchResultListResponse struct { type SearchResultListResponse []SearchResult
SearchResults []SearchResult
}

View file

@ -84,20 +84,20 @@ func (c *SearchClient) Search(ctx context.Context, params ...SearchParam) ([]*Se
searchURL.RawQuery = query.Encode() searchURL.RawQuery = query.Encode()
} }
req, err := c.client.NewRequest(ctx, "GET", searchURL.String(), nil) req, err := c.client.newRequest(ctx, "GET", searchURL.String(), nil)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
var body schema.SearchResultListResponse var body schema.SearchResultListResponse
resp, err := c.client.Do(req, &body) resp, err := c.client.do(req, &body)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
searchResults := make([]*SearchResult, 0, len(body.SearchResults)) searchResults := make([]*SearchResult, 0, len(body))
for _, searchResult := range body.SearchResults { for _, searchResult := range body {
searchResults = append(searchResults, SearchResultFromSchema(searchResult)) searchResults = append(searchResults, SearchResultFromSchema(searchResult))
} }