|
|
@@ -104,17 +104,56 @@ func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) {
|
|
|
c.JSON(200, util.DynMap{"message": "Datasource added", "id": cmd.Result.Id})
|
|
|
}
|
|
|
|
|
|
-func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) {
|
|
|
+func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) Response {
|
|
|
cmd.OrgId = c.OrgId
|
|
|
cmd.Id = c.ParamsInt64(":id")
|
|
|
|
|
|
- err := bus.Dispatch(&cmd)
|
|
|
+ err := fillWithSecureJsonData(&cmd)
|
|
|
if err != nil {
|
|
|
- c.JsonApiErr(500, "Failed to update datasource", err)
|
|
|
- return
|
|
|
+ return ApiError(500, "Failed to update datasource", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = bus.Dispatch(&cmd)
|
|
|
+ if err != nil {
|
|
|
+ return ApiError(500, "Failed to update datasource", err)
|
|
|
}
|
|
|
|
|
|
- c.JsonOK("Datasource updated")
|
|
|
+ return Json(200, "Datasource updated")
|
|
|
+}
|
|
|
+
|
|
|
+func fillWithSecureJsonData(cmd *m.UpdateDataSourceCommand) error {
|
|
|
+ if len(cmd.SecureJsonData) == 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ ds, err := getRawDataSourceById(cmd.Id, cmd.OrgId)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ secureJsonData := ds.SecureJsonData.Decrypt()
|
|
|
+
|
|
|
+ for k, v := range secureJsonData {
|
|
|
+
|
|
|
+ if _, ok := cmd.SecureJsonData[k]; !ok {
|
|
|
+ cmd.SecureJsonData[k] = v
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func getRawDataSourceById(id int64, orgId int64) (*m.DataSource, error) {
|
|
|
+ query := m.GetDataSourceByIdQuery{
|
|
|
+ Id: id,
|
|
|
+ OrgId: orgId,
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := bus.Dispatch(&query); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return query.Result, nil
|
|
|
}
|
|
|
|
|
|
// Get /api/datasources/name/:name
|
|
|
@@ -152,7 +191,7 @@ func GetDataSourceIdByName(c *middleware.Context) Response {
|
|
|
}
|
|
|
|
|
|
func convertModelToDtos(ds *m.DataSource) dtos.DataSource {
|
|
|
- return dtos.DataSource{
|
|
|
+ dto := dtos.DataSource{
|
|
|
Id: ds.Id,
|
|
|
OrgId: ds.OrgId,
|
|
|
Name: ds.Name,
|
|
|
@@ -169,4 +208,12 @@ func convertModelToDtos(ds *m.DataSource) dtos.DataSource {
|
|
|
IsDefault: ds.IsDefault,
|
|
|
JsonData: ds.JsonData,
|
|
|
}
|
|
|
+
|
|
|
+ if len(ds.SecureJsonData) > 0 {
|
|
|
+ dto.TLSAuth.CACertSet = len(ds.SecureJsonData["tlsCACert"]) > 0
|
|
|
+ dto.TLSAuth.ClientCertSet = len(ds.SecureJsonData["tlsClientCert"]) > 0
|
|
|
+ dto.TLSAuth.ClientKeySet = len(ds.SecureJsonData["tlsClientKey"]) > 0
|
|
|
+ }
|
|
|
+
|
|
|
+ return dto
|
|
|
}
|