config_reader.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package dashboards
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/grafana/grafana/pkg/log"
  9. yaml "gopkg.in/yaml.v2"
  10. )
  11. type configReader struct {
  12. path string
  13. log log.Logger
  14. }
  15. func (cr *configReader) parseConfigs(file os.FileInfo) ([]*DashboardsAsConfig, error) {
  16. filename, _ := filepath.Abs(filepath.Join(cr.path, file.Name()))
  17. yamlFile, err := ioutil.ReadFile(filename)
  18. if err != nil {
  19. return nil, err
  20. }
  21. apiVersion := &ConfigVersion{ApiVersion: 0}
  22. // We ignore the error here because it errors out for version 0 which does not have apiVersion
  23. // specified (so 0 is default). This can also error in case the apiVersion is not an integer but at the moment
  24. // this does not handle that case and would still go on as if version = 0.
  25. // TODO: return appropriate error in case the apiVersion is specified but isn't integer (or even if it is
  26. // integer > max version?).
  27. _ = yaml.Unmarshal(yamlFile, &apiVersion)
  28. if apiVersion.ApiVersion > 0 {
  29. v1 := &DashboardAsConfigV1{}
  30. err := yaml.Unmarshal(yamlFile, &v1)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if v1 != nil {
  35. return v1.mapToDashboardAsConfig(), nil
  36. }
  37. } else {
  38. var v0 []*DashboardsAsConfigV0
  39. err := yaml.Unmarshal(yamlFile, &v0)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if v0 != nil {
  44. cr.log.Warn("[Deprecated] the dashboard provisioning config is outdated. please upgrade", "filename", filename)
  45. return mapV0ToDashboardAsConfig(v0), nil
  46. }
  47. }
  48. return []*DashboardsAsConfig{}, nil
  49. }
  50. func (cr *configReader) readConfig() ([]*DashboardsAsConfig, error) {
  51. var dashboards []*DashboardsAsConfig
  52. files, err := ioutil.ReadDir(cr.path)
  53. if err != nil {
  54. cr.log.Error("can't read dashboard provisioning files from directory", "path", cr.path, "error", err)
  55. return dashboards, nil
  56. }
  57. for _, file := range files {
  58. if !strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml") {
  59. continue
  60. }
  61. parsedDashboards, err := cr.parseConfigs(file)
  62. if err != nil {
  63. return nil, fmt.Errorf("could not parse provisioning config file: %s error: %v", file.Name(), err)
  64. }
  65. if len(parsedDashboards) > 0 {
  66. dashboards = append(dashboards, parsedDashboards...)
  67. }
  68. }
  69. uidUsage := map[string]uint8{}
  70. for _, dashboard := range dashboards {
  71. if dashboard.OrgId == 0 {
  72. dashboard.OrgId = 1
  73. }
  74. if dashboard.UpdateIntervalSeconds == 0 {
  75. dashboard.UpdateIntervalSeconds = 10
  76. }
  77. if len(dashboard.FolderUid) > 0 {
  78. uidUsage[dashboard.FolderUid] += 1
  79. }
  80. }
  81. for uid, times := range uidUsage {
  82. if times > 1 {
  83. cr.log.Error("the same 'folderUid' is used more than once", "folderUid", uid)
  84. }
  85. }
  86. return dashboards, nil
  87. }