2018-02-12 08:17:32 -06:00
|
|
|
package datasources
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2018-02-13 07:29:56 -06:00
|
|
|
"os"
|
2018-02-12 08:17:32 -06:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type configReader struct {
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) {
|
|
|
|
var datasources []*DatasourcesAsConfig
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
2018-04-13 11:40:14 -05:00
|
|
|
cr.log.Error("can't read datasource provisioning files from directory", "path", path)
|
2018-02-12 08:17:32 -06:00
|
|
|
return datasources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2018-02-20 00:33:24 -06:00
|
|
|
if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") {
|
2018-02-13 07:29:56 -06:00
|
|
|
datasource, err := cr.parseDatasourceConfig(path, file)
|
2018-02-12 08:17:32 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if datasource != nil {
|
|
|
|
datasources = append(datasources, datasource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateDefaultUniqueness(datasources)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return datasources, nil
|
|
|
|
}
|
|
|
|
|
2018-02-13 07:29:56 -06:00
|
|
|
func (cr *configReader) parseDatasourceConfig(path string, file os.FileInfo) (*DatasourcesAsConfig, error) {
|
|
|
|
filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
|
|
|
|
yamlFile, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-12 08:17:32 -06:00
|
|
|
var apiVersion *ConfigVersion
|
2018-02-13 08:02:27 -06:00
|
|
|
err = yaml.Unmarshal(yamlFile, &apiVersion)
|
2018-02-12 08:17:32 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-14 04:22:24 -06:00
|
|
|
if apiVersion == nil {
|
|
|
|
apiVersion = &ConfigVersion{ApiVersion: 0}
|
|
|
|
}
|
|
|
|
|
2018-02-12 08:17:32 -06:00
|
|
|
if apiVersion.ApiVersion > 0 {
|
|
|
|
var v1 *DatasourcesAsConfigV1
|
|
|
|
err = yaml.Unmarshal(yamlFile, &v1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-13 07:29:56 -06:00
|
|
|
return v1.mapToDatasourceFromConfig(apiVersion.ApiVersion), nil
|
2018-02-12 08:17:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var v0 *DatasourcesAsConfigV0
|
|
|
|
err = yaml.Unmarshal(yamlFile, &v0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-13 07:29:56 -06:00
|
|
|
|
|
|
|
cr.log.Warn("[Deprecated] the datasource provisioning config is outdated. please upgrade", "filename", filename)
|
|
|
|
|
2018-02-12 08:17:32 -06:00
|
|
|
return v0.mapToDatasourceFromConfig(apiVersion.ApiVersion), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateDefaultUniqueness(datasources []*DatasourcesAsConfig) error {
|
|
|
|
defaultCount := 0
|
|
|
|
for i := range datasources {
|
|
|
|
if datasources[i].Datasources == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range datasources[i].Datasources {
|
|
|
|
if ds.OrgId == 0 {
|
|
|
|
ds.OrgId = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if ds.IsDefault {
|
|
|
|
defaultCount++
|
|
|
|
if defaultCount > 1 {
|
|
|
|
return ErrInvalidConfigToManyDefault
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range datasources[i].DeleteDatasources {
|
|
|
|
if ds.OrgId == 0 {
|
|
|
|
ds.OrgId = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|