mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge pull request #12921 from grafana/12229_provisioning
Provisioning: Should allow one default datasource per organisation
This commit is contained in:
commit
88b96fbd15
@ -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 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 (
|
|||||||
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) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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 (
|
|||||||
)
|
)
|
||||||
|
|
||||||
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 {
|
||||||
|
25
pkg/services/provisioning/datasources/testdata/multiple-org-default/config.yaml
vendored
Normal file
25
pkg/services/provisioning/datasources/testdata/multiple-org-default/config.yaml
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user