mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Remove bus from folder service (#46840)
* Remove bus from folder service * Fix tests
This commit is contained in:
parent
7839fadf00
commit
d57c94fb6a
@ -6,15 +6,14 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||||
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||||
"github.com/grafana/grafana/pkg/services/guardian"
|
"github.com/grafana/grafana/pkg/services/guardian"
|
||||||
"github.com/grafana/grafana/pkg/services/search"
|
"github.com/grafana/grafana/pkg/services/search"
|
||||||
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,12 +25,13 @@ type FolderServiceImpl struct {
|
|||||||
searchService *search.SearchService
|
searchService *search.SearchService
|
||||||
features featuremgmt.FeatureToggles
|
features featuremgmt.FeatureToggles
|
||||||
permissions accesscontrol.PermissionsService
|
permissions accesscontrol.PermissionsService
|
||||||
|
sqlStore sqlstore.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProvideFolderService(
|
func ProvideFolderService(
|
||||||
cfg *setting.Cfg, dashboardService dashboards.DashboardService, dashboardStore dashboards.Store,
|
cfg *setting.Cfg, dashboardService dashboards.DashboardService, dashboardStore dashboards.Store,
|
||||||
searchService *search.SearchService, features featuremgmt.FeatureToggles, permissionsServices accesscontrol.PermissionsServices,
|
searchService *search.SearchService, features featuremgmt.FeatureToggles, permissionsServices accesscontrol.PermissionsServices,
|
||||||
ac accesscontrol.AccessControl,
|
ac accesscontrol.AccessControl, sqlStore sqlstore.Store,
|
||||||
) *FolderServiceImpl {
|
) *FolderServiceImpl {
|
||||||
ac.RegisterAttributeScopeResolver(dashboards.NewNameScopeResolver(dashboardStore))
|
ac.RegisterAttributeScopeResolver(dashboards.NewNameScopeResolver(dashboardStore))
|
||||||
ac.RegisterAttributeScopeResolver(dashboards.NewUidScopeResolver(dashboardStore))
|
ac.RegisterAttributeScopeResolver(dashboards.NewUidScopeResolver(dashboardStore))
|
||||||
@ -44,6 +44,7 @@ func ProvideFolderService(
|
|||||||
searchService: searchService,
|
searchService: searchService,
|
||||||
features: features,
|
features: features,
|
||||||
permissions: permissionsServices.GetFolderService(),
|
permissions: permissionsServices.GetFolderService(),
|
||||||
|
sqlStore: sqlStore,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,11 +186,16 @@ func (f *FolderServiceImpl) CreateFolder(ctx context.Context, user *models.Signe
|
|||||||
|
|
||||||
func (f *FolderServiceImpl) UpdateFolder(ctx context.Context, user *models.SignedInUser, orgID int64, existingUid string, cmd *models.UpdateFolderCommand) error {
|
func (f *FolderServiceImpl) UpdateFolder(ctx context.Context, user *models.SignedInUser, orgID int64, existingUid string, cmd *models.UpdateFolderCommand) error {
|
||||||
query := models.GetDashboardQuery{OrgId: orgID, Uid: existingUid}
|
query := models.GetDashboardQuery{OrgId: orgID, Uid: existingUid}
|
||||||
dashFolder, err := getFolder(ctx, query)
|
if err := f.sqlStore.GetDashboard(ctx, &query); err != nil {
|
||||||
if err != nil {
|
|
||||||
return toFolderError(err)
|
return toFolderError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dashFolder := query.Result
|
||||||
|
|
||||||
|
if !dashFolder.IsFolder {
|
||||||
|
return models.ErrFolderNotFound
|
||||||
|
}
|
||||||
|
|
||||||
cmd.UpdateDashboardModel(dashFolder, orgID, user.UserId)
|
cmd.UpdateDashboardModel(dashFolder, orgID, user.UserId)
|
||||||
|
|
||||||
dto := &dashboards.SaveDashboardDTO{
|
dto := &dashboards.SaveDashboardDTO{
|
||||||
@ -245,18 +251,6 @@ func (f *FolderServiceImpl) MakeUserAdmin(ctx context.Context, orgID int64, user
|
|||||||
return f.dashboardService.MakeUserAdmin(ctx, orgID, userID, folderID, setViewAndEditPermissions)
|
return f.dashboardService.MakeUserAdmin(ctx, orgID, userID, folderID, setViewAndEditPermissions)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFolder(ctx context.Context, query models.GetDashboardQuery) (*models.Dashboard, error) {
|
|
||||||
if err := bus.Dispatch(ctx, &query); err != nil {
|
|
||||||
return nil, toFolderError(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !query.Result.IsFolder {
|
|
||||||
return nil, models.ErrFolderNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return query.Result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func toFolderError(err error) error {
|
func toFolderError(err error) error {
|
||||||
if errors.Is(err, models.ErrDashboardTitleEmpty) {
|
if errors.Is(err, models.ErrDashboardTitleEmpty) {
|
||||||
return models.ErrFolderTitleEmpty
|
return models.ErrFolderTitleEmpty
|
||||||
|
@ -12,13 +12,13 @@ import (
|
|||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||||
"github.com/grafana/grafana/pkg/services/guardian"
|
"github.com/grafana/grafana/pkg/services/guardian"
|
||||||
|
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
)
|
)
|
||||||
@ -37,7 +37,7 @@ func TestProvideFolderService(t *testing.T) {
|
|||||||
|
|
||||||
ProvideFolderService(
|
ProvideFolderService(
|
||||||
cfg, &dashboards.FakeDashboardService{DashboardService: dashboardService},
|
cfg, &dashboards.FakeDashboardService{DashboardService: dashboardService},
|
||||||
store, nil, features, permissionsServices, ac,
|
store, nil, features, permissionsServices, ac, mockstore.NewSQLStoreMock(),
|
||||||
)
|
)
|
||||||
|
|
||||||
require.Len(t, ac.Calls.RegisterAttributeScopeResolver, 2)
|
require.Len(t, ac.Calls.RegisterAttributeScopeResolver, 2)
|
||||||
@ -51,6 +51,7 @@ func TestFolderService(t *testing.T) {
|
|||||||
features := featuremgmt.WithFeatures()
|
features := featuremgmt.WithFeatures()
|
||||||
permissionsServices := acmock.NewPermissionsServicesMock()
|
permissionsServices := acmock.NewPermissionsServicesMock()
|
||||||
dashboardService := ProvideDashboardService(cfg, store, nil, features, permissionsServices)
|
dashboardService := ProvideDashboardService(cfg, store, nil, features, permissionsServices)
|
||||||
|
mockStore := mockstore.NewSQLStoreMock()
|
||||||
|
|
||||||
service := FolderServiceImpl{
|
service := FolderServiceImpl{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
@ -60,6 +61,7 @@ func TestFolderService(t *testing.T) {
|
|||||||
searchService: nil,
|
searchService: nil,
|
||||||
features: features,
|
features: features,
|
||||||
permissions: permissionsServices.GetFolderService(),
|
permissions: permissionsServices.GetFolderService(),
|
||||||
|
sqlStore: mockStore,
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("Given user has no permissions", func(t *testing.T) {
|
t.Run("Given user has no permissions", func(t *testing.T) {
|
||||||
@ -99,12 +101,7 @@ func TestFolderService(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("When updating folder should return access denied error", func(t *testing.T) {
|
t.Run("When updating folder should return access denied error", func(t *testing.T) {
|
||||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardQuery) error {
|
mockStore.ExpectedDashboard = models.NewDashboardFolder("Folder")
|
||||||
query.Result = models.NewDashboardFolder("Folder")
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
defer bus.ClearBusHandlers()
|
|
||||||
|
|
||||||
err := service.UpdateFolder(context.Background(), user, orgID, folderUID, &models.UpdateFolderCommand{
|
err := service.UpdateFolder(context.Background(), user, orgID, folderUID, &models.UpdateFolderCommand{
|
||||||
Uid: folderUID,
|
Uid: folderUID,
|
||||||
Title: "Folder-TEST",
|
Title: "Folder-TEST",
|
||||||
@ -132,12 +129,6 @@ func TestFolderService(t *testing.T) {
|
|||||||
dash.Id = rand.Int63()
|
dash.Id = rand.Int63()
|
||||||
f := models.DashboardToFolder(dash)
|
f := models.DashboardToFolder(dash)
|
||||||
|
|
||||||
bus.AddHandler("test", func(ctx context.Context, cmd *models.SaveDashboardCommand) error {
|
|
||||||
cmd.Result = dash
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
defer bus.ClearBusHandlers()
|
|
||||||
|
|
||||||
store.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil)
|
store.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil)
|
||||||
store.On("SaveDashboard", mock.Anything).Return(dash, nil).Once()
|
store.On("SaveDashboard", mock.Anything).Return(dash, nil).Once()
|
||||||
store.On("GetFolderByID", mock.Anything, orgID, dash.Id).Return(f, nil)
|
store.On("GetFolderByID", mock.Anything, orgID, dash.Id).Return(f, nil)
|
||||||
@ -153,17 +144,7 @@ func TestFolderService(t *testing.T) {
|
|||||||
dashboardFolder.Uid = util.GenerateShortUID()
|
dashboardFolder.Uid = util.GenerateShortUID()
|
||||||
f := models.DashboardToFolder(dashboardFolder)
|
f := models.DashboardToFolder(dashboardFolder)
|
||||||
|
|
||||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardQuery) error {
|
mockStore.ExpectedDashboard = dashboardFolder
|
||||||
query.Result = dashboardFolder
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
bus.AddHandler("test", func(ctx context.Context, cmd *models.SaveDashboardCommand) error {
|
|
||||||
cmd.Result = dashboardFolder
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
defer bus.ClearBusHandlers()
|
|
||||||
|
|
||||||
store.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil)
|
store.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil)
|
||||||
store.On("SaveDashboard", mock.Anything).Return(dashboardFolder, nil)
|
store.On("SaveDashboard", mock.Anything).Return(dashboardFolder, nil)
|
||||||
|
@ -228,7 +228,7 @@ func createFolderWithACL(t *testing.T, sqlStore *sqlstore.SQLStore, title string
|
|||||||
ac := acmock.New()
|
ac := acmock.New()
|
||||||
s := dashboardservice.ProvideFolderService(
|
s := dashboardservice.ProvideFolderService(
|
||||||
cfg, d, dashboardStore, nil,
|
cfg, d, dashboardStore, nil,
|
||||||
features, permissionsServices, ac,
|
features, permissionsServices, ac, nil,
|
||||||
)
|
)
|
||||||
t.Logf("Creating folder with title and UID %q", title)
|
t.Logf("Creating folder with title and UID %q", title)
|
||||||
folder, err := s.CreateFolder(context.Background(), &user, user.OrgId, title, title)
|
folder, err := s.CreateFolder(context.Background(), &user, user.OrgId, title, title)
|
||||||
@ -327,7 +327,7 @@ func testScenario(t *testing.T, desc string, fn func(t *testing.T, sc scenarioCo
|
|||||||
SQLStore: sqlStore,
|
SQLStore: sqlStore,
|
||||||
folderService: dashboardservice.ProvideFolderService(
|
folderService: dashboardservice.ProvideFolderService(
|
||||||
setting.NewCfg(), dashboardService, dashboardStore, nil,
|
setting.NewCfg(), dashboardService, dashboardStore, nil,
|
||||||
featuremgmt.WithFeatures(), acmock.NewPermissionsServicesMock(), ac,
|
featuremgmt.WithFeatures(), acmock.NewPermissionsServicesMock(), ac, nil,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1443,7 +1443,7 @@ func createFolderWithACL(t *testing.T, sqlStore *sqlstore.SQLStore, title string
|
|||||||
dashboardStore := database.ProvideDashboardStore(sqlStore)
|
dashboardStore := database.ProvideDashboardStore(sqlStore)
|
||||||
d := dashboardservice.ProvideDashboardService(cfg, dashboardStore, nil, features, permissionsServices)
|
d := dashboardservice.ProvideDashboardService(cfg, dashboardStore, nil, features, permissionsServices)
|
||||||
ac := acmock.New()
|
ac := acmock.New()
|
||||||
s := dashboardservice.ProvideFolderService(cfg, d, dashboardStore, nil, features, permissionsServices, ac)
|
s := dashboardservice.ProvideFolderService(cfg, d, dashboardStore, nil, features, permissionsServices, ac, nil)
|
||||||
|
|
||||||
t.Logf("Creating folder with title and UID %q", title)
|
t.Logf("Creating folder with title and UID %q", title)
|
||||||
folder, err := s.CreateFolder(context.Background(), user, user.OrgId, title, title)
|
folder, err := s.CreateFolder(context.Background(), user, user.OrgId, title, title)
|
||||||
@ -1546,7 +1546,7 @@ func testScenario(t *testing.T, desc string, fn func(t *testing.T, sc scenarioCo
|
|||||||
|
|
||||||
folderService := dashboardservice.ProvideFolderService(
|
folderService := dashboardservice.ProvideFolderService(
|
||||||
cfg, dashboardService, dashboardStore, nil,
|
cfg, dashboardService, dashboardStore, nil,
|
||||||
features, permissionsServices, ac,
|
features, permissionsServices, ac, nil,
|
||||||
)
|
)
|
||||||
|
|
||||||
elementService := libraryelements.ProvideService(cfg, sqlStore, routing.NewRouteRegister(), folderService)
|
elementService := libraryelements.ProvideService(cfg, sqlStore, routing.NewRouteRegister(), folderService)
|
||||||
|
@ -54,7 +54,7 @@ func SetupTestEnv(t *testing.T, baseInterval time.Duration) (*ngalert.AlertNG, *
|
|||||||
)
|
)
|
||||||
folderService := dashboardservice.ProvideFolderService(
|
folderService := dashboardservice.ProvideFolderService(
|
||||||
cfg, dashboardService, dashboardStore, nil,
|
cfg, dashboardService, dashboardStore, nil,
|
||||||
features, permissionsServices, ac,
|
features, permissionsServices, ac, nil,
|
||||||
)
|
)
|
||||||
|
|
||||||
ng, err := ngalert.ProvideService(
|
ng, err := ngalert.ProvideService(
|
||||||
|
Loading…
Reference in New Issue
Block a user