mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Refactor plugin dashboards (#44315)
Moves/refactor Grafana specific functionality related to plugin dashboards out to specific services for importing dashboards and keep app plugin dashboards up-to-date. Fixes #44257
This commit is contained in:
committed by
GitHub
parent
7e26dbb5ff
commit
94edd7a762
89
pkg/services/dashboardimport/api/api.go
Normal file
89
pkg/services/dashboardimport/api/api.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/apierrors"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
type ImportDashboardAPI struct {
|
||||
dashboardImportService dashboardimport.Service
|
||||
quotaService QuotaService
|
||||
schemaLoaderService SchemaLoaderService
|
||||
pluginStore plugins.Store
|
||||
}
|
||||
|
||||
func New(dashboardImportService dashboardimport.Service, quotaService QuotaService,
|
||||
schemaLoaderService SchemaLoaderService, pluginStore plugins.Store) *ImportDashboardAPI {
|
||||
return &ImportDashboardAPI{
|
||||
dashboardImportService: dashboardImportService,
|
||||
quotaService: quotaService,
|
||||
schemaLoaderService: schemaLoaderService,
|
||||
pluginStore: pluginStore,
|
||||
}
|
||||
}
|
||||
|
||||
func (api *ImportDashboardAPI) RegisterAPIEndpoints(routeRegister routing.RouteRegister) {
|
||||
routeRegister.Group("/api/dashboards", func(route routing.RouteRegister) {
|
||||
route.Post("/import", routing.Wrap(api.ImportDashboard))
|
||||
}, middleware.ReqSignedIn)
|
||||
}
|
||||
|
||||
func (api *ImportDashboardAPI) ImportDashboard(c *models.ReqContext) response.Response {
|
||||
req := dashboardimport.ImportDashboardRequest{}
|
||||
if err := web.Bind(c.Req, &req); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
|
||||
if req.PluginId == "" && req.Dashboard == nil {
|
||||
return response.Error(http.StatusUnprocessableEntity, "Dashboard must be set", nil)
|
||||
}
|
||||
|
||||
limitReached, err := api.quotaService.QuotaReached(c, "dashboard")
|
||||
if err != nil {
|
||||
return response.Error(500, "failed to get quota", err)
|
||||
}
|
||||
|
||||
if limitReached {
|
||||
return response.Error(403, "Quota reached", nil)
|
||||
}
|
||||
|
||||
trimDefaults := c.QueryBoolWithDefault("trimdefaults", true)
|
||||
if trimDefaults && !api.schemaLoaderService.IsDisabled() {
|
||||
req.Dashboard, err = api.schemaLoaderService.DashboardApplyDefaults(req.Dashboard)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Error while applying default value to the dashboard json", err)
|
||||
}
|
||||
}
|
||||
|
||||
req.User = c.SignedInUser
|
||||
resp, err := api.dashboardImportService.ImportDashboard(c.Req.Context(), &req)
|
||||
if err != nil {
|
||||
return apierrors.ToDashboardErrorResponse(c.Req.Context(), api.pluginStore, err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
type QuotaService interface {
|
||||
QuotaReached(c *models.ReqContext, target string) (bool, error)
|
||||
}
|
||||
|
||||
type quotaServiceFunc func(c *models.ReqContext, target string) (bool, error)
|
||||
|
||||
func (fn quotaServiceFunc) QuotaReached(c *models.ReqContext, target string) (bool, error) {
|
||||
return fn(c, target)
|
||||
}
|
||||
|
||||
type SchemaLoaderService interface {
|
||||
IsDisabled() bool
|
||||
DashboardApplyDefaults(input *simplejson.Json) (*simplejson.Json, error)
|
||||
}
|
||||
209
pkg/services/dashboardimport/api/api_test.go
Normal file
209
pkg/services/dashboardimport/api/api_test.go
Normal file
@@ -0,0 +1,209 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||
"github.com/grafana/grafana/pkg/web/webtest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestImportDashboardAPI(t *testing.T) {
|
||||
t.Run("Quota not reached, schema loader service disabled", func(t *testing.T) {
|
||||
importDashboardServiceCalled := false
|
||||
service := &serviceMock{
|
||||
importDashboardFunc: func(ctx context.Context, req *dashboardimport.ImportDashboardRequest) (*dashboardimport.ImportDashboardResponse, error) {
|
||||
importDashboardServiceCalled = true
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
|
||||
schemaLoaderServiceCalled := false
|
||||
schemaLoaderService := &schemaLoaderServiceMock{
|
||||
dashboardApplyDefaultsFunc: func(input *simplejson.Json) (*simplejson.Json, error) {
|
||||
schemaLoaderServiceCalled = true
|
||||
return input, nil
|
||||
},
|
||||
}
|
||||
|
||||
importDashboardAPI := New(service, quotaServiceFunc(quotaNotReached), schemaLoaderService, nil)
|
||||
routeRegister := routing.NewRouteRegister()
|
||||
importDashboardAPI.RegisterAPIEndpoints(routeRegister)
|
||||
s := webtest.NewServer(t, routeRegister)
|
||||
|
||||
t.Run("Not signed in should return 404", func(t *testing.T) {
|
||||
cmd := &dashboardimport.ImportDashboardRequest{}
|
||||
jsonBytes, err := json.Marshal(cmd)
|
||||
require.NoError(t, err)
|
||||
req := s.NewRequest(http.MethodPost, "/api/dashboards/import", bytes.NewReader(jsonBytes))
|
||||
resp, err := s.Send(req)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||
})
|
||||
|
||||
t.Run("Signed in, empty plugin id and dashboard model empty should return error", func(t *testing.T) {
|
||||
cmd := &dashboardimport.ImportDashboardRequest{
|
||||
PluginId: "",
|
||||
Dashboard: nil,
|
||||
}
|
||||
jsonBytes, err := json.Marshal(cmd)
|
||||
require.NoError(t, err)
|
||||
req := s.NewRequest(http.MethodPost, "/api/dashboards/import", bytes.NewReader(jsonBytes))
|
||||
webtest.RequestWithSignedInUser(req, &models.SignedInUser{
|
||||
UserId: 1,
|
||||
})
|
||||
resp, err := s.Send(req)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
|
||||
})
|
||||
|
||||
t.Run("Signed in, dashboard model set should call import dashboard service", func(t *testing.T) {
|
||||
cmd := &dashboardimport.ImportDashboardRequest{
|
||||
Dashboard: simplejson.New(),
|
||||
}
|
||||
jsonBytes, err := json.Marshal(cmd)
|
||||
require.NoError(t, err)
|
||||
req := s.NewRequest(http.MethodPost, "/api/dashboards/import", bytes.NewReader(jsonBytes))
|
||||
webtest.RequestWithSignedInUser(req, &models.SignedInUser{
|
||||
UserId: 1,
|
||||
})
|
||||
resp, err := s.Send(req)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
require.True(t, importDashboardServiceCalled)
|
||||
})
|
||||
|
||||
t.Run("Signed in, dashboard model set, trimdefaults enabled should not call schema loader service", func(t *testing.T) {
|
||||
cmd := &dashboardimport.ImportDashboardRequest{
|
||||
Dashboard: simplejson.New(),
|
||||
}
|
||||
jsonBytes, err := json.Marshal(cmd)
|
||||
require.NoError(t, err)
|
||||
req := s.NewRequest(http.MethodPost, "/api/dashboards/import?trimdefaults=true", bytes.NewReader(jsonBytes))
|
||||
webtest.RequestWithSignedInUser(req, &models.SignedInUser{
|
||||
UserId: 1,
|
||||
})
|
||||
resp, err := s.Send(req)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
require.False(t, schemaLoaderServiceCalled)
|
||||
require.True(t, importDashboardServiceCalled)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Quota not reached, schema loader service enabled", func(t *testing.T) {
|
||||
importDashboardServiceCalled := false
|
||||
service := &serviceMock{
|
||||
importDashboardFunc: func(ctx context.Context, req *dashboardimport.ImportDashboardRequest) (*dashboardimport.ImportDashboardResponse, error) {
|
||||
importDashboardServiceCalled = true
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
|
||||
schemaLoaderServiceCalled := false
|
||||
schemaLoaderService := &schemaLoaderServiceMock{
|
||||
enabled: true,
|
||||
dashboardApplyDefaultsFunc: func(input *simplejson.Json) (*simplejson.Json, error) {
|
||||
schemaLoaderServiceCalled = true
|
||||
return input, nil
|
||||
},
|
||||
}
|
||||
|
||||
importDashboardAPI := New(service, quotaServiceFunc(quotaNotReached), schemaLoaderService, nil)
|
||||
routeRegister := routing.NewRouteRegister()
|
||||
importDashboardAPI.RegisterAPIEndpoints(routeRegister)
|
||||
s := webtest.NewServer(t, routeRegister)
|
||||
|
||||
t.Run("Signed in, dashboard model set, trimdefaults enabled should call schema loader service", func(t *testing.T) {
|
||||
cmd := &dashboardimport.ImportDashboardRequest{
|
||||
Dashboard: simplejson.New(),
|
||||
}
|
||||
jsonBytes, err := json.Marshal(cmd)
|
||||
require.NoError(t, err)
|
||||
req := s.NewRequest(http.MethodPost, "/api/dashboards/import?trimdefaults=true", bytes.NewReader(jsonBytes))
|
||||
webtest.RequestWithSignedInUser(req, &models.SignedInUser{
|
||||
UserId: 1,
|
||||
})
|
||||
resp, err := s.Send(req)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
require.True(t, schemaLoaderServiceCalled)
|
||||
require.True(t, importDashboardServiceCalled)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Quota reached", func(t *testing.T) {
|
||||
service := &serviceMock{}
|
||||
schemaLoaderService := &schemaLoaderServiceMock{}
|
||||
importDashboardAPI := New(service, quotaServiceFunc(quotaReached), schemaLoaderService, nil)
|
||||
|
||||
routeRegister := routing.NewRouteRegister()
|
||||
importDashboardAPI.RegisterAPIEndpoints(routeRegister)
|
||||
s := webtest.NewServer(t, routeRegister)
|
||||
|
||||
t.Run("Signed in, dashboard model set, should return 403 forbidden/quota reached", func(t *testing.T) {
|
||||
cmd := &dashboardimport.ImportDashboardRequest{
|
||||
Dashboard: simplejson.New(),
|
||||
}
|
||||
jsonBytes, err := json.Marshal(cmd)
|
||||
require.NoError(t, err)
|
||||
req := s.NewRequest(http.MethodPost, "/api/dashboards/import", bytes.NewReader(jsonBytes))
|
||||
webtest.RequestWithSignedInUser(req, &models.SignedInUser{
|
||||
UserId: 1,
|
||||
})
|
||||
resp, err := s.Send(req)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, http.StatusForbidden, resp.StatusCode)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
type serviceMock struct {
|
||||
importDashboardFunc func(ctx context.Context, req *dashboardimport.ImportDashboardRequest) (*dashboardimport.ImportDashboardResponse, error)
|
||||
}
|
||||
|
||||
func (s *serviceMock) ImportDashboard(ctx context.Context, req *dashboardimport.ImportDashboardRequest) (*dashboardimport.ImportDashboardResponse, error) {
|
||||
if s.importDashboardFunc != nil {
|
||||
return s.importDashboardFunc(ctx, req)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type schemaLoaderServiceMock struct {
|
||||
enabled bool
|
||||
dashboardApplyDefaultsFunc func(input *simplejson.Json) (*simplejson.Json, error)
|
||||
}
|
||||
|
||||
func (s *schemaLoaderServiceMock) IsDisabled() bool {
|
||||
return !s.enabled
|
||||
}
|
||||
|
||||
func (s *schemaLoaderServiceMock) DashboardApplyDefaults(input *simplejson.Json) (*simplejson.Json, error) {
|
||||
if s.dashboardApplyDefaultsFunc != nil {
|
||||
return s.dashboardApplyDefaultsFunc(input)
|
||||
}
|
||||
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func quotaReached(c *models.ReqContext, target string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func quotaNotReached(c *models.ReqContext, target string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
52
pkg/services/dashboardimport/dashboardimport.go
Normal file
52
pkg/services/dashboardimport/dashboardimport.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package dashboardimport
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
// ImportDashboardInput definition of input parameters when importing a dashboard.
|
||||
type ImportDashboardInput struct {
|
||||
Type string `json:"type"`
|
||||
PluginId string `json:"pluginId"`
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// ImportDashboardRequest request object for importing a dashboard.
|
||||
type ImportDashboardRequest struct {
|
||||
PluginId string `json:"pluginId"`
|
||||
Path string `json:"path"`
|
||||
Overwrite bool `json:"overwrite"`
|
||||
Dashboard *simplejson.Json `json:"dashboard"`
|
||||
Inputs []ImportDashboardInput `json:"inputs"`
|
||||
FolderId int64 `json:"folderId"`
|
||||
FolderUid string `json:"folderUid"`
|
||||
|
||||
User *models.SignedInUser `json:"-"`
|
||||
}
|
||||
|
||||
// ImportDashboardResponse response object returned when importing a dashboard.
|
||||
type ImportDashboardResponse struct {
|
||||
UID string `json:"uid"`
|
||||
PluginId string `json:"pluginId"`
|
||||
Title string `json:"title"`
|
||||
Imported bool `json:"imported"`
|
||||
ImportedUri string `json:"importedUri"`
|
||||
ImportedUrl string `json:"importedUrl"`
|
||||
Slug string `json:"slug"`
|
||||
DashboardId int64 `json:"dashboardId"`
|
||||
FolderId int64 `json:"folderId"`
|
||||
ImportedRevision int64 `json:"importedRevision"`
|
||||
Revision int64 `json:"revision"`
|
||||
Description string `json:"description"`
|
||||
Path string `json:"path"`
|
||||
Removed bool `json:"removed"`
|
||||
}
|
||||
|
||||
// Service service interface for importing dashboards.
|
||||
type Service interface {
|
||||
ImportDashboard(ctx context.Context, req *ImportDashboardRequest) (*ImportDashboardResponse, error)
|
||||
}
|
||||
103
pkg/services/dashboardimport/service/service.go
Normal file
103
pkg/services/dashboardimport/service/service.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||
"github.com/grafana/grafana/pkg/services/dashboardimport/api"
|
||||
"github.com/grafana/grafana/pkg/services/dashboardimport/utils"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/librarypanels"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
"github.com/grafana/grafana/pkg/services/schemaloader"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
)
|
||||
|
||||
func ProvideService(sqlStore *sqlstore.SQLStore, routeRegister routing.RouteRegister,
|
||||
quotaService *quota.QuotaService, schemaLoaderService *schemaloader.SchemaLoaderService,
|
||||
pluginDashboardManager plugins.PluginDashboardManager, pluginStore plugins.Store,
|
||||
libraryPanelService librarypanels.Service) *ImportDashboardService {
|
||||
s := &ImportDashboardService{
|
||||
pluginDashboardManager: pluginDashboardManager,
|
||||
dashboardService: dashboards.NewService(sqlStore),
|
||||
libraryPanelService: libraryPanelService,
|
||||
}
|
||||
|
||||
dashboardImportAPI := api.New(s, quotaService, schemaLoaderService, pluginStore)
|
||||
dashboardImportAPI.RegisterAPIEndpoints(routeRegister)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
type ImportDashboardService struct {
|
||||
pluginDashboardManager plugins.PluginDashboardManager
|
||||
dashboardService dashboards.DashboardService
|
||||
libraryPanelService librarypanels.Service
|
||||
}
|
||||
|
||||
func (s *ImportDashboardService) ImportDashboard(ctx context.Context, req *dashboardimport.ImportDashboardRequest) (*dashboardimport.ImportDashboardResponse, error) {
|
||||
var dashboard *models.Dashboard
|
||||
if req.PluginId != "" {
|
||||
var err error
|
||||
if dashboard, err = s.pluginDashboardManager.LoadPluginDashboard(ctx, req.PluginId, req.Path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
dashboard = models.NewDashboardFromJson(req.Dashboard)
|
||||
}
|
||||
|
||||
evaluator := utils.NewDashTemplateEvaluator(dashboard.Data, req.Inputs)
|
||||
generatedDash, err := evaluator.Eval()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
saveCmd := models.SaveDashboardCommand{
|
||||
Dashboard: generatedDash,
|
||||
OrgId: req.User.OrgId,
|
||||
UserId: req.User.UserId,
|
||||
Overwrite: req.Overwrite,
|
||||
PluginId: req.PluginId,
|
||||
FolderId: req.FolderId,
|
||||
}
|
||||
|
||||
dto := &dashboards.SaveDashboardDTO{
|
||||
OrgId: saveCmd.OrgId,
|
||||
Dashboard: saveCmd.GetDashboardModel(),
|
||||
Overwrite: saveCmd.Overwrite,
|
||||
User: req.User,
|
||||
}
|
||||
|
||||
savedDash, err := s.dashboardService.ImportDashboard(ctx, dto)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = s.libraryPanelService.ImportLibraryPanelsForDashboard(ctx, req.User, savedDash, req.FolderId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = s.libraryPanelService.ConnectLibraryPanelsForDashboard(ctx, req.User, dashboard)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dashboardimport.ImportDashboardResponse{
|
||||
UID: savedDash.Uid,
|
||||
PluginId: req.PluginId,
|
||||
Title: savedDash.Title,
|
||||
Path: req.Path,
|
||||
Revision: savedDash.Data.Get("revision").MustInt64(1),
|
||||
FolderId: savedDash.FolderId,
|
||||
ImportedUri: "db/" + savedDash.Slug,
|
||||
ImportedUrl: savedDash.GetUrl(),
|
||||
ImportedRevision: dashboard.Data.Get("revision").MustInt64(1),
|
||||
Imported: true,
|
||||
DashboardId: savedDash.Id,
|
||||
Slug: savedDash.Slug,
|
||||
}, nil
|
||||
}
|
||||
201
pkg/services/dashboardimport/service/service_test.go
Normal file
201
pkg/services/dashboardimport/service/service_test.go
Normal file
@@ -0,0 +1,201 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/librarypanels"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestImportDashboardService(t *testing.T) {
|
||||
t.Run("When importing a plugin dashboard should save dashboard and sync library panels", func(t *testing.T) {
|
||||
pluginDashboardManager := &pluginDashboardManagerMock{
|
||||
loadPluginDashboardFunc: loadTestDashboard,
|
||||
}
|
||||
|
||||
var importDashboardArg *dashboards.SaveDashboardDTO
|
||||
dashboardService := &dashboardServiceMock{
|
||||
importDashboardFunc: func(ctx context.Context, dto *dashboards.SaveDashboardDTO) (*models.Dashboard, error) {
|
||||
importDashboardArg = dto
|
||||
return &models.Dashboard{
|
||||
Id: 4,
|
||||
Uid: dto.Dashboard.Uid,
|
||||
Slug: dto.Dashboard.Slug,
|
||||
OrgId: 3,
|
||||
Version: dto.Dashboard.Version,
|
||||
PluginId: "prometheus",
|
||||
FolderId: dto.Dashboard.FolderId,
|
||||
Title: dto.Dashboard.Title,
|
||||
Data: dto.Dashboard.Data,
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
||||
importLibraryPanelsForDashboard := false
|
||||
connectLibraryPanelsForDashboardCalled := false
|
||||
libraryPanelService := &libraryPanelServiceMock{
|
||||
importLibraryPanelsForDashboardFunc: func(ctx context.Context, signedInUser *models.SignedInUser, dash *models.Dashboard, folderID int64) error {
|
||||
importLibraryPanelsForDashboard = true
|
||||
return nil
|
||||
},
|
||||
connectLibraryPanelsForDashboardFunc: func(ctx context.Context, signedInUser *models.SignedInUser, dash *models.Dashboard) error {
|
||||
connectLibraryPanelsForDashboardCalled = true
|
||||
return nil
|
||||
},
|
||||
}
|
||||
s := &ImportDashboardService{
|
||||
pluginDashboardManager: pluginDashboardManager,
|
||||
dashboardService: dashboardService,
|
||||
libraryPanelService: libraryPanelService,
|
||||
}
|
||||
|
||||
req := &dashboardimport.ImportDashboardRequest{
|
||||
PluginId: "prometheus",
|
||||
Path: "dashboard.json",
|
||||
Inputs: []dashboardimport.ImportDashboardInput{
|
||||
{Name: "*", Type: "datasource", Value: "prom"},
|
||||
},
|
||||
User: &models.SignedInUser{UserId: 2, OrgRole: models.ROLE_ADMIN, OrgId: 3},
|
||||
FolderId: 5,
|
||||
}
|
||||
resp, err := s.ImportDashboard(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, resp)
|
||||
require.Equal(t, "UDdpyzz7z", resp.UID)
|
||||
|
||||
require.NotNil(t, importDashboardArg)
|
||||
require.Equal(t, int64(3), importDashboardArg.OrgId)
|
||||
require.Equal(t, int64(2), importDashboardArg.User.UserId)
|
||||
require.Equal(t, "prometheus", importDashboardArg.Dashboard.PluginId)
|
||||
require.Equal(t, int64(5), importDashboardArg.Dashboard.FolderId)
|
||||
|
||||
panel := importDashboardArg.Dashboard.Data.Get("panels").GetIndex(0)
|
||||
require.Equal(t, "prom", panel.Get("datasource").MustString())
|
||||
|
||||
require.True(t, importLibraryPanelsForDashboard)
|
||||
require.True(t, connectLibraryPanelsForDashboardCalled)
|
||||
})
|
||||
|
||||
t.Run("When importing a non-plugin dashboard should save dashboard and sync library panels", func(t *testing.T) {
|
||||
var importDashboardArg *dashboards.SaveDashboardDTO
|
||||
dashboardService := &dashboardServiceMock{
|
||||
importDashboardFunc: func(ctx context.Context, dto *dashboards.SaveDashboardDTO) (*models.Dashboard, error) {
|
||||
importDashboardArg = dto
|
||||
return &models.Dashboard{
|
||||
Id: 4,
|
||||
Uid: dto.Dashboard.Uid,
|
||||
Slug: dto.Dashboard.Slug,
|
||||
OrgId: 3,
|
||||
Version: dto.Dashboard.Version,
|
||||
PluginId: "prometheus",
|
||||
FolderId: dto.Dashboard.FolderId,
|
||||
Title: dto.Dashboard.Title,
|
||||
Data: dto.Dashboard.Data,
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
libraryPanelService := &libraryPanelServiceMock{}
|
||||
s := &ImportDashboardService{
|
||||
dashboardService: dashboardService,
|
||||
libraryPanelService: libraryPanelService,
|
||||
}
|
||||
|
||||
dash, err := loadTestDashboard(context.Background(), "", "dashboard.json")
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dashboardimport.ImportDashboardRequest{
|
||||
Dashboard: dash.Data,
|
||||
Path: "plugin_dashboard.json",
|
||||
Inputs: []dashboardimport.ImportDashboardInput{
|
||||
{Name: "*", Type: "datasource", Value: "prom"},
|
||||
},
|
||||
User: &models.SignedInUser{UserId: 2, OrgRole: models.ROLE_ADMIN, OrgId: 3},
|
||||
FolderId: 5,
|
||||
}
|
||||
resp, err := s.ImportDashboard(context.Background(), req)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, resp)
|
||||
require.Equal(t, "UDdpyzz7z", resp.UID)
|
||||
|
||||
require.NotNil(t, importDashboardArg)
|
||||
require.Equal(t, int64(3), importDashboardArg.OrgId)
|
||||
require.Equal(t, int64(2), importDashboardArg.User.UserId)
|
||||
require.Equal(t, "", importDashboardArg.Dashboard.PluginId)
|
||||
require.Equal(t, int64(5), importDashboardArg.Dashboard.FolderId)
|
||||
|
||||
panel := importDashboardArg.Dashboard.Data.Get("panels").GetIndex(0)
|
||||
require.Equal(t, "prom", panel.Get("datasource").MustString())
|
||||
})
|
||||
}
|
||||
|
||||
func loadTestDashboard(ctx context.Context, pluginID, path string) (*models.Dashboard, error) {
|
||||
// It's safe to ignore gosec warning G304 since this is a test and arguments comes from test configuration.
|
||||
// nolint:gosec
|
||||
bytes, err := ioutil.ReadFile(filepath.Join("testdata", path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dashboardJSON, err := simplejson.NewJson(bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return models.NewDashboardFromJson(dashboardJSON), nil
|
||||
}
|
||||
|
||||
type pluginDashboardManagerMock struct {
|
||||
plugins.PluginDashboardManager
|
||||
loadPluginDashboardFunc func(ctx context.Context, pluginID, path string) (*models.Dashboard, error)
|
||||
}
|
||||
|
||||
func (m *pluginDashboardManagerMock) LoadPluginDashboard(ctx context.Context, pluginID, path string) (*models.Dashboard, error) {
|
||||
if m.loadPluginDashboardFunc != nil {
|
||||
return m.loadPluginDashboardFunc(ctx, pluginID, path)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type dashboardServiceMock struct {
|
||||
dashboards.DashboardService
|
||||
importDashboardFunc func(ctx context.Context, dto *dashboards.SaveDashboardDTO) (*models.Dashboard, error)
|
||||
}
|
||||
|
||||
func (s *dashboardServiceMock) ImportDashboard(ctx context.Context, dto *dashboards.SaveDashboardDTO) (*models.Dashboard, error) {
|
||||
if s.importDashboardFunc != nil {
|
||||
return s.importDashboardFunc(ctx, dto)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type libraryPanelServiceMock struct {
|
||||
librarypanels.Service
|
||||
connectLibraryPanelsForDashboardFunc func(ctx context.Context, signedInUser *models.SignedInUser, dash *models.Dashboard) error
|
||||
importLibraryPanelsForDashboardFunc func(ctx context.Context, signedInUser *models.SignedInUser, dash *models.Dashboard, folderID int64) error
|
||||
}
|
||||
|
||||
func (s *libraryPanelServiceMock) ConnectLibraryPanelsForDashboard(ctx context.Context, signedInUser *models.SignedInUser, dash *models.Dashboard) error {
|
||||
if s.connectLibraryPanelsForDashboardFunc != nil {
|
||||
return s.connectLibraryPanelsForDashboardFunc(ctx, signedInUser, dash)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *libraryPanelServiceMock) ImportLibraryPanelsForDashboard(ctx context.Context, signedInUser *models.SignedInUser, dash *models.Dashboard, folderID int64) error {
|
||||
if s.importLibraryPanelsForDashboardFunc != nil {
|
||||
return s.importLibraryPanelsForDashboardFunc(ctx, signedInUser, dash, folderID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
220
pkg/services/dashboardimport/service/testdata/dashboard.json
vendored
Normal file
220
pkg/services/dashboardimport/service/testdata/dashboard.json
vendored
Normal file
@@ -0,0 +1,220 @@
|
||||
{
|
||||
"__inputs": [
|
||||
{
|
||||
"name": "DS_GDEV-PROMETHEUS",
|
||||
"label": "gdev-prometheus",
|
||||
"description": "",
|
||||
"type": "datasource",
|
||||
"pluginId": "prometheus",
|
||||
"pluginName": "Prometheus"
|
||||
}
|
||||
],
|
||||
"__requires": [
|
||||
{
|
||||
"type": "grafana",
|
||||
"id": "grafana",
|
||||
"name": "Grafana",
|
||||
"version": "8.1.0-pre"
|
||||
},
|
||||
{
|
||||
"type": "datasource",
|
||||
"id": "prometheus",
|
||||
"name": "Prometheus",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
{
|
||||
"type": "panel",
|
||||
"id": "stat",
|
||||
"name": "Stat",
|
||||
"version": ""
|
||||
},
|
||||
{
|
||||
"type": "panel",
|
||||
"id": "timeseries",
|
||||
"name": "Time series",
|
||||
"version": ""
|
||||
}
|
||||
],
|
||||
"annotations": {
|
||||
"list": [
|
||||
{
|
||||
"builtIn": 1,
|
||||
"datasource": "-- Grafana --",
|
||||
"enable": true,
|
||||
"hide": true,
|
||||
"iconColor": "rgba(0, 211, 255, 1)",
|
||||
"name": "Annotations & Alerts",
|
||||
"type": "dashboard"
|
||||
}
|
||||
]
|
||||
},
|
||||
"editable": true,
|
||||
"gnetId": null,
|
||||
"graphTooltip": 1,
|
||||
"id": null,
|
||||
"links": [
|
||||
{
|
||||
"icon": "info",
|
||||
"tags": [],
|
||||
"targetBlank": true,
|
||||
"title": "Grafana Docs",
|
||||
"tooltip": "",
|
||||
"type": "link",
|
||||
"url": "https://grafana.com/docs/grafana/latest/"
|
||||
},
|
||||
{
|
||||
"icon": "info",
|
||||
"tags": [],
|
||||
"targetBlank": true,
|
||||
"title": "Prometheus Docs",
|
||||
"type": "link",
|
||||
"url": "http://prometheus.io/docs/introduction/overview/"
|
||||
}
|
||||
],
|
||||
"panels": [
|
||||
{
|
||||
"datasource": "${DS_GDEV-PROMETHEUS}",
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"color": {
|
||||
"mode": "palette-classic"
|
||||
},
|
||||
"custom": {
|
||||
"axisLabel": "",
|
||||
"axisPlacement": "auto",
|
||||
"barAlignment": 0,
|
||||
"drawStyle": "line",
|
||||
"fillOpacity": 0,
|
||||
"gradientMode": "none",
|
||||
"hideFrom": {
|
||||
"legend": false,
|
||||
"tooltip": false,
|
||||
"viz": false
|
||||
},
|
||||
"lineInterpolation": "linear",
|
||||
"lineWidth": 1,
|
||||
"pointSize": 5,
|
||||
"scaleDistribution": {
|
||||
"type": "linear"
|
||||
},
|
||||
"showPoints": "never",
|
||||
"spanNulls": false,
|
||||
"stacking": {
|
||||
"group": "A",
|
||||
"mode": "none"
|
||||
},
|
||||
"thresholdsStyle": {
|
||||
"mode": "off"
|
||||
}
|
||||
},
|
||||
"links": [],
|
||||
"mappings": [],
|
||||
"min": 0,
|
||||
"thresholds": {
|
||||
"mode": "absolute",
|
||||
"steps": [
|
||||
{
|
||||
"color": "green",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"color": "red",
|
||||
"value": 80
|
||||
}
|
||||
]
|
||||
},
|
||||
"unit": "short"
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
"matcher": {
|
||||
"id": "byName",
|
||||
"options": "prometheus"
|
||||
},
|
||||
"properties": [
|
||||
{
|
||||
"id": "color",
|
||||
"value": {
|
||||
"fixedColor": "#C15C17",
|
||||
"mode": "fixed"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"matcher": {
|
||||
"id": "byName",
|
||||
"options": "{instance=\"localhost:9090\",job=\"prometheus\"}"
|
||||
},
|
||||
"properties": [
|
||||
{
|
||||
"id": "color",
|
||||
"value": {
|
||||
"fixedColor": "#CCA300",
|
||||
"mode": "fixed"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"gridPos": {
|
||||
"h": 6,
|
||||
"w": 6,
|
||||
"x": 0,
|
||||
"y": 0
|
||||
},
|
||||
"id": 3,
|
||||
"links": [],
|
||||
"options": {
|
||||
"legend": {
|
||||
"calcs": [],
|
||||
"displayMode": "list",
|
||||
"placement": "bottom"
|
||||
},
|
||||
"tooltip": {
|
||||
"mode": "single"
|
||||
}
|
||||
},
|
||||
"pluginVersion": "8.1.0-pre",
|
||||
"targets": [
|
||||
{
|
||||
"expr": "sum(irate(prometheus_tsdb_head_samples_appended_total{job=\"prometheus\"}[5m]))",
|
||||
"format": "time_series",
|
||||
"hide": false,
|
||||
"interval": "",
|
||||
"intervalFactor": 2,
|
||||
"legendFormat": "samples",
|
||||
"metric": "",
|
||||
"refId": "A",
|
||||
"step": 20
|
||||
}
|
||||
],
|
||||
"timeFrom": null,
|
||||
"timeShift": null,
|
||||
"title": "Samples Appended",
|
||||
"type": "timeseries"
|
||||
}
|
||||
],
|
||||
"refresh": "1m",
|
||||
"revision": "1.0",
|
||||
"schemaVersion": 30,
|
||||
"style": "dark",
|
||||
"tags": ["prometheus"],
|
||||
"templating": {
|
||||
"list": []
|
||||
},
|
||||
"time": {
|
||||
"from": "now-1h",
|
||||
"to": "now"
|
||||
},
|
||||
"timepicker": {
|
||||
"now": true,
|
||||
"refresh_intervals": ["5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d"],
|
||||
"time_options": ["5m", "15m", "1h", "6h", "12h", "24h", "2d", "7d", "30d"]
|
||||
},
|
||||
"timezone": "browser",
|
||||
"title": "Prometheus 2.0 Stats",
|
||||
"uid": "UDdpyzz7z",
|
||||
"version": 1
|
||||
}
|
||||
116
pkg/services/dashboardimport/utils/dash_template_evaluator.go
Normal file
116
pkg/services/dashboardimport/utils/dash_template_evaluator.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/expr"
|
||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||
)
|
||||
|
||||
var varRegex = regexp.MustCompile(`(\$\{.+?\})`)
|
||||
|
||||
type DashboardInputMissingError struct {
|
||||
VariableName string
|
||||
}
|
||||
|
||||
func (e DashboardInputMissingError) Error() string {
|
||||
return fmt.Sprintf("Dashboard input variable: %v missing from import command", e.VariableName)
|
||||
}
|
||||
|
||||
type DashTemplateEvaluator struct {
|
||||
template *simplejson.Json
|
||||
inputs []dashboardimport.ImportDashboardInput
|
||||
variables map[string]string
|
||||
result *simplejson.Json
|
||||
}
|
||||
|
||||
func NewDashTemplateEvaluator(template *simplejson.Json, inputs []dashboardimport.ImportDashboardInput) *DashTemplateEvaluator {
|
||||
return &DashTemplateEvaluator{
|
||||
template: template,
|
||||
inputs: inputs,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *DashTemplateEvaluator) findInput(varName string, varType string) *dashboardimport.ImportDashboardInput {
|
||||
for _, input := range e.inputs {
|
||||
if varType == input.Type && (input.Name == varName || input.Name == "*") {
|
||||
return &input
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *DashTemplateEvaluator) Eval() (*simplejson.Json, error) {
|
||||
e.result = simplejson.New()
|
||||
e.variables = make(map[string]string)
|
||||
|
||||
// check that we have all inputs we need
|
||||
for _, inputDef := range e.template.Get("__inputs").MustArray() {
|
||||
inputDefJson := simplejson.NewFromAny(inputDef)
|
||||
inputName := inputDefJson.Get("name").MustString()
|
||||
inputType := inputDefJson.Get("type").MustString()
|
||||
input := e.findInput(inputName, inputType)
|
||||
|
||||
// force expressions value to `__expr__`
|
||||
if inputDefJson.Get("pluginId").MustString() == expr.DatasourceType {
|
||||
input = &dashboardimport.ImportDashboardInput{
|
||||
Value: expr.DatasourceType,
|
||||
}
|
||||
}
|
||||
|
||||
if input == nil {
|
||||
return nil, &DashboardInputMissingError{VariableName: inputName}
|
||||
}
|
||||
|
||||
e.variables["${"+inputName+"}"] = input.Value
|
||||
}
|
||||
|
||||
return simplejson.NewFromAny(e.evalObject(e.template)), nil
|
||||
}
|
||||
|
||||
func (e *DashTemplateEvaluator) evalValue(source *simplejson.Json) interface{} {
|
||||
sourceValue := source.Interface()
|
||||
|
||||
switch v := sourceValue.(type) {
|
||||
case string:
|
||||
interpolated := varRegex.ReplaceAllStringFunc(v, func(match string) string {
|
||||
replacement, exists := e.variables[match]
|
||||
if exists {
|
||||
return replacement
|
||||
}
|
||||
return match
|
||||
})
|
||||
return interpolated
|
||||
case bool:
|
||||
return v
|
||||
case json.Number:
|
||||
return v
|
||||
case map[string]interface{}:
|
||||
return e.evalObject(source)
|
||||
case []interface{}:
|
||||
array := make([]interface{}, 0)
|
||||
for _, item := range v {
|
||||
array = append(array, e.evalValue(simplejson.NewFromAny(item)))
|
||||
}
|
||||
return array
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *DashTemplateEvaluator) evalObject(source *simplejson.Json) interface{} {
|
||||
result := make(map[string]interface{})
|
||||
|
||||
for key, value := range source.MustMap() {
|
||||
if key == "__inputs" {
|
||||
continue
|
||||
}
|
||||
result[key] = e.evalValue(simplejson.NewFromAny(value))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDashTemplateEvaluator(t *testing.T) {
|
||||
template, err := simplejson.NewJson([]byte(`{
|
||||
"__inputs": [
|
||||
{
|
||||
"name": "DS_NAME",
|
||||
"type": "datasource"
|
||||
}
|
||||
],
|
||||
"test": {
|
||||
"prop": "${DS_NAME}_${DS_NAME}"
|
||||
}
|
||||
}`))
|
||||
require.NoError(t, err)
|
||||
|
||||
evaluator := &DashTemplateEvaluator{
|
||||
template: template,
|
||||
inputs: []dashboardimport.ImportDashboardInput{
|
||||
{Name: "*", Type: "datasource", Value: "my-server"},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := evaluator.Eval()
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "my-server_my-server", res.GetPath("test", "prop").MustString())
|
||||
|
||||
inputs := res.Get("__inputs")
|
||||
require.Nil(t, inputs.Interface())
|
||||
}
|
||||
Reference in New Issue
Block a user