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

View File

@@ -0,0 +1,57 @@
// Code generated by mockery v2.10.0. DO NOT EDIT.
package mocks
import (
context "context"
models "github.com/grafana/grafana/pkg/models"
mock "github.com/stretchr/testify/mock"
)
// Store is an autogenerated mock type for the Store type
type Store struct {
mock.Mock
}
// GetOrgByNameHandler provides a mock function with given fields: ctx, query
func (_m *Store) GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error {
ret := _m.Called(ctx, query)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *models.GetOrgByNameQuery) error); ok {
r0 = rf(ctx, query)
} else {
r0 = ret.Error(0)
}
return r0
}
// GetPluginSettingById provides a mock function with given fields: ctx, query
func (_m *Store) GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
ret := _m.Called(ctx, query)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *models.GetPluginSettingByIdQuery) error); ok {
r0 = rf(ctx, query)
} else {
r0 = ret.Error(0)
}
return r0
}
// UpdatePluginSetting provides a mock function with given fields: ctx, cmd
func (_m *Store) UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
ret := _m.Called(ctx, cmd)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *models.UpdatePluginSettingCmd) error); ok {
r0 = rf(ctx, cmd)
} else {
r0 = ret.Error(0)
}
return r0
}

View File

@@ -4,19 +4,25 @@ import (
"context"
"errors"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
)
type Store interface {
GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error
GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error
UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error
}
// Provision scans a directory for provisioning config files
// and provisions the app in those files.
func Provision(ctx context.Context, configDirectory string, pluginStore plugins.Store) error {
func Provision(ctx context.Context, configDirectory string, store Store, pluginStore plugins.Store) error {
logger := log.New("provisioning.plugins")
ap := PluginProvisioner{
log: logger,
cfgProvider: newConfigReader(logger, pluginStore),
store: store,
}
return ap.applyChanges(ctx, configDirectory)
}
@@ -26,13 +32,14 @@ func Provision(ctx context.Context, configDirectory string, pluginStore plugins.
type PluginProvisioner struct {
log log.Logger
cfgProvider configReader
store Store
}
func (ap *PluginProvisioner) apply(ctx context.Context, cfg *pluginsAsConfig) error {
for _, app := range cfg.Apps {
if app.OrgID == 0 && app.OrgName != "" {
getOrgQuery := &models.GetOrgByNameQuery{Name: app.OrgName}
if err := bus.Dispatch(ctx, getOrgQuery); err != nil {
if err := ap.store.GetOrgByNameHandler(ctx, getOrgQuery); err != nil {
return err
}
app.OrgID = getOrgQuery.Result.Id
@@ -41,7 +48,7 @@ func (ap *PluginProvisioner) apply(ctx context.Context, cfg *pluginsAsConfig) er
}
query := &models.GetPluginSettingByIdQuery{OrgId: app.OrgID, PluginId: app.PluginID}
err := bus.Dispatch(ctx, query)
err := ap.store.GetPluginSettingById(ctx, query)
if err != nil {
if !errors.Is(err, models.ErrPluginSettingNotFound) {
return err
@@ -60,7 +67,7 @@ func (ap *PluginProvisioner) apply(ctx context.Context, cfg *pluginsAsConfig) er
SecureJsonData: app.SecureJSONData,
PluginVersion: app.PluginVersion,
}
if err := bus.Dispatch(ctx, cmd); err != nil {
if err := ap.store.UpdatePluginSetting(ctx, cmd); err != nil {
return err
}
}

View File

@@ -5,7 +5,6 @@ import (
"errors"
"testing"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/require"
@@ -21,32 +20,6 @@ func TestPluginProvisioner(t *testing.T) {
})
t.Run("Should apply configurations", func(t *testing.T) {
bus.AddHandler("test", func(ctx context.Context, query *models.GetOrgByNameQuery) error {
if query.Name == "Org 4" {
query.Result = &models.Org{Id: 4}
}
return nil
})
bus.AddHandler("test", func(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
if query.PluginId == "test-plugin" && query.OrgId == 2 {
query.Result = &models.PluginSetting{
PluginVersion: "2.0.1",
}
return nil
}
return models.ErrPluginSettingNotFound
})
sentCommands := []*models.UpdatePluginSettingCmd{}
bus.AddHandler("test", func(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
sentCommands = append(sentCommands, cmd)
return nil
})
cfg := []*pluginsAsConfig{
{
Apps: []*appFromConfig{
@@ -58,11 +31,12 @@ func TestPluginProvisioner(t *testing.T) {
},
}
reader := &testConfigReader{result: cfg}
ap := PluginProvisioner{log: log.New("test"), cfgProvider: reader}
store := &mockStore{}
ap := PluginProvisioner{log: log.New("test"), cfgProvider: reader, store: store}
err := ap.applyChanges(context.Background(), "")
require.NoError(t, err)
require.Len(t, sentCommands, 4)
require.Len(t, store.sentCommands, 4)
testCases := []struct {
ExpectedPluginID string
@@ -77,7 +51,7 @@ func TestPluginProvisioner(t *testing.T) {
}
for index, tc := range testCases {
cmd := sentCommands[index]
cmd := store.sentCommands[index]
require.NotNil(t, cmd)
require.Equal(t, tc.ExpectedPluginID, cmd.PluginId)
require.Equal(t, tc.ExpectedOrgID, cmd.OrgId)
@@ -95,3 +69,30 @@ type testConfigReader struct {
func (tcr *testConfigReader) readConfig(ctx context.Context, path string) ([]*pluginsAsConfig, error) {
return tcr.result, tcr.err
}
type mockStore struct {
sentCommands []*models.UpdatePluginSettingCmd
}
func (m *mockStore) GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error {
if query.Name == "Org 4" {
query.Result = &models.Org{Id: 4}
}
return nil
}
func (m *mockStore) GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
if query.PluginId == "test-plugin" && query.OrgId == 2 {
query.Result = &models.PluginSetting{
PluginVersion: "2.0.1",
}
return nil
}
return models.ErrPluginSettingNotFound
}
func (m *mockStore) UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
m.sentCommands = append(m.sentCommands, cmd)
return nil
}