DashboardService: Consolidate constructors (#31886)

* DashboardService: Consolidate constructors

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
pull/31942/head
Arve Knudsen 4 years ago committed by GitHub
parent f917c2ab0f
commit 1716de88b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      pkg/api/dashboard.go
  2. 45
      pkg/api/dashboard_test.go
  3. 2
      pkg/plugins/manager/dashboard_import.go
  4. 29
      pkg/services/dashboards/dashboard_service.go
  5. 2
      pkg/services/librarypanels/librarypanels_test.go
  6. 15
      pkg/services/provisioning/dashboards/file_reader_linux_test.go
  7. 12
      pkg/services/provisioning/dashboards/file_reader_test.go
  8. 8
      pkg/services/sqlstore/dashboard_service_integration_test.go

@ -122,7 +122,8 @@ func (hs *HTTPServer) GetDashboard(c *models.ReqContext) response.Response {
meta.FolderUrl = query.Result.GetUrl()
}
provisioningData, err := dashboards.NewProvisioningService().GetProvisionedDashboardDataByDashboardID(dash.Id)
svc := dashboards.NewProvisioningService()
provisioningData, err := svc.GetProvisionedDashboardDataByDashboardID(dash.Id)
if err != nil {
return response.Error(500, "Error while checking if dashboard is provisioned", err)
}
@ -226,7 +227,8 @@ func (hs *HTTPServer) deleteDashboard(c *models.ReqContext) response.Response {
}
}
err := dashboards.NewService(hs.DataService).DeleteDashboard(dash.Id, c.OrgId)
svc := dashboards.NewService()
err := svc.DeleteDashboard(dash.Id, c.OrgId)
if err != nil {
var dashboardErr models.DashboardErr
if ok := errors.As(err, &dashboardErr); ok {
@ -262,7 +264,8 @@ func (hs *HTTPServer) PostDashboard(c *models.ReqContext, cmd models.SaveDashboa
}
}
provisioningData, err := dashboards.NewProvisioningService().GetProvisionedDashboardDataByDashboardID(dash.Id)
svc := dashboards.NewProvisioningService()
provisioningData, err := svc.GetProvisionedDashboardDataByDashboardID(dash.Id)
if err != nil {
return response.Error(500, "Error while checking if dashboard is provisioned", err)
}
@ -288,7 +291,8 @@ func (hs *HTTPServer) PostDashboard(c *models.ReqContext, cmd models.SaveDashboa
Overwrite: cmd.Overwrite,
}
dashboard, err := dashboards.NewService(hs.DataService).SaveDashboard(dashItem, allowUiUpdate)
dashSvc := dashboards.NewService()
dashboard, err := dashSvc.SaveDashboard(dashItem, allowUiUpdate)
if err != nil {
return dashboardSaveErrorToApiResponse(err)
}

@ -1173,10 +1173,6 @@ func callPostDashboardShouldReturnSuccess(sc *scenarioContext) {
assert.Equal(sc.t, 200, sc.resp.Code)
}
func (m mockDashboardProvisioningService) DeleteProvisionedDashboard(dashboardId int64, orgId int64) error {
panic("implement me")
}
func postDashboardScenario(t *testing.T, desc string, url string, routePattern string,
mock *dashboards.FakeDashboardService, cmd models.SaveDashboardCommand, fn scenarioFunc) {
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
@ -1202,20 +1198,18 @@ func postDashboardScenario(t *testing.T, desc string, url string, routePattern s
})
origNewDashboardService := dashboards.NewService
dashboards.MockDashboardService(mock)
origProvisioningService := dashboards.NewProvisioningService
t.Cleanup(func() {
dashboards.NewService = origNewDashboardService
dashboards.NewProvisioningService = origProvisioningService
})
dashboards.MockDashboardService(mock)
dashboards.NewProvisioningService = func() dashboards.DashboardProvisioningService {
return mockDashboardProvisioningService{}
}
sc.m.Post(routePattern, sc.defaultHandler)
defer func() {
dashboards.NewService = origNewDashboardService
dashboards.NewProvisioningService = origProvisioningService
}()
fn(sc)
})
}
@ -1269,20 +1263,18 @@ func restoreDashboardVersionScenario(t *testing.T, desc string, url string, rout
})
origProvisioningService := dashboards.NewProvisioningService
origNewDashboardService := dashboards.NewService
t.Cleanup(func() {
dashboards.NewService = origNewDashboardService
dashboards.NewProvisioningService = origProvisioningService
})
dashboards.NewProvisioningService = func() dashboards.DashboardProvisioningService {
return mockDashboardProvisioningService{}
}
origNewDashboardService := dashboards.NewService
dashboards.MockDashboardService(mock)
sc.m.Post(routePattern, sc.defaultHandler)
defer func() {
dashboards.NewService = origNewDashboardService
dashboards.NewProvisioningService = origProvisioningService
}()
fn(sc)
})
}
@ -1295,24 +1287,9 @@ func (sc *scenarioContext) ToJSON() *simplejson.Json {
}
type mockDashboardProvisioningService struct {
}
func (m mockDashboardProvisioningService) SaveProvisionedDashboard(dto *dashboards.SaveDashboardDTO, provisioning *models.DashboardProvisioning) (*models.Dashboard, error) {
panic("implement me")
}
func (m mockDashboardProvisioningService) SaveFolderForProvisionedDashboards(*dashboards.SaveDashboardDTO) (*models.Dashboard, error) {
panic("implement me")
}
func (m mockDashboardProvisioningService) GetProvisionedDashboardData(name string) ([]*models.DashboardProvisioning, error) {
panic("implement me")
dashboards.DashboardProvisioningService
}
func (m mockDashboardProvisioningService) GetProvisionedDashboardDataByDashboardID(dashboardId int64) (*models.DashboardProvisioning, error) {
return &models.DashboardProvisioning{}, nil
}
func (m mockDashboardProvisioningService) UnprovisionDashboard(dashboardId int64) error {
panic("implement me")
}

@ -68,7 +68,7 @@ func (pm *PluginManager) ImportDashboard(pluginID, path string, orgID, folderID
User: user,
}
savedDash, err := dashboards.NewService(requestHandler).ImportDashboard(dto)
savedDash, err := dashboards.NewService().ImportDashboard(dto)
if err != nil {
return plugins.PluginDashboardInfoDTO{}, err
}

@ -6,7 +6,6 @@ import (
"github.com/grafana/grafana/pkg/components/gtime"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/tsdbifaces"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
@ -16,14 +15,14 @@ import (
"github.com/grafana/grafana/pkg/util/errutil"
)
// DashboardService service for operating on dashboards
// DashboardService is a service for operating on dashboards.
type DashboardService interface {
SaveDashboard(dto *SaveDashboardDTO, allowUiUpdate bool) (*models.Dashboard, error)
ImportDashboard(dto *SaveDashboardDTO) (*models.Dashboard, error)
DeleteDashboard(dashboardId int64, orgId int64) error
}
// DashboardProvisioningService service for operating on provisioned dashboards
// DashboardProvisioningService is a service for operating on provisioned dashboards.
type DashboardProvisioningService interface {
SaveProvisionedDashboard(dto *SaveDashboardDTO, provisioning *models.DashboardProvisioning) (*models.Dashboard, error)
SaveFolderForProvisionedDashboards(*SaveDashboardDTO) (*models.Dashboard, error)
@ -33,19 +32,16 @@ type DashboardProvisioningService interface {
DeleteProvisionedDashboard(dashboardId int64, orgId int64) error
}
// NewService factory for creating a new dashboard service
var NewService = func(reqHandler tsdbifaces.RequestHandler) DashboardService {
// NewService is a factory for creating a new dashboard service.
var NewService = func() DashboardService {
return &dashboardServiceImpl{
log: log.New("dashboard-service"),
reqHandler: reqHandler,
log: log.New("dashboard-service"),
}
}
// NewProvisioningService factory for creating a new dashboard provisioning service
// NewProvisioningService is a factory for creating a new dashboard provisioning service.
var NewProvisioningService = func() DashboardProvisioningService {
return &dashboardServiceImpl{
log: log.New("dashboard-provisioning-service"),
}
return NewService().(*dashboardServiceImpl)
}
type SaveDashboardDTO struct {
@ -58,10 +54,9 @@ type SaveDashboardDTO struct {
}
type dashboardServiceImpl struct {
orgId int64
user *models.SignedInUser
log log.Logger
reqHandler tsdbifaces.RequestHandler
orgId int64
user *models.SignedInUser
log log.Logger
}
func (dr *dashboardServiceImpl) GetProvisionedDashboardData(name string) ([]*models.DashboardProvisioning, error) {
@ -359,6 +354,8 @@ func (dr *dashboardServiceImpl) UnprovisionDashboard(dashboardId int64) error {
}
type FakeDashboardService struct {
DashboardService
SaveDashboardResult *models.Dashboard
SaveDashboardError error
SavedDashboards []*SaveDashboardDTO
@ -389,7 +386,7 @@ func (s *FakeDashboardService) DeleteDashboard(dashboardId int64, orgId int64) e
}
func MockDashboardService(mock *FakeDashboardService) {
NewService = func(tsdbifaces.RequestHandler) DashboardService {
NewService = func() DashboardService {
return mock
}
}

@ -763,7 +763,7 @@ func createDashboard(t *testing.T, user models.SignedInUser, title string, folde
return nil
})
dashboard, err := dashboards.NewService(nil).SaveDashboard(dashItem, true)
dashboard, err := dashboards.NewService().SaveDashboard(dashItem, true)
require.NoError(t, err)
return dashboard

@ -7,6 +7,8 @@ import (
"testing"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
@ -23,18 +25,11 @@ func TestProvisionedSymlinkedFolder(t *testing.T) {
}
reader, err := NewDashboardFileReader(cfg, log.New("test-logger"))
if err != nil {
t.Error("expected err to be nil")
}
require.NoError(t, err)
want, err := filepath.Abs(containingID)
if err != nil {
t.Errorf("expected err to be nil")
}
require.NoError(t, err)
resolvedPath := reader.resolvedPath()
if resolvedPath != want {
t.Errorf("got %s want %s", resolvedPath, want)
}
assert.Equal(t, want, resolvedPath)
}

@ -90,6 +90,9 @@ func TestDashboardFileReader(t *testing.T) {
Convey("Dashboard file reader", t, func() {
bus.ClearBusHandlers()
origNewDashboardProvisioningService := dashboards.NewProvisioningService
Reset(func() {
dashboards.NewProvisioningService = origNewDashboardProvisioningService
})
fakeService = mockDashboardProvisioningService()
bus.AddHandler("test", mockGetDashboardQuery)
@ -356,10 +359,6 @@ func TestDashboardFileReader(t *testing.T) {
So(fakeService.inserted[0].Dashboard.Id, ShouldEqual, 1)
})
})
Reset(func() {
dashboards.NewProvisioningService = origNewDashboardProvisioningService
})
})
}
@ -403,6 +402,8 @@ func mockDashboardProvisioningService() *fakeDashboardProvisioningService {
}
type fakeDashboardProvisioningService struct {
dashboards.DashboardProvisioningService
inserted []*dashboards.SaveDashboardDTO
provisioned map[string][]*models.DashboardProvisioning
getDashboard []*models.Dashboard
@ -416,7 +417,8 @@ func (s *fakeDashboardProvisioningService) GetProvisionedDashboardData(name stri
return s.provisioned[name], nil
}
func (s *fakeDashboardProvisioningService) SaveProvisionedDashboard(dto *dashboards.SaveDashboardDTO, provisioning *models.DashboardProvisioning) (*models.Dashboard, error) {
func (s *fakeDashboardProvisioningService) SaveProvisionedDashboard(dto *dashboards.SaveDashboardDTO,
provisioning *models.DashboardProvisioning) (*models.Dashboard, error) {
// Copy the structs as we need to change them but do not want to alter outside world.
var copyProvisioning = &models.DashboardProvisioning{}
*copyProvisioning = *provisioning

@ -967,7 +967,7 @@ func callSaveWithResult(t *testing.T, cmd models.SaveDashboardCommand) *models.D
t.Helper()
dto := toSaveDashboardDto(cmd)
res, err := dashboards.NewService(nil).SaveDashboard(&dto, false)
res, err := dashboards.NewService().SaveDashboard(&dto, false)
require.NoError(t, err)
return res
@ -975,7 +975,7 @@ func callSaveWithResult(t *testing.T, cmd models.SaveDashboardCommand) *models.D
func callSaveWithError(cmd models.SaveDashboardCommand) error {
dto := toSaveDashboardDto(cmd)
_, err := dashboards.NewService(nil).SaveDashboard(&dto, false)
_, err := dashboards.NewService().SaveDashboard(&dto, false)
return err
}
@ -1001,7 +1001,7 @@ func saveTestDashboard(t *testing.T, title string, orgId int64, folderId int64)
},
}
res, err := dashboards.NewService(nil).SaveDashboard(&dto, false)
res, err := dashboards.NewService().SaveDashboard(&dto, false)
require.NoError(t, err)
return res
@ -1028,7 +1028,7 @@ func saveTestFolder(t *testing.T, title string, orgId int64) *models.Dashboard {
},
}
res, err := dashboards.NewService(nil).SaveDashboard(&dto, false)
res, err := dashboards.NewService().SaveDashboard(&dto, false)
require.NoError(t, err)
return res

Loading…
Cancel
Save