grafana/pkg/api/datasources.go

270 lines
6.2 KiB
Go
Raw Normal View History

package api
import (
"sort"
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/util"
)
2018-03-07 10:54:50 -06:00
func GetDataSources(c *m.ReqContext) Response {
query := m.GetDataSourcesQuery{OrgId: c.OrgId}
if err := bus.Dispatch(&query); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to query datasources", err)
}
result := make(dtos.DataSourceList, 0)
for _, ds := range query.Result {
dsItem := dtos.DataSourceListItemDTO{
Id: ds.Id,
OrgId: ds.OrgId,
Name: ds.Name,
Url: ds.Url,
Type: ds.Type,
Access: ds.Access,
Password: ds.Password,
Database: ds.Database,
User: ds.User,
BasicAuth: ds.BasicAuth,
2015-01-09 09:36:23 -06:00
IsDefault: ds.IsDefault,
JsonData: ds.JsonData,
ReadOnly: ds.ReadOnly,
}
if plugin, exists := plugins.DataSources[ds.Type]; exists {
dsItem.TypeLogoUrl = plugin.Info.Logos.Small
} else {
2016-03-23 09:09:48 -05:00
dsItem.TypeLogoUrl = "public/img/icn-datasource.svg"
}
result = append(result, dsItem)
}
sort.Sort(result)
2018-03-22 16:13:46 -05:00
return JSON(200, &result)
}
2018-03-22 06:37:35 -05:00
func GetDataSourceByID(c *m.ReqContext) Response {
query := m.GetDataSourceByIdQuery{
Id: c.ParamsInt64(":id"),
OrgId: c.OrgId,
}
if err := bus.Dispatch(&query); err != nil {
if err == m.ErrDataSourceNotFound {
2018-03-22 16:13:46 -05:00
return Error(404, "Data source not found", nil)
}
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to query datasources", err)
}
ds := query.Result
2016-03-08 04:51:03 -06:00
dtos := convertModelToDtos(ds)
2018-03-22 16:13:46 -05:00
return JSON(200, &dtos)
}
2018-03-22 06:37:35 -05:00
func DeleteDataSourceByID(c *m.ReqContext) Response {
id := c.ParamsInt64(":id")
if id <= 0 {
2018-03-22 16:13:46 -05:00
return Error(400, "Missing valid datasource id", nil)
}
2018-03-22 06:37:35 -05:00
ds, err := getRawDataSourceByID(id, c.OrgId)
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(400, "Failed to delete datasource", nil)
}
if ds.ReadOnly {
2018-03-22 16:13:46 -05:00
return Error(403, "Cannot delete read-only data source", nil)
}
cmd := &m.DeleteDataSourceByIdCommand{Id: id, OrgId: c.OrgId}
err = bus.Dispatch(cmd)
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to delete datasource", err)
}
2018-03-22 16:13:46 -05:00
return Success("Data source deleted")
}
2018-03-07 10:54:50 -06:00
func DeleteDataSourceByName(c *m.ReqContext) Response {
name := c.Params(":name")
if name == "" {
2018-03-22 16:13:46 -05:00
return Error(400, "Missing valid datasource name", nil)
}
getCmd := &m.GetDataSourceByNameQuery{Name: name, OrgId: c.OrgId}
if err := bus.Dispatch(getCmd); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to delete datasource", err)
}
if getCmd.Result.ReadOnly {
2018-03-22 16:13:46 -05:00
return Error(403, "Cannot delete read-only data source", nil)
}
cmd := &m.DeleteDataSourceByNameCommand{Name: name, OrgId: c.OrgId}
err := bus.Dispatch(cmd)
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to delete datasource", err)
}
2018-03-22 16:13:46 -05:00
return Success("Data source deleted")
}
2018-03-07 10:54:50 -06:00
func AddDataSource(c *m.ReqContext, cmd m.AddDataSourceCommand) Response {
cmd.OrgId = c.OrgId
if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrDataSourceNameExists {
2018-03-22 16:13:46 -05:00
return Error(409, err.Error(), err)
}
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to add datasource", err)
}
ds := convertModelToDtos(cmd.Result)
2018-03-22 16:13:46 -05:00
return JSON(200, util.DynMap{
"message": "Datasource added",
"id": cmd.Result.Id,
"name": cmd.Result.Name,
"datasource": ds,
})
}
2018-03-07 10:54:50 -06:00
func UpdateDataSource(c *m.ReqContext, cmd m.UpdateDataSourceCommand) Response {
cmd.OrgId = c.OrgId
cmd.Id = c.ParamsInt64(":id")
2018-03-22 06:37:35 -05:00
err := fillWithSecureJSONData(&cmd)
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to update datasource", err)
}
err = bus.Dispatch(&cmd)
if err != nil {
2017-10-25 04:29:19 -05:00
if err == m.ErrDataSourceUpdatingOldVersion {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to update datasource. Reload new version and try again", err)
}
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to update datasource", err)
}
ds := convertModelToDtos(cmd.Result)
2018-03-22 16:13:46 -05:00
return JSON(200, util.DynMap{
"message": "Datasource updated",
"id": cmd.Id,
"name": cmd.Name,
"datasource": ds,
})
}
2018-03-22 06:37:35 -05:00
func fillWithSecureJSONData(cmd *m.UpdateDataSourceCommand) error {
if len(cmd.SecureJsonData) == 0 {
return nil
}
2018-03-22 06:37:35 -05:00
ds, err := getRawDataSourceByID(cmd.Id, cmd.OrgId)
if err != nil {
return err
}
if ds.ReadOnly {
return m.ErrDatasourceIsReadOnly
}
2018-03-22 06:37:35 -05:00
secureJSONData := ds.SecureJsonData.Decrypt()
for k, v := range secureJSONData {
if _, ok := cmd.SecureJsonData[k]; !ok {
cmd.SecureJsonData[k] = v
}
}
return nil
}
2018-03-22 06:37:35 -05:00
func getRawDataSourceByID(id int64, orgID int64) (*m.DataSource, error) {
query := m.GetDataSourceByIdQuery{
Id: id,
2018-03-22 06:37:35 -05:00
OrgId: orgID,
}
if err := bus.Dispatch(&query); err != nil {
return nil, err
}
return query.Result, nil
}
2016-03-07 14:25:26 -06:00
// Get /api/datasources/name/:name
2018-03-07 10:54:50 -06:00
func GetDataSourceByName(c *m.ReqContext) Response {
2016-03-07 14:25:26 -06:00
query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
if err := bus.Dispatch(&query); err != nil {
if err == m.ErrDataSourceNotFound {
2018-03-22 16:13:46 -05:00
return Error(404, "Data source not found", nil)
2016-03-07 14:25:26 -06:00
}
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to query datasources", err)
2016-03-07 14:25:26 -06:00
}
dtos := convertModelToDtos(query.Result)
dtos.ReadOnly = true
2018-03-22 16:13:46 -05:00
return JSON(200, &dtos)
2016-03-08 04:51:03 -06:00
}
2016-03-07 14:25:26 -06:00
2016-03-10 03:31:10 -06:00
// Get /api/datasources/id/:name
2018-03-22 06:37:35 -05:00
func GetDataSourceIDByName(c *m.ReqContext) Response {
2016-03-10 03:31:10 -06:00
query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
if err := bus.Dispatch(&query); err != nil {
if err == m.ErrDataSourceNotFound {
2018-03-22 16:13:46 -05:00
return Error(404, "Data source not found", nil)
2016-03-10 03:31:10 -06:00
}
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to query datasources", err)
2016-03-10 03:31:10 -06:00
}
ds := query.Result
dtos := dtos.AnyId{
Id: ds.Id,
}
2018-03-22 16:13:46 -05:00
return JSON(200, &dtos)
2016-03-10 03:31:10 -06:00
}
func convertModelToDtos(ds *m.DataSource) dtos.DataSource {
dto := dtos.DataSource{
2016-03-07 14:25:26 -06:00
Id: ds.Id,
OrgId: ds.OrgId,
Name: ds.Name,
Url: ds.Url,
Type: ds.Type,
Access: ds.Access,
Password: ds.Password,
Database: ds.Database,
User: ds.User,
BasicAuth: ds.BasicAuth,
BasicAuthUser: ds.BasicAuthUser,
BasicAuthPassword: ds.BasicAuthPassword,
WithCredentials: ds.WithCredentials,
IsDefault: ds.IsDefault,
JsonData: ds.JsonData,
SecureJsonFields: map[string]bool{},
Version: ds.Version,
ReadOnly: ds.ReadOnly,
}
for k, v := range ds.SecureJsonData {
if len(v) > 0 {
dto.SecureJsonFields[k] = true
}
}
return dto
2016-03-07 14:25:26 -06:00
}