Chore: Remove bus.Dispatch from provisioning services (#44989)

* make getordbyname a method

* remove one dispatch from plugins provisioner

* remove bus from the plugins provisioner, skip test for now

* remove bus from datasource provisioning

* resolve tests in notifier provisioning

* remove bus from the dashboards provisioning service

* fix missing struct field

* fix getorgbyid method calls

* pass org store into dashboard provisioner

* fix test function prototype

* fix tests

* attempt to fix tests after the rebase

* fix integration test

* avoid using transaction

* remove comments
This commit is contained in:
Serge Zaitsev
2022-02-23 11:12:37 +01:00
committed by GitHub
parent 49ac8b9f0a
commit a231c6861c
34 changed files with 445 additions and 377 deletions
@@ -1,43 +1 @@
package sqlstore
import (
"context"
"errors"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
)
func (ss *SQLStore) addDashboardProvisioningQueryAndCommandHandlers() {
bus.AddHandler("sql", ss.DeleteOrphanedProvisionedDashboards)
}
type DashboardExtras struct {
Id int64
DashboardId int64
Key string
Value string
}
func (ss *SQLStore) DeleteOrphanedProvisionedDashboards(ctx context.Context, cmd *models.DeleteOrphanedProvisionedDashboardsCommand) error {
var result []*models.DashboardProvisioning
convertedReaderNames := make([]interface{}, len(cmd.ReaderNames))
for index, readerName := range cmd.ReaderNames {
convertedReaderNames[index] = readerName
}
err := x.NotIn("name", convertedReaderNames...).Find(&result)
if err != nil {
return err
}
for _, deleteDashCommand := range result {
err := ss.DeleteDashboard(ctx, &models.DeleteDashboardCommand{Id: deleteDashCommand.DashboardId})
if err != nil && !errors.Is(err, models.ErrDashboardNotFound) {
return err
}
}
return nil
}
+13 -2
View File
@@ -13,14 +13,16 @@ type OrgListResponse []struct {
Response error
}
type SQLStoreMock struct {
LastGetAlertsQuery *models.GetAlertsQuery
LatestUserId int64
LastGetAlertsQuery *models.GetAlertsQuery
LatestUserId int64
ExpectedUser *models.User
ExpectedDatasource *models.DataSource
ExpectedAlert *models.Alert
ExpectedPluginSetting *models.PluginSetting
ExpectedDashboard *models.Dashboard
ExpectedDashboards []*models.Dashboard
ExpectedDashboardVersion *models.DashboardVersion
ExpectedDashboardVersions []*models.DashboardVersion
ExpectedDashboardAclInfoList []*models.DashboardAclInfoDTO
ExpectedUserOrgList []*models.UserOrgDTO
@@ -92,10 +94,19 @@ func (m *SQLStoreMock) SearchDashboardSnapshots(query *models.GetDashboardSnapsh
return m.ExpectedError
}
func (m *SQLStoreMock) GetOrgById(ctx context.Context, cmd *models.GetOrgByIdQuery) error {
return m.ExpectedError
}
func (m *SQLStoreMock) GetOrgByName(name string) (*models.Org, error) {
return m.ExpectedOrg, m.ExpectedError
}
func (m *SQLStoreMock) GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error {
query.Result = m.ExpectedOrg
return m.ExpectedError
}
func (m *SQLStoreMock) CreateOrgWithMember(name string, userID int64) (models.Org, error) {
return *m.ExpectedOrg, nil
}
+4 -4
View File
@@ -16,11 +16,11 @@ import (
const MainOrgName = "Main Org."
func (ss *SQLStore) addOrgQueryAndCommandHandlers() {
bus.AddHandler("sql", GetOrgById)
bus.AddHandler("sql", ss.GetOrgById)
bus.AddHandler("sql", CreateOrg)
bus.AddHandler("sql", ss.UpdateOrg)
bus.AddHandler("sql", ss.UpdateOrgAddress)
bus.AddHandler("sql", GetOrgByName)
bus.AddHandler("sql", ss.GetOrgByNameHandler)
bus.AddHandler("sql", ss.SearchOrgs)
bus.AddHandler("sql", ss.DeleteOrg)
}
@@ -48,7 +48,7 @@ func (ss *SQLStore) SearchOrgs(ctx context.Context, query *models.SearchOrgsQuer
return err
}
func GetOrgById(ctx context.Context, query *models.GetOrgByIdQuery) error {
func (ss *SQLStore) GetOrgById(ctx context.Context, query *models.GetOrgByIdQuery) error {
var org models.Org
exists, err := x.Id(query.Id).Get(&org)
if err != nil {
@@ -63,7 +63,7 @@ func GetOrgById(ctx context.Context, query *models.GetOrgByIdQuery) error {
return nil
}
func GetOrgByName(ctx context.Context, query *models.GetOrgByNameQuery) error {
func (ss *SQLStore) GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error {
var org models.Org
exists, err := x.Where("name=?", query.Name).Get(&org)
if err != nil {
-1
View File
@@ -130,7 +130,6 @@ func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, b bus.
ss.addPlaylistQueryAndCommandHandlers()
ss.addLoginAttemptQueryAndCommandHandlers()
ss.addTeamQueryAndCommandHandlers()
ss.addDashboardProvisioningQueryAndCommandHandlers()
ss.addOrgQueryAndCommandHandlers()
bus.AddHandler("sql", ss.GetDBHealthQuery)
+2 -2
View File
@@ -79,7 +79,7 @@ func populateDB(t *testing.T, sqlStore *SQLStore) {
// get 1st user's organisation
getOrgByIdQuery := &models.GetOrgByIdQuery{Id: users[0].OrgId}
err := GetOrgById(context.Background(), getOrgByIdQuery)
err := sqlStore.GetOrgById(context.Background(), getOrgByIdQuery)
require.NoError(t, err)
org := getOrgByIdQuery.Result
@@ -103,7 +103,7 @@ func populateDB(t *testing.T, sqlStore *SQLStore) {
// get 2nd user's organisation
getOrgByIdQuery = &models.GetOrgByIdQuery{Id: users[1].OrgId}
err = GetOrgById(context.Background(), getOrgByIdQuery)
err = sqlStore.GetOrgById(context.Background(), getOrgByIdQuery)
require.NoError(t, err)
org = getOrgByIdQuery.Result
+2 -1
View File
@@ -24,7 +24,8 @@ type Store interface {
UpdateOrg(ctx context.Context, cmd *models.UpdateOrgCommand) error
UpdateOrgAddress(ctx context.Context, cmd *models.UpdateOrgAddressCommand) error
DeleteOrg(ctx context.Context, cmd *models.DeleteOrgCommand) error
DeleteOrphanedProvisionedDashboards(ctx context.Context, cmd *models.DeleteOrphanedProvisionedDashboardsCommand) error
GetOrgById(context.Context, *models.GetOrgByIdQuery) error
GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error
CreateLoginAttempt(ctx context.Context, cmd *models.CreateLoginAttemptCommand) error
DeleteOldLoginAttempts(ctx context.Context, cmd *models.DeleteOldLoginAttemptsCommand) error
CloneUserToServiceAccount(ctx context.Context, siUser *models.SignedInUser) (*models.User, error)