mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
ProvisioningService: Change the ProvisioningService interface for easier extension (#32910)
* Made a public constructor in order to instantiate it from the service override * Removed unused plugins.DataRequestHandler * Added a Run and RunInitProvisioners methods that can be run from Enterprise * Adding a mock for Run and RunInitProvisioners as well
This commit is contained in:
parent
63e2977837
commit
a151dfaa04
@ -9,7 +9,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/dashboards"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
)
|
||||
|
||||
@ -24,7 +23,7 @@ type DashboardProvisioner interface {
|
||||
}
|
||||
|
||||
// DashboardProvisionerFactory creates DashboardProvisioners based on input
|
||||
type DashboardProvisionerFactory func(string, dashboards.Store, plugins.DataRequestHandler) (DashboardProvisioner, error)
|
||||
type DashboardProvisionerFactory func(string, dashboards.Store) (DashboardProvisioner, error)
|
||||
|
||||
// Provisioner is responsible for syncing dashboard from disk to Grafana's database.
|
||||
type Provisioner struct {
|
||||
@ -34,7 +33,7 @@ type Provisioner struct {
|
||||
}
|
||||
|
||||
// New returns a new DashboardProvisioner
|
||||
func New(configDirectory string, store dashboards.Store, reqHandler plugins.DataRequestHandler) (DashboardProvisioner, error) {
|
||||
func New(configDirectory string, store dashboards.Store) (DashboardProvisioner, error) {
|
||||
logger := log.New("provisioning.dashboard")
|
||||
cfgReader := &configReader{path: configDirectory, log: logger}
|
||||
configs, err := cfgReader.readConfig()
|
||||
|
@ -18,6 +18,8 @@ import (
|
||||
)
|
||||
|
||||
type ProvisioningService interface {
|
||||
registry.BackgroundService
|
||||
RunInitProvisioners() error
|
||||
ProvisionDatasources() error
|
||||
ProvisionPlugins() error
|
||||
ProvisionNotifications() error
|
||||
@ -28,17 +30,24 @@ type ProvisioningService interface {
|
||||
|
||||
func init() {
|
||||
registry.Register(®istry.Descriptor{
|
||||
Name: "ProvisioningService",
|
||||
Instance: newProvisioningServiceImpl(
|
||||
dashboards.New,
|
||||
notifiers.Provision,
|
||||
datasources.Provision,
|
||||
plugins.Provision,
|
||||
),
|
||||
Name: "ProvisioningService",
|
||||
Instance: NewProvisioningServiceImpl(),
|
||||
InitPriority: registry.Low,
|
||||
})
|
||||
}
|
||||
|
||||
// Add a public constructor for overriding service to be able to instantiate OSS as fallback
|
||||
func NewProvisioningServiceImpl() *provisioningServiceImpl {
|
||||
return &provisioningServiceImpl{
|
||||
log: log.New("provisioning"),
|
||||
newDashboardProvisioner: dashboards.New,
|
||||
provisionNotifiers: notifiers.Provision,
|
||||
provisionDatasources: datasources.Provision,
|
||||
provisionPlugins: plugins.Provision,
|
||||
}
|
||||
}
|
||||
|
||||
// Used for testing purposes
|
||||
func newProvisioningServiceImpl(
|
||||
newDashboardProvisioner dashboards.DashboardProvisionerFactory,
|
||||
provisionNotifiers func(string) error,
|
||||
@ -55,10 +64,9 @@ func newProvisioningServiceImpl(
|
||||
}
|
||||
|
||||
type provisioningServiceImpl struct {
|
||||
Cfg *setting.Cfg `inject:""`
|
||||
RequestHandler plugifaces.DataRequestHandler `inject:""`
|
||||
SQLStore *sqlstore.SQLStore `inject:""`
|
||||
PluginManager plugifaces.Manager `inject:""`
|
||||
Cfg *setting.Cfg `inject:""`
|
||||
SQLStore *sqlstore.SQLStore `inject:""`
|
||||
PluginManager plugifaces.Manager `inject:""`
|
||||
log log.Logger
|
||||
pollingCtxCancel context.CancelFunc
|
||||
newDashboardProvisioner dashboards.DashboardProvisionerFactory
|
||||
@ -70,6 +78,10 @@ type provisioningServiceImpl struct {
|
||||
}
|
||||
|
||||
func (ps *provisioningServiceImpl) Init() error {
|
||||
return ps.RunInitProvisioners()
|
||||
}
|
||||
|
||||
func (ps *provisioningServiceImpl) RunInitProvisioners() error {
|
||||
err := ps.ProvisionDatasources()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -137,7 +149,7 @@ func (ps *provisioningServiceImpl) ProvisionNotifications() error {
|
||||
|
||||
func (ps *provisioningServiceImpl) ProvisionDashboards() error {
|
||||
dashboardPath := filepath.Join(ps.Cfg.ProvisioningPath, "dashboards")
|
||||
dashProvisioner, err := ps.newDashboardProvisioner(dashboardPath, ps.SQLStore, ps.RequestHandler)
|
||||
dashProvisioner, err := ps.newDashboardProvisioner(dashboardPath, ps.SQLStore)
|
||||
if err != nil {
|
||||
return errutil.Wrap("Failed to create provisioner", err)
|
||||
}
|
||||
|
@ -1,22 +1,28 @@
|
||||
package provisioning
|
||||
|
||||
import "context"
|
||||
|
||||
type Calls struct {
|
||||
RunInitProvisioners []interface{}
|
||||
ProvisionDatasources []interface{}
|
||||
ProvisionPlugins []interface{}
|
||||
ProvisionNotifications []interface{}
|
||||
ProvisionDashboards []interface{}
|
||||
GetDashboardProvisionerResolvedPath []interface{}
|
||||
GetAllowUIUpdatesFromConfig []interface{}
|
||||
Run []interface{}
|
||||
}
|
||||
|
||||
type ProvisioningServiceMock struct {
|
||||
Calls *Calls
|
||||
RunInitProvisionersFunc func() error
|
||||
ProvisionDatasourcesFunc func() error
|
||||
ProvisionPluginsFunc func() error
|
||||
ProvisionNotificationsFunc func() error
|
||||
ProvisionDashboardsFunc func() error
|
||||
GetDashboardProvisionerResolvedPathFunc func(name string) string
|
||||
GetAllowUIUpdatesFromConfigFunc func(name string) bool
|
||||
RunFunc func(ctx context.Context) error
|
||||
}
|
||||
|
||||
func NewProvisioningServiceMock() *ProvisioningServiceMock {
|
||||
@ -25,6 +31,14 @@ func NewProvisioningServiceMock() *ProvisioningServiceMock {
|
||||
}
|
||||
}
|
||||
|
||||
func (mock *ProvisioningServiceMock) RunInitProvisioners() error {
|
||||
mock.Calls.RunInitProvisioners = append(mock.Calls.RunInitProvisioners, nil)
|
||||
if mock.RunInitProvisionersFunc != nil {
|
||||
return mock.RunInitProvisionersFunc()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mock *ProvisioningServiceMock) ProvisionDatasources() error {
|
||||
mock.Calls.ProvisionDatasources = append(mock.Calls.ProvisionDatasources, nil)
|
||||
if mock.ProvisionDatasourcesFunc != nil {
|
||||
@ -72,3 +86,11 @@ func (mock *ProvisioningServiceMock) GetAllowUIUpdatesFromConfig(name string) bo
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (mock *ProvisioningServiceMock) Run(ctx context.Context) error {
|
||||
mock.Calls.Run = append(mock.Calls.Run, nil)
|
||||
if mock.RunFunc != nil {
|
||||
return mock.RunFunc(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"time"
|
||||
|
||||
dboards "github.com/grafana/grafana/pkg/dashboards"
|
||||
plugifaces "github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/services/provisioning/dashboards"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -93,7 +92,7 @@ func setup() *serviceTestStruct {
|
||||
}
|
||||
|
||||
serviceTest.service = newProvisioningServiceImpl(
|
||||
func(string, dboards.Store, plugifaces.DataRequestHandler) (dashboards.DashboardProvisioner, error) {
|
||||
func(string, dboards.Store) (dashboards.DashboardProvisioner, error) {
|
||||
return serviceTest.mock, nil
|
||||
},
|
||||
nil,
|
||||
|
Loading…
Reference in New Issue
Block a user