datasources as cfg: convert yaml map into json for jsonData

This commit is contained in:
bergquist
2017-11-16 13:23:42 +01:00
parent 6267ef1391
commit 87983021e2
4 changed files with 50 additions and 27 deletions

View File

@@ -32,10 +32,13 @@ datasources:
# with_credentials: # with_credentials:
# # <bool> mark as default datasource. Max one per org # # <bool> mark as default datasource. Max one per org
# is_default: # is_default:
# # <string> json data # # <map> fields that will be converted to json and stored in json_data
# json_data: '{"graphiteVersion":"0.9"}' # json_data:
# # <string> json object of data that will be encrypted in UI. # graphiteVersion: "1.1"
# secure_json_fields: '' # tlsAuth: true
# tlsAuthWithCACert: true
# # <string> json object of data that will be encrypted.
# secure_json_data: ''
# # <int> including this value guarantees that instance with old configs cannot # # <int> including this value guarantees that instance with old configs cannot
# # overwrite your last change. # # overwrite your last change.
# version: 1 # version: 1

View File

@@ -146,6 +146,16 @@ func TestDatasourceAsConfig(t *testing.T) {
So(ds.IsDefault, ShouldBeTrue) So(ds.IsDefault, ShouldBeTrue)
So(ds.Editable, ShouldBeTrue) So(ds.Editable, ShouldBeTrue)
So(len(ds.JsonData), ShouldBeGreaterThan, 2)
So(ds.JsonData["graphiteVersion"], ShouldEqual, "1.1")
So(ds.JsonData["tlsAuth"], ShouldEqual, true)
So(ds.JsonData["tlsAuthWithCACert"], ShouldEqual, true)
So(len(ds.SecureJsonData), ShouldBeGreaterThan, 2)
So(ds.SecureJsonData["tlsCACert"], ShouldEqual, "MjNOcW9RdkbUDHZmpco2HCYzVq9dE+i6Yi+gmUJotq5CDA==")
So(ds.SecureJsonData["tlsClientCert"], ShouldEqual, "ckN0dGlyMXN503YNfjTcf9CV+GGQneN+xmAclQ==")
So(ds.SecureJsonData["tlsClientKey"], ShouldEqual, "ZkN4aG1aNkja/gKAB1wlnKFIsy2SRDq4slrM0A==")
dstwo := cfg[1].Datasources[0] dstwo := cfg[1].Datasources[0]
So(dstwo.Name, ShouldEqual, "name2") So(dstwo.Name, ShouldEqual, "name2")
}) })

View File

@@ -12,6 +12,12 @@ datasources:
basic_auth_password: basic_auth_password basic_auth_password: basic_auth_password
with_credentials: true with_credentials: true
is_default: true is_default: true
json_data: '{"graphiteVersion":"0.9"}' json_data:
secure_json_fields: '' graphiteVersion: "1.1"
tlsAuth: true
tlsAuthWithCACert: true
secure_json_data:
tlsCACert: "MjNOcW9RdkbUDHZmpco2HCYzVq9dE+i6Yi+gmUJotq5CDA=="
tlsClientCert: "ckN0dGlyMXN503YNfjTcf9CV+GGQneN+xmAclQ=="
tlsClientKey: "ZkN4aG1aNkja/gKAB1wlnKFIsy2SRDq4slrM0A=="
editable: true editable: true

View File

@@ -17,27 +17,29 @@ type DataSourceFromConfig struct {
OrgId int64 `json:"org_id" yaml:"org_id"` OrgId int64 `json:"org_id" yaml:"org_id"`
Version int `json:"version" yaml:"version"` Version int `json:"version" yaml:"version"`
Name string `json:"name" yaml:"name"` Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"` Type string `json:"type" yaml:"type"`
Access string `json:"access" yaml:"access"` Access string `json:"access" yaml:"access"`
Url string `json:"url" yaml:"url"` Url string `json:"url" yaml:"url"`
Password string `json:"password" yaml:"password"` Password string `json:"password" yaml:"password"`
User string `json:"user" yaml:"user"` User string `json:"user" yaml:"user"`
Database string `json:"database" yaml:"database"` Database string `json:"database" yaml:"database"`
BasicAuth bool `json:"basic_auth" yaml:"basic_auth"` BasicAuth bool `json:"basic_auth" yaml:"basic_auth"`
BasicAuthUser string `json:"basic_auth_user" yaml:"basic_auth_user"` BasicAuthUser string `json:"basic_auth_user" yaml:"basic_auth_user"`
BasicAuthPassword string `json:"basic_auth_password" yaml:"basic_auth_password"` BasicAuthPassword string `json:"basic_auth_password" yaml:"basic_auth_password"`
WithCredentials bool `json:"with_credentials" yaml:"with_credentials"` WithCredentials bool `json:"with_credentials" yaml:"with_credentials"`
IsDefault bool `json:"is_default" yaml:"is_default"` IsDefault bool `json:"is_default" yaml:"is_default"`
JsonData string `json:"json_data" yaml:"json_data"` JsonData map[string]interface{} `json:"json_data" yaml:"json_data"`
SecureJsonData map[string]string `json:"secure_json_data" yaml:"secure_json_data"` SecureJsonData map[string]string `json:"secure_json_data" yaml:"secure_json_data"`
Editable bool `json:"editable" yaml:"editable"` Editable bool `json:"editable" yaml:"editable"`
} }
func createInsertCommand(ds *DataSourceFromConfig) *models.AddDataSourceCommand { func createInsertCommand(ds *DataSourceFromConfig) *models.AddDataSourceCommand {
jsonData, err := simplejson.NewJson([]byte(ds.JsonData)) jsonData := simplejson.New()
if err != nil { if len(ds.JsonData) > 0 {
jsonData = simplejson.New() for k, v := range ds.JsonData {
jsonData.Set(k, v)
}
} }
return &models.AddDataSourceCommand{ return &models.AddDataSourceCommand{
@@ -61,9 +63,11 @@ func createInsertCommand(ds *DataSourceFromConfig) *models.AddDataSourceCommand
} }
func createUpdateCommand(ds *DataSourceFromConfig, id int64) *models.UpdateDataSourceCommand { func createUpdateCommand(ds *DataSourceFromConfig, id int64) *models.UpdateDataSourceCommand {
jsonData, err := simplejson.NewJson([]byte(ds.JsonData)) jsonData := simplejson.New()
if err != nil { if len(ds.JsonData) > 0 {
jsonData = simplejson.New() for k, v := range ds.JsonData {
jsonData.Set(k, v)
}
} }
return &models.UpdateDataSourceCommand{ return &models.UpdateDataSourceCommand{