Commit 88b96fbd by Marcus Efraimsson Committed by GitHub

Merge pull request #12921 from grafana/12229_provisioning

Provisioning: Should allow one default datasource per organisation
parents 2bb87446 0a7be261
...@@ -83,7 +83,7 @@ func (cr *configReader) parseDatasourceConfig(path string, file os.FileInfo) (*D ...@@ -83,7 +83,7 @@ func (cr *configReader) parseDatasourceConfig(path string, file os.FileInfo) (*D
} }
func validateDefaultUniqueness(datasources []*DatasourcesAsConfig) error { func validateDefaultUniqueness(datasources []*DatasourcesAsConfig) error {
defaultCount := 0 defaultCount := map[int64]int{}
for i := range datasources { for i := range datasources {
if datasources[i].Datasources == nil { if datasources[i].Datasources == nil {
continue continue
...@@ -95,8 +95,8 @@ func validateDefaultUniqueness(datasources []*DatasourcesAsConfig) error { ...@@ -95,8 +95,8 @@ func validateDefaultUniqueness(datasources []*DatasourcesAsConfig) error {
} }
if ds.IsDefault { if ds.IsDefault {
defaultCount++ defaultCount[ds.OrgId] = defaultCount[ds.OrgId] + 1
if defaultCount > 1 { if defaultCount[ds.OrgId] > 1 {
return ErrInvalidConfigToManyDefault return ErrInvalidConfigToManyDefault
} }
} }
......
...@@ -19,6 +19,7 @@ var ( ...@@ -19,6 +19,7 @@ var (
allProperties = "testdata/all-properties" allProperties = "testdata/all-properties"
versionZero = "testdata/version-0" versionZero = "testdata/version-0"
brokenYaml = "testdata/broken-yaml" brokenYaml = "testdata/broken-yaml"
multipleOrgsWithDefault = "testdata/multiple-org-default"
fakeRepo *fakeRepository fakeRepo *fakeRepository
) )
...@@ -73,6 +74,19 @@ func TestDatasourceAsConfig(t *testing.T) { ...@@ -73,6 +74,19 @@ func TestDatasourceAsConfig(t *testing.T) {
}) })
}) })
Convey("Multiple datasources in different organizations with isDefault in each organization", func() {
dc := newDatasourceProvisioner(logger)
err := dc.applyChanges(multipleOrgsWithDefault)
Convey("should not raise error", func() {
So(err, ShouldBeNil)
So(len(fakeRepo.inserted), ShouldEqual, 4)
So(fakeRepo.inserted[0].IsDefault, ShouldBeTrue)
So(fakeRepo.inserted[0].OrgId, ShouldEqual, 1)
So(fakeRepo.inserted[2].IsDefault, ShouldBeTrue)
So(fakeRepo.inserted[2].OrgId, ShouldEqual, 2)
})
})
Convey("Two configured datasource and purge others ", func() { Convey("Two configured datasource and purge others ", func() {
Convey("two other datasources in database", func() { Convey("two other datasources in database", func() {
fakeRepo.loadAll = []*models.DataSource{ fakeRepo.loadAll = []*models.DataSource{
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
) )
var ( var (
ErrInvalidConfigToManyDefault = errors.New("datasource.yaml config is invalid. Only one datasource can be marked as default") ErrInvalidConfigToManyDefault = errors.New("datasource.yaml config is invalid. Only one datasource per organization can be marked as default")
) )
func Provision(configDirectory string) error { func Provision(configDirectory string) error {
......
apiVersion: 1
datasources:
- orgId: 1
name: prometheus
type: prometheus
isDefault: True
access: proxy
url: http://prometheus.example.com:9090
- name: Graphite
type: graphite
access: proxy
url: http://localhost:8080
- orgId: 2
name: prometheus
type: prometheus
isDefault: True
access: proxy
url: http://prometheus.example.com:9090
- orgId: 2
name: Graphite
type: graphite
access: proxy
url: http://localhost:8080
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment