mirror of
https://github.com/grafana/grafana.git
synced 2026-08-18 17:15:08 -05:00
Chore: Propagate context for data source provisioning (#40235)
* context all the things * apply feedback * rollback some alerting changes * rollback some alerting changes #2 * more rollbacks * more rollbacks #2 * more rollbacks #3 * more rollbacks #4 * fix integration test * add missing context * add missing and remove incorrect dispatch
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package datasources
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -39,7 +40,7 @@ func TestDatasourceAsConfig(t *testing.T) {
|
||||
|
||||
Convey("apply default values when missing", func() {
|
||||
dc := newDatasourceProvisioner(logger)
|
||||
err := dc.applyChanges(withoutDefaults)
|
||||
err := dc.applyChanges(context.Background(), withoutDefaults)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
@@ -52,7 +53,7 @@ func TestDatasourceAsConfig(t *testing.T) {
|
||||
Convey("One configured datasource", func() {
|
||||
Convey("no datasource in database", func() {
|
||||
dc := newDatasourceProvisioner(logger)
|
||||
err := dc.applyChanges(twoDatasourcesConfig)
|
||||
err := dc.applyChanges(context.Background(), twoDatasourcesConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
@@ -69,7 +70,7 @@ func TestDatasourceAsConfig(t *testing.T) {
|
||||
|
||||
Convey("should update one datasource", func() {
|
||||
dc := newDatasourceProvisioner(logger)
|
||||
err := dc.applyChanges(twoDatasourcesConfig)
|
||||
err := dc.applyChanges(context.Background(), twoDatasourcesConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
@@ -82,7 +83,7 @@ func TestDatasourceAsConfig(t *testing.T) {
|
||||
|
||||
Convey("Two datasources with is_default", func() {
|
||||
dc := newDatasourceProvisioner(logger)
|
||||
err := dc.applyChanges(doubleDatasourcesConfig)
|
||||
err := dc.applyChanges(context.Background(), doubleDatasourcesConfig)
|
||||
Convey("should raise error", func() {
|
||||
So(err, ShouldEqual, ErrInvalidConfigToManyDefault)
|
||||
})
|
||||
@@ -91,7 +92,7 @@ func TestDatasourceAsConfig(t *testing.T) {
|
||||
|
||||
Convey("Multiple datasources in different organizations with isDefault in each organization", func() {
|
||||
dc := newDatasourceProvisioner(logger)
|
||||
err := dc.applyChanges(multipleOrgsWithDefault)
|
||||
err := dc.applyChanges(context.Background(), multipleOrgsWithDefault)
|
||||
Convey("should not raise error", func() {
|
||||
So(err, ShouldBeNil)
|
||||
So(len(fakeRepo.inserted), ShouldEqual, 4)
|
||||
@@ -111,7 +112,7 @@ func TestDatasourceAsConfig(t *testing.T) {
|
||||
|
||||
Convey("should have two new datasources", func() {
|
||||
dc := newDatasourceProvisioner(logger)
|
||||
err := dc.applyChanges(twoDatasourcesConfigPurgeOthers)
|
||||
err := dc.applyChanges(context.Background(), twoDatasourcesConfigPurgeOthers)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
@@ -132,7 +133,7 @@ func TestDatasourceAsConfig(t *testing.T) {
|
||||
|
||||
Convey("should have two new datasources", func() {
|
||||
dc := newDatasourceProvisioner(logger)
|
||||
err := dc.applyChanges(twoDatasourcesConfig)
|
||||
err := dc.applyChanges(context.Background(), twoDatasourcesConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("applyChanges return an error %v", err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package datasources
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@@ -18,9 +19,9 @@ var (
|
||||
|
||||
// Provision scans a directory for provisioning config files
|
||||
// and provisions the datasource in those files.
|
||||
func Provision(configDirectory string) error {
|
||||
func Provision(ctx context.Context, configDirectory string) error {
|
||||
dc := newDatasourceProvisioner(log.New("provisioning.datasources"))
|
||||
return dc.applyChanges(configDirectory)
|
||||
return dc.applyChanges(ctx, configDirectory)
|
||||
}
|
||||
|
||||
// DatasourceProvisioner is responsible for provisioning datasources based on
|
||||
@@ -37,14 +38,14 @@ func newDatasourceProvisioner(log log.Logger) DatasourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
func (dc *DatasourceProvisioner) apply(cfg *configs) error {
|
||||
if err := dc.deleteDatasources(cfg.DeleteDatasources); err != nil {
|
||||
func (dc *DatasourceProvisioner) apply(ctx context.Context, cfg *configs) error {
|
||||
if err := dc.deleteDatasources(ctx, cfg.DeleteDatasources); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, ds := range cfg.Datasources {
|
||||
cmd := &models.GetDataSourceQuery{OrgId: ds.OrgID, Name: ds.Name}
|
||||
err := bus.Dispatch(cmd)
|
||||
err := bus.DispatchCtx(ctx, cmd)
|
||||
if err != nil && !errors.Is(err, models.ErrDataSourceNotFound) {
|
||||
return err
|
||||
}
|
||||
@@ -52,13 +53,13 @@ func (dc *DatasourceProvisioner) apply(cfg *configs) error {
|
||||
if errors.Is(err, models.ErrDataSourceNotFound) {
|
||||
dc.log.Info("inserting datasource from configuration ", "name", ds.Name, "uid", ds.UID)
|
||||
insertCmd := createInsertCommand(ds)
|
||||
if err := bus.Dispatch(insertCmd); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, insertCmd); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
dc.log.Debug("updating datasource from configuration", "name", ds.Name, "uid", ds.UID)
|
||||
updateCmd := createUpdateCommand(ds, cmd.Result.Id)
|
||||
if err := bus.Dispatch(updateCmd); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, updateCmd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -67,14 +68,14 @@ func (dc *DatasourceProvisioner) apply(cfg *configs) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dc *DatasourceProvisioner) applyChanges(configPath string) error {
|
||||
func (dc *DatasourceProvisioner) applyChanges(ctx context.Context, configPath string) error {
|
||||
configs, err := dc.cfgProvider.readConfig(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cfg := range configs {
|
||||
if err := dc.apply(cfg); err != nil {
|
||||
if err := dc.apply(ctx, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -82,10 +83,10 @@ func (dc *DatasourceProvisioner) applyChanges(configPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dc *DatasourceProvisioner) deleteDatasources(dsToDelete []*deleteDatasourceConfig) error {
|
||||
func (dc *DatasourceProvisioner) deleteDatasources(ctx context.Context, dsToDelete []*deleteDatasourceConfig) error {
|
||||
for _, ds := range dsToDelete {
|
||||
cmd := &models.DeleteDataSourceCommand{OrgID: ds.OrgID, Name: ds.Name}
|
||||
if err := bus.Dispatch(cmd); err != nil {
|
||||
if err := bus.DispatchCtx(ctx, cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user