config_reader.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package datasources
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/grafana/grafana/pkg/infra/log"
  8. "gopkg.in/yaml.v2"
  9. )
  10. type configReader struct {
  11. log log.Logger
  12. }
  13. func (cr *configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) {
  14. var datasources []*DatasourcesAsConfig
  15. files, err := ioutil.ReadDir(path)
  16. if err != nil {
  17. cr.log.Error("can't read datasource provisioning files from directory", "path", path, "error", err)
  18. return datasources, nil
  19. }
  20. for _, file := range files {
  21. if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") {
  22. datasource, err := cr.parseDatasourceConfig(path, file)
  23. if err != nil {
  24. return nil, err
  25. }
  26. if datasource != nil {
  27. datasources = append(datasources, datasource)
  28. }
  29. }
  30. }
  31. err = validateDefaultUniqueness(datasources)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return datasources, nil
  36. }
  37. func (cr *configReader) parseDatasourceConfig(path string, file os.FileInfo) (*DatasourcesAsConfig, error) {
  38. filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
  39. yamlFile, err := ioutil.ReadFile(filename)
  40. if err != nil {
  41. return nil, err
  42. }
  43. var apiVersion *ConfigVersion
  44. err = yaml.Unmarshal(yamlFile, &apiVersion)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if apiVersion == nil {
  49. apiVersion = &ConfigVersion{ApiVersion: 0}
  50. }
  51. if apiVersion.ApiVersion > 0 {
  52. v1 := &DatasourcesAsConfigV1{log: cr.log}
  53. err = yaml.Unmarshal(yamlFile, v1)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return v1.mapToDatasourceFromConfig(apiVersion.ApiVersion), nil
  58. }
  59. var v0 *DatasourcesAsConfigV0
  60. err = yaml.Unmarshal(yamlFile, &v0)
  61. if err != nil {
  62. return nil, err
  63. }
  64. cr.log.Warn("[Deprecated] the datasource provisioning config is outdated. please upgrade", "filename", filename)
  65. return v0.mapToDatasourceFromConfig(apiVersion.ApiVersion), nil
  66. }
  67. func validateDefaultUniqueness(datasources []*DatasourcesAsConfig) error {
  68. defaultCount := map[int64]int{}
  69. for i := range datasources {
  70. if datasources[i].Datasources == nil {
  71. continue
  72. }
  73. for _, ds := range datasources[i].Datasources {
  74. if ds.OrgId == 0 {
  75. ds.OrgId = 1
  76. }
  77. if ds.IsDefault {
  78. defaultCount[ds.OrgId] = defaultCount[ds.OrgId] + 1
  79. if defaultCount[ds.OrgId] > 1 {
  80. return ErrInvalidConfigToManyDefault
  81. }
  82. }
  83. }
  84. for _, ds := range datasources[i].DeleteDatasources {
  85. if ds.OrgId == 0 {
  86. ds.OrgId = 1
  87. }
  88. }
  89. }
  90. return nil
  91. }