Skip to content

Commit

Permalink
Provisioning: Ensure that the default value for orgID is set when pro…
Browse files Browse the repository at this point in the history
…visioning datasources to be deleted (grafana#44244)

Fixes grafana#44243

Signed-off-by: Maicon Costa <maiconscosta@gmail.com>
  • Loading branch information
filewalkwithme committed Feb 9, 2022
1 parent b615d05 commit 8e03541
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/services/provisioning/datasources/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ func (cr *configReader) parseDatasourceConfig(path string, file os.FileInfo) (*c
func (cr *configReader) validateDefaultUniqueness(ctx context.Context, datasources []*configs) error {
defaultCount := map[int64]int{}
for i := range datasources {
if datasources[i].Datasources == nil {
continue
}

for _, ds := range datasources[i].Datasources {
if ds == nil {
continue
}

if ds.OrgID == 0 {
ds.OrgID = 1
}
Expand All @@ -115,6 +115,10 @@ func (cr *configReader) validateDefaultUniqueness(ctx context.Context, datasourc
}

for _, ds := range datasources[i].DeleteDatasources {
if ds == nil {
continue
}

if ds.OrgID == 0 {
ds.OrgID = 1
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/services/provisioning/datasources/config_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (

twoDatasourcesConfig = "testdata/two-datasources"
twoDatasourcesConfigPurgeOthers = "testdata/insert-two-delete-two"
deleteOneDatasource = "testdata/delete-one"
doubleDatasourcesConfig = "testdata/double-default"
allProperties = "testdata/all-properties"
versionZero = "testdata/version-0"
Expand Down Expand Up @@ -129,6 +130,27 @@ func TestDatasourceAsConfig(t *testing.T) {
})
})

t.Run("Remove one datasource", func(t *testing.T) {
setup()
t.Run("Remove one datasource", func(t *testing.T) {
fakeRepo.loadAll = []*models.DataSource{}

t.Run("should have removed old datasource", func(t *testing.T) {
dc := newDatasourceProvisioner(logger)
err := dc.applyChanges(context.Background(), deleteOneDatasource)
if err != nil {
t.Fatalf("applyChanges return an error %v", err)
}

require.Equal(t, 1, len(fakeRepo.deleted))
// should have set OrgID to 1
require.Equal(t, fakeRepo.deleted[0].OrgID, int64(1))
require.Equal(t, 0, len(fakeRepo.inserted))
require.Equal(t, len(fakeRepo.updated), 0)
})
})
})

t.Run("Two configured datasource and purge others ", func(t *testing.T) {
setup()
t.Run("two other datasources in database", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
datasources: []
delete_datasources:
- name: old-data-source

0 comments on commit 8e03541

Please sign in to comment.