Commit 5f5cdad9 by bergquist

improve error handling for datasources as cfg

parent c7668023
# # list of datasources that should be deleted from the database # # list of datasources that should be deleted from the database
delete_datasources: #delete_datasources:
# - name: Graphite # - name: Graphite
# org_id: 1 # org_id: 1
# # list of datasources to insert/update depending # # list of datasources to insert/update depending
# # whats available in the datbase # # whats available in the datbase
datasources: #datasources:
# # <string, required> name of the datasource. Required # # <string, required> name of the datasource. Required
# - name: Graphite # - name: Graphite
# # <string, required> datasource type. Required # # <string, required> datasource type. Required
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
var ( var (
simpleDashboardConfig string = "./test-configs/dashboards-from-disk" simpleDashboardConfig string = "./test-configs/dashboards-from-disk"
brokenConfigs string = "./test-configs/borken-configs"
) )
func TestDashboardsAsConfig(t *testing.T) { func TestDashboardsAsConfig(t *testing.T) {
...@@ -45,5 +46,17 @@ func TestDashboardsAsConfig(t *testing.T) { ...@@ -45,5 +46,17 @@ func TestDashboardsAsConfig(t *testing.T) {
So(len(ds2.Options), ShouldEqual, 1) So(len(ds2.Options), ShouldEqual, 1)
So(ds2.Options["folder"], ShouldEqual, "/var/lib/grafana/dashboards") 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)
})
}) })
} }
# - name: 'default'
# org_id: 1
# folder: ''
# type: file
# options:
# folder: /var/lib/grafana/dashboards
...@@ -118,13 +118,19 @@ func (configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) { ...@@ -118,13 +118,19 @@ func (configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) {
return nil, err return nil, err
} }
datasources = append(datasources, datasource) if datasource != nil {
datasources = append(datasources, datasource)
}
} }
} }
defaultCount := 0 defaultCount := 0
for _, cfg := range datasources { for i := range datasources {
for _, ds := range cfg.Datasources { if datasources[i].Datasources == nil {
continue
}
for _, ds := range datasources[i].Datasources {
if ds.OrgId == 0 { if ds.OrgId == 0 {
ds.OrgId = 1 ds.OrgId = 1
} }
...@@ -137,7 +143,7 @@ func (configReader) readConfig(path string) ([]*DatasourcesAsConfig, error) { ...@@ -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 { if ds.OrgId == 0 {
ds.OrgId = 1 ds.OrgId = 1
} }
......
# # 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment