WIP: Refactor entire project #16
8 changed files with 173 additions and 110 deletions
|
@ -67,8 +67,8 @@ func NewClient(endpoint string, options ...ClientOption) *Client {
|
|||
return client
|
||||
}
|
||||
|
||||
func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
|
||||
url := fmt.Sprintf("%s/%s", c.endpoint, path)
|
||||
func (c *Client) newRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
|
||||
url := fmt.Sprintf("%s%s", c.endpoint, path)
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -102,7 +102,7 @@ type ErrorResponse struct {
|
|||
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)
|
||||
resp := &Response{Response: httpResp}
|
||||
if err != nil {
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
"git.ar21.de/yolokube/grafana-backuper/pkg/grafana/schema"
|
||||
)
|
||||
|
||||
type DashboardMeta struct {
|
||||
type Dashboard struct {
|
||||
IsStarred bool
|
||||
Type string
|
||||
CanSave bool
|
||||
|
@ -54,20 +54,20 @@ type DashboardClient struct {
|
|||
client *Client
|
||||
}
|
||||
|
||||
func (c *DashboardClient) Get(ctx context.Context, uid string) (*DashboardMeta, *Response, error) {
|
||||
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/dashboards/uid/%s", uid), nil)
|
||||
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)
|
||||
if err != nil {
|
||||
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 {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return DashboardMetaFromSchema(body), resp, nil
|
||||
return DashboardFromSchema(body), resp, nil
|
||||
}
|
||||
|
||||
type DashboardCreateOpts struct {
|
||||
|
@ -120,13 +120,13 @@ func (c *DashboardClient) Create(
|
|||
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 {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var respBody schema.DashboardCreateResponse
|
||||
resp, err := c.client.Do(req, &respBody)
|
||||
resp, err := c.client.do(req, &respBody)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
@ -135,10 +135,10 @@ func (c *DashboardClient) Create(
|
|||
}
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c.client.Do(req, nil)
|
||||
return c.client.do(req, nil)
|
||||
}
|
||||
|
|
|
@ -51,12 +51,7 @@ func (c *DashboardVersionClient) GetByID(
|
|||
return nil, nil, err
|
||||
}
|
||||
|
||||
dashboards, resp, err := c.get(ctx, dashboardVersionURL, params...)
|
||||
if err != nil || len(dashboards) < 1 {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return dashboards[0], resp, nil
|
||||
return c.get(ctx, dashboardVersionURL, params...)
|
||||
}
|
||||
|
||||
func (c *DashboardVersionClient) GetByUID(
|
||||
|
@ -65,17 +60,12 @@ func (c *DashboardVersionClient) GetByUID(
|
|||
version uint,
|
||||
params ...DashboardVersionParam,
|
||||
) (*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 {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
dashboards, resp, err := c.get(ctx, dashboardVersionURL, params...)
|
||||
if err != nil || len(dashboards) < 1 {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return dashboards[0], resp, nil
|
||||
return c.get(ctx, dashboardVersionURL, params...)
|
||||
}
|
||||
|
||||
func (c *DashboardVersionClient) Get(
|
||||
|
@ -100,7 +90,7 @@ func (c *DashboardVersionClient) ListByID(
|
|||
return nil, nil, err
|
||||
}
|
||||
|
||||
return c.get(ctx, dashboardVersionURL, params...)
|
||||
return c.list(ctx, dashboardVersionURL, params...)
|
||||
}
|
||||
|
||||
func (c *DashboardVersionClient) ListByUID(
|
||||
|
@ -113,7 +103,7 @@ func (c *DashboardVersionClient) ListByUID(
|
|||
return nil, nil, err
|
||||
}
|
||||
|
||||
return c.get(ctx, dashboardVersionURL, params...)
|
||||
return c.list(ctx, dashboardVersionURL, params...)
|
||||
}
|
||||
|
||||
func (c *DashboardVersionClient) List(
|
||||
|
@ -131,6 +121,36 @@ func (c *DashboardVersionClient) get(
|
|||
ctx context.Context,
|
||||
dashboardVersionURL *url.URL,
|
||||
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) {
|
||||
if len(params) > 0 {
|
||||
query := dashboardVersionURL.Query()
|
||||
|
@ -142,20 +162,20 @@ func (c *DashboardVersionClient) get(
|
|||
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 {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var body schema.DashboardVersionListResponse
|
||||
|
||||
resp, err := c.client.Do(req, &body)
|
||||
resp, err := c.client.do(req, &body)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
dashboardVersions := make([]*DashboardVersion, 0, len(body.DashboardVersions))
|
||||
for _, dashboardVersion := range body.DashboardVersions {
|
||||
dashboardVersions := make([]*DashboardVersion, 0, len(body))
|
||||
for _, dashboardVersion := range body {
|
||||
dashboardVersions = append(dashboardVersions, DashboardVersionFromSchema(dashboardVersion))
|
||||
}
|
||||
|
||||
|
|
|
@ -15,41 +15,41 @@ func DashboardCreateResponseFromSchema(source schema.DashboardCreateResponse) *D
|
|||
}
|
||||
}
|
||||
|
||||
func DashboardMetaFromSchema(source schema.DashboardMeta) *DashboardMeta {
|
||||
return &DashboardMeta{
|
||||
IsStarred: source.IsStarred,
|
||||
Type: source.Type,
|
||||
CanSave: source.CanSave,
|
||||
CanEdit: source.CanEdit,
|
||||
CanAdmin: source.CanAdmin,
|
||||
CanStar: source.CanStar,
|
||||
CanDelete: source.CanDelete,
|
||||
Slug: source.Slug,
|
||||
URL: source.URL,
|
||||
Expires: source.Expires,
|
||||
Created: source.Created,
|
||||
Updated: source.Updated,
|
||||
UpdatedBy: source.UpdatedBy,
|
||||
CreatedBy: source.CreatedBy,
|
||||
Version: source.Version,
|
||||
HasACL: source.HasACL,
|
||||
IsFolder: source.IsFolder,
|
||||
FolderID: source.FolderID,
|
||||
FolderUID: source.FolderUID,
|
||||
FolderTitle: source.FolderTitle,
|
||||
FolderURL: source.FolderURL,
|
||||
Provisioned: source.Provisioned,
|
||||
ProvisionedExternalID: source.ProvisionedExternalID,
|
||||
func DashboardFromSchema(source schema.Dashboard) *Dashboard {
|
||||
return &Dashboard{
|
||||
IsStarred: source.Meta.IsStarred,
|
||||
Type: source.Meta.Type,
|
||||
CanSave: source.Meta.CanSave,
|
||||
CanEdit: source.Meta.CanEdit,
|
||||
CanAdmin: source.Meta.CanAdmin,
|
||||
CanStar: source.Meta.CanStar,
|
||||
CanDelete: source.Meta.CanDelete,
|
||||
Slug: source.Meta.Slug,
|
||||
URL: source.Meta.URL,
|
||||
Expires: source.Meta.Expires,
|
||||
Created: source.Meta.Created,
|
||||
Updated: source.Meta.Updated,
|
||||
UpdatedBy: source.Meta.UpdatedBy,
|
||||
CreatedBy: source.Meta.CreatedBy,
|
||||
Version: source.Meta.Version,
|
||||
HasACL: source.Meta.HasACL,
|
||||
IsFolder: source.Meta.IsFolder,
|
||||
FolderID: source.Meta.FolderID,
|
||||
FolderUID: source.Meta.FolderUID,
|
||||
FolderTitle: source.Meta.FolderTitle,
|
||||
FolderURL: source.Meta.FolderURL,
|
||||
Provisioned: source.Meta.Provisioned,
|
||||
ProvisionedExternalID: source.Meta.ProvisionedExternalID,
|
||||
AnnotationsPermissions: AnnotationsPermissions{
|
||||
Dashboard: AnnotationPermissions{
|
||||
CanAdd: source.AnnotationsPermissions.Dashboard.CanAdd,
|
||||
CanEdit: source.AnnotationsPermissions.Dashboard.CanEdit,
|
||||
CanDelete: source.AnnotationsPermissions.Dashboard.CanDelete,
|
||||
CanAdd: source.Meta.AnnotationsPermissions.Dashboard.CanAdd,
|
||||
CanEdit: source.Meta.AnnotationsPermissions.Dashboard.CanEdit,
|
||||
CanDelete: source.Meta.AnnotationsPermissions.Dashboard.CanDelete,
|
||||
},
|
||||
Organization: AnnotationPermissions{
|
||||
CanAdd: source.AnnotationsPermissions.Organization.CanAdd,
|
||||
CanEdit: source.AnnotationsPermissions.Organization.CanEdit,
|
||||
CanDelete: source.AnnotationsPermissions.Organization.CanDelete,
|
||||
CanAdd: source.Meta.AnnotationsPermissions.Organization.CanAdd,
|
||||
CanEdit: source.Meta.AnnotationsPermissions.Organization.CanEdit,
|
||||
CanDelete: source.Meta.AnnotationsPermissions.Organization.CanDelete,
|
||||
},
|
||||
},
|
||||
Dashboard: source.Dashboard,
|
||||
|
@ -96,3 +96,46 @@ func SearchResultFromSchema(source schema.SearchResult) *SearchResult {
|
|||
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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,41 +19,45 @@ type DashboardCreateResponse struct {
|
|||
Slug string `json:"slug"`
|
||||
}
|
||||
|
||||
type DashboardMeta struct {
|
||||
IsStarred bool `json:"isStarred"`
|
||||
Type string `json:"type"`
|
||||
CanSave bool `json:"canSave"`
|
||||
CanEdit bool `json:"canEdit"`
|
||||
CanAdmin bool `json:"canAdmin"`
|
||||
CanStar bool `json:"canStar"`
|
||||
CanDelete bool `json:"canDelete"`
|
||||
Slug string `json:"slug"`
|
||||
URL string `json:"url"`
|
||||
Expires time.Time `json:"expires"`
|
||||
Created time.Time `json:"created"`
|
||||
Updated time.Time `json:"updated"`
|
||||
UpdatedBy string `json:"updatedBy"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
Version uint `json:"version"`
|
||||
HasACL bool `json:"hasAcl"`
|
||||
IsFolder bool `json:"isFolder"`
|
||||
FolderID uint `json:"folderId"`
|
||||
FolderUID string `json:"folderUid"`
|
||||
FolderTitle string `json:"folderTitle"`
|
||||
FolderURL string `json:"folderUrl"`
|
||||
Provisioned bool `json:"provisioned"`
|
||||
ProvisionedExternalID string `json:"provisionedExternalId"`
|
||||
AnnotationsPermissions struct {
|
||||
Dashboard struct {
|
||||
CanAdd bool `json:"canAdd"`
|
||||
CanEdit bool `json:"canEdit"`
|
||||
CanDelete bool `json:"canDelete"`
|
||||
} `json:"dashboard"`
|
||||
Organization struct {
|
||||
CanAdd bool `json:"canAdd"`
|
||||
CanEdit bool `json:"canEdit"`
|
||||
CanDelete bool `json:"canDelete"`
|
||||
} `json:"organization"`
|
||||
} `json:"annotationsPermissions"`
|
||||
Dashboard any `json:"dashboard"`
|
||||
type Dashboard struct {
|
||||
Meta DashboardMeta `json:"meta"`
|
||||
Dashboard any `json:"dashboard"`
|
||||
}
|
||||
|
||||
type DashboardMeta struct {
|
||||
IsStarred bool `json:"isStarred"`
|
||||
Type string `json:"type"`
|
||||
CanSave bool `json:"canSave"`
|
||||
CanEdit bool `json:"canEdit"`
|
||||
CanAdmin bool `json:"canAdmin"`
|
||||
CanStar bool `json:"canStar"`
|
||||
CanDelete bool `json:"canDelete"`
|
||||
Slug string `json:"slug"`
|
||||
URL string `json:"url"`
|
||||
Expires time.Time `json:"expires"`
|
||||
Created time.Time `json:"created"`
|
||||
Updated time.Time `json:"updated"`
|
||||
UpdatedBy string `json:"updatedBy"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
Version uint `json:"version"`
|
||||
HasACL bool `json:"hasAcl"`
|
||||
IsFolder bool `json:"isFolder"`
|
||||
FolderID uint `json:"folderId"`
|
||||
FolderUID string `json:"folderUid"`
|
||||
FolderTitle string `json:"folderTitle"`
|
||||
FolderURL string `json:"folderUrl"`
|
||||
Provisioned bool `json:"provisioned"`
|
||||
ProvisionedExternalID string `json:"provisionedExternalId"`
|
||||
AnnotationsPermissions AnnotationsPermissions `json:"annotationsPermissions"`
|
||||
}
|
||||
|
||||
type AnnotationsPermissions struct {
|
||||
Dashboard AnnotationPermissions `json:"dashboard"`
|
||||
Organization AnnotationPermissions `json:"organization"`
|
||||
}
|
||||
|
||||
type AnnotationPermissions struct {
|
||||
CanAdd bool `json:"canAdd"`
|
||||
CanEdit bool `json:"canEdit"`
|
||||
CanDelete bool `json:"canDelete"`
|
||||
}
|
||||
|
|
|
@ -15,6 +15,4 @@ type DashboardVersion struct {
|
|||
Data any `json:"data"`
|
||||
}
|
||||
|
||||
type DashboardVersionListResponse struct {
|
||||
DashboardVersions []DashboardVersion
|
||||
}
|
||||
type DashboardVersionListResponse []DashboardVersion
|
||||
|
|
|
@ -17,6 +17,4 @@ type SearchResult struct {
|
|||
FolderURL string `json:"folderUrl"`
|
||||
}
|
||||
|
||||
type SearchResultListResponse struct {
|
||||
SearchResults []SearchResult
|
||||
}
|
||||
type SearchResultListResponse []SearchResult
|
||||
|
|
|
@ -84,20 +84,20 @@ func (c *SearchClient) Search(ctx context.Context, params ...SearchParam) ([]*Se
|
|||
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 {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var body schema.SearchResultListResponse
|
||||
|
||||
resp, err := c.client.Do(req, &body)
|
||||
resp, err := c.client.do(req, &body)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
searchResults := make([]*SearchResult, 0, len(body.SearchResults))
|
||||
for _, searchResult := range body.SearchResults {
|
||||
searchResults := make([]*SearchResult, 0, len(body))
|
||||
for _, searchResult := range body {
|
||||
searchResults = append(searchResults, SearchResultFromSchema(searchResult))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue