Jelajahi Sumber

Merge branch 'master' into backend_plugins

* master:
  improve error handling for datasources as cfg
  improve sample datasource.yaml
bergquist 8 tahun lalu
induk
melakukan
f0abe696b5

+ 2 - 2
conf/provisioning/datasources/custom.yaml → conf/provisioning/datasources/sample.yaml

@@ -1,11 +1,11 @@
 # # list of datasources that should be deleted from the database
-# delete_datasources:
+#delete_datasources:
 #   - name: Graphite
 #     org_id: 1
 
 # # list of datasources to insert/update depending
 # # whats available in the datbase
-# datasources:
+#datasources:
 #   # <string, required> name of the datasource. Required
 # - name: Graphite
 #   # <string, required> datasource type. Required

+ 13 - 0
pkg/services/provisioning/dashboards/dashboard_test.go → pkg/services/provisioning/dashboards/config_reader_test.go

@@ -8,6 +8,7 @@ import (
 
 var (
 	simpleDashboardConfig string = "./test-configs/dashboards-from-disk"
+	brokenConfigs         string = "./test-configs/borken-configs"
 )
 
 func TestDashboardsAsConfig(t *testing.T) {
@@ -45,5 +46,17 @@ func TestDashboardsAsConfig(t *testing.T) {
 			So(len(ds2.Options), ShouldEqual, 1)
 			So(ds2.Options["folder"], ShouldEqual, "/var/lib/grafana/dashboards")
 		})
+
+		Convey("Should skip broken config files", func() {
+
+			cfgProvifer := configReader{path: brokenConfigs}
+			cfg, err := cfgProvifer.readConfig()
+			if err != nil {
+				t.Fatalf("readConfig return an error %v", err)
+			}
+
+			So(len(cfg), ShouldEqual, 0)
+
+		})
 	})
 }

+ 6 - 0
pkg/services/provisioning/dashboards/test-configs/broken-configs/commented.yaml

@@ -0,0 +1,6 @@
+# - name: 'default'
+#   org_id: 1
+#   folder: ''
+#   type: file
+#   options:
+#     folder: /var/lib/grafana/dashboards

+ 10 - 4
pkg/services/provisioning/datasources/datasources.go

@@ -118,13 +118,19 @@ func (configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) {
 				return nil, err
 			}
 
-			datasources = append(datasources, datasource)
+			if datasource != nil {
+				datasources = append(datasources, datasource)
+			}
 		}
 	}
 
 	defaultCount := 0
-	for _, cfg := range datasources {
-		for _, ds := range cfg.Datasources {
+	for i := range datasources {
+		if datasources[i].Datasources == nil {
+			continue
+		}
+
+		for _, ds := range datasources[i].Datasources {
 			if ds.OrgId == 0 {
 				ds.OrgId = 1
 			}
@@ -137,7 +143,7 @@ func (configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) {
 			}
 		}
 
-		for _, ds := range cfg.DeleteDatasources {
+		for _, ds := range datasources[i].DeleteDatasources {
 			if ds.OrgId == 0 {
 				ds.OrgId = 1
 			}

+ 48 - 0
pkg/services/provisioning/datasources/test-configs/broken-yaml/commented.yaml

@@ -0,0 +1,48 @@
+# # list of datasources that should be deleted from the database
+#delete_datasources:
+#   - name: Graphite
+#     org_id: 1
+
+# # list of datasources to insert/update depending
+# # whats available in the datbase
+#datasources:
+#   # <string, required> name of the datasource. Required
+# - name: Graphite
+#   # <string, required> datasource type. Required
+#   type: graphite
+#   # <string, required> access mode. direct or proxy. Required
+#   access: proxy
+#   # <int> org id. will default to org_id 1 if not specified
+#   org_id: 1
+#   # <string> url
+#   url: http://localhost:8080
+#   # <string> database password, if used
+#   password:
+#   # <string> database user, if used
+#   user:
+#   # <string> database name, if used
+#   database:
+#   # <bool> enable/disable basic auth
+#   basic_auth:
+#   # <string> basic auth username
+#   basic_auth_user:
+#   # <string> basic auth password
+#   basic_auth_password:
+#   # <bool> enable/disable with credentials headers
+#   with_credentials:
+#   # <bool> mark as default datasource. Max one per org
+#   is_default:
+#   # <map> fields that will be converted to json and stored in json_data
+#   json_data:
+#      graphiteVersion: "1.1"
+#      tlsAuth: true
+#      tlsAuthWithCACert: true
+#   # <string> json object of data that will be encrypted.
+#   secure_json_data:
+#     tlsCACert: "..."
+#     tlsClientCert: "..."
+#     tlsClientKey: "..."
+#   version: 1
+#   # <bool> allow users to edit datasources from the UI.
+#   editable: false
+