2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-10-07 09:33:50 -05:00
|
|
|
"context"
|
2020-03-18 06:08:52 -05:00
|
|
|
"encoding/json"
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2020-04-22 03:30:06 -05:00
|
|
|
"fmt"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2016-03-15 14:29:35 -05:00
|
|
|
"sort"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
2016-03-15 14:29:35 -05:00
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2020-05-12 06:04:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/datasource"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2020-06-17 04:17:11 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/adapters"
|
2022-03-24 06:21:26 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
|
|
"github.com/grafana/grafana/pkg/services/datasources/permissions"
|
2015-06-01 05:15:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
2020-06-17 04:17:11 -05:00
|
|
|
var datasourcesLogger = log.New("datasources")
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) GetDataSources(c *models.ReqContext) response.Response {
|
2020-12-28 05:24:42 -06:00
|
|
|
query := models.GetDataSourcesQuery{OrgId: c.OrgId, DataSourceLimit: hs.Cfg.DataSourceLimit}
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
if err := hs.DataSourcesService.GetDataSources(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to query datasources", err)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
filtered, err := hs.filterDatasourcesByQueryPermission(c.Req.Context(), c.SignedInUser, query.Result)
|
2022-01-17 03:16:12 -06:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Failed to query datasources", err)
|
|
|
|
}
|
|
|
|
|
2016-03-15 14:29:35 -05:00
|
|
|
result := make(dtos.DataSourceList, 0)
|
2022-01-17 03:16:12 -06:00
|
|
|
for _, ds := range filtered {
|
2017-04-25 08:17:49 -05:00
|
|
|
dsItem := dtos.DataSourceListItemDTO{
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId: ds.OrgId,
|
2018-10-01 08:38:55 -05:00
|
|
|
Id: ds.Id,
|
2021-01-13 12:16:27 -06:00
|
|
|
UID: ds.Uid,
|
2014-12-29 06:36:08 -06:00
|
|
|
Name: ds.Name,
|
|
|
|
Url: ds.Url,
|
|
|
|
Type: ds.Type,
|
2021-02-22 06:02:10 -06:00
|
|
|
TypeName: ds.Type,
|
2014-12-29 06:36:08 -06:00
|
|
|
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,
|
2016-09-01 10:21:13 -05:00
|
|
|
JsonData: ds.JsonData,
|
2017-10-24 08:28:39 -05:00
|
|
|
ReadOnly: ds.ReadOnly,
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
2016-03-15 14:29:35 -05:00
|
|
|
|
2021-11-17 05:04:22 -06:00
|
|
|
if plugin, exists := hs.pluginStore.Plugin(c.Req.Context(), ds.Type); exists {
|
2016-03-15 14:29:35 -05:00
|
|
|
dsItem.TypeLogoUrl = plugin.Info.Logos.Small
|
2021-02-22 06:02:10 -06:00
|
|
|
dsItem.TypeName = plugin.Name
|
2016-03-15 14:29:35 -05:00
|
|
|
} else {
|
2016-03-23 09:09:48 -05:00
|
|
|
dsItem.TypeLogoUrl = "public/img/icn-datasource.svg"
|
2016-03-15 14:29:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, dsItem)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2016-03-15 14:29:35 -05:00
|
|
|
sort.Sort(result)
|
2017-02-08 07:20:07 -06:00
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &result)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 17:22:43 -06:00
|
|
|
// GET /api/datasources/:id
|
2021-12-15 05:08:15 -06:00
|
|
|
func (hs *HTTPServer) GetDataSourceById(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2021-01-13 12:16:27 -06:00
|
|
|
query := models.GetDataSourceQuery{
|
2022-01-14 10:55:57 -06:00
|
|
|
Id: id,
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId: c.OrgId,
|
2015-02-14 03:04:27 -06:00
|
|
|
}
|
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
if err := hs.DataSourcesService.GetDataSource(c.Req.Context(), &query); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Data source not found", nil)
|
2015-11-13 02:43:25 -06:00
|
|
|
}
|
2021-03-29 01:56:58 -05:00
|
|
|
if errors.Is(err, models.ErrDataSourceIdentifierNotSet) {
|
|
|
|
return response.Error(400, "Datasource id is missing", nil)
|
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to query datasources", err)
|
2015-02-14 03:04:27 -06:00
|
|
|
}
|
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
filtered, err := hs.filterDatasourcesByQueryPermission(c.Req.Context(), c.SignedInUser, []*models.DataSource{query.Result})
|
2022-01-17 03:16:12 -06:00
|
|
|
if err != nil || len(filtered) != 1 {
|
|
|
|
return response.Error(404, "Data source not found", err)
|
|
|
|
}
|
2015-02-14 03:04:27 -06:00
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
dto := hs.convertModelToDtos(c.Req.Context(), filtered[0])
|
2022-02-18 04:27:00 -06:00
|
|
|
|
2021-12-15 05:08:15 -06:00
|
|
|
// Add accesscontrol metadata
|
2022-03-24 06:21:26 -05:00
|
|
|
dto.AccessControl = hs.getAccessControlMetadata(c, c.OrgId, datasources.ScopePrefix, dto.UID)
|
2021-12-15 05:08:15 -06:00
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &dto)
|
2015-02-14 03:04:27 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 17:22:43 -06:00
|
|
|
// DELETE /api/datasources/:id
|
2021-05-18 13:39:56 -05:00
|
|
|
func (hs *HTTPServer) DeleteDataSourceById(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2014-12-29 06:36:08 -06:00
|
|
|
|
|
|
|
if id <= 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Missing valid datasource id", nil)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
ds, err := hs.getRawDataSourceById(c.Req.Context(), id, c.OrgId)
|
2017-10-24 08:28:39 -05:00
|
|
|
if err != nil {
|
2021-01-13 12:16:27 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Data source not found", nil)
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Failed to delete datasource", nil)
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if ds.ReadOnly {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Cannot delete read-only data source", nil)
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
cmd := &models.DeleteDataSourceCommand{ID: id, OrgID: c.OrgId, Name: ds.Name}
|
2021-01-13 12:16:27 -06:00
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
err = hs.DataSourcesService.DeleteDataSource(c.Req.Context(), cmd)
|
2021-01-13 12:16:27 -06:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete datasource", err)
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
|
|
|
|
2021-05-18 13:39:56 -05:00
|
|
|
hs.Live.HandleDatasourceDelete(c.OrgId, ds.Uid)
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Data source deleted")
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/datasources/uid/:uid
|
2021-12-15 05:08:15 -06:00
|
|
|
func (hs *HTTPServer) GetDataSourceByUID(c *models.ReqContext) response.Response {
|
2022-02-09 07:01:32 -06:00
|
|
|
ds, err := hs.getRawDataSourceByUID(c.Req.Context(), web.Params(c.Req)[":uid"], c.OrgId)
|
2021-01-13 12:16:27 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
2021-12-15 05:08:15 -06:00
|
|
|
return response.Error(http.StatusNotFound, "Data source not found", nil)
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
2021-12-15 05:08:15 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to query datasource", err)
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
filtered, err := hs.filterDatasourcesByQueryPermission(c.Req.Context(), c.SignedInUser, []*models.DataSource{ds})
|
2022-01-17 03:16:12 -06:00
|
|
|
if err != nil || len(filtered) != 1 {
|
|
|
|
return response.Error(404, "Data source not found", err)
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
dto := hs.convertModelToDtos(c.Req.Context(), filtered[0])
|
2021-12-15 05:08:15 -06:00
|
|
|
|
|
|
|
// Add accesscontrol metadata
|
2022-03-24 06:21:26 -05:00
|
|
|
dto.AccessControl = hs.getAccessControlMetadata(c, c.OrgId, datasources.ScopePrefix, dto.UID)
|
2022-03-24 02:58:10 -05:00
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &dto)
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/datasources/uid/:uid
|
2021-05-18 13:39:56 -05:00
|
|
|
func (hs *HTTPServer) DeleteDataSourceByUID(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
uid := web.Params(c.Req)[":uid"]
|
2021-01-13 12:16:27 -06:00
|
|
|
|
|
|
|
if uid == "" {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Missing datasource uid", nil)
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
ds, err := hs.getRawDataSourceByUID(c.Req.Context(), uid, c.OrgId)
|
2021-01-13 12:16:27 -06:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Data source not found", nil)
|
2021-01-13 12:16:27 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Failed to delete datasource", nil)
|
2017-10-24 08:28:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ds.ReadOnly {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Cannot delete read-only data source", nil)
|
2017-10-24 08:28:39 -05:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
cmd := &models.DeleteDataSourceCommand{UID: uid, OrgID: c.OrgId, Name: ds.Name}
|
2017-02-09 20:11:36 -06:00
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
err = hs.DataSourcesService.DeleteDataSource(c.Req.Context(), cmd)
|
2017-02-09 20:11:36 -06:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete datasource", err)
|
2017-02-09 20:11:36 -06:00
|
|
|
}
|
|
|
|
|
2021-05-18 13:39:56 -05:00
|
|
|
hs.Live.HandleDatasourceDelete(c.OrgId, ds.Uid)
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{
|
2021-11-05 09:06:14 -05:00
|
|
|
"message": "Data source deleted",
|
|
|
|
"id": ds.Id,
|
|
|
|
})
|
2017-02-09 20:11:36 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 17:22:43 -06:00
|
|
|
// DELETE /api/datasources/name/:name
|
2021-05-18 13:39:56 -05:00
|
|
|
func (hs *HTTPServer) DeleteDataSourceByName(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
name := web.Params(c.Req)[":name"]
|
2017-02-09 20:11:36 -06:00
|
|
|
|
|
|
|
if name == "" {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Missing valid datasource name", nil)
|
2017-02-09 20:11:36 -06:00
|
|
|
}
|
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
getCmd := &models.GetDataSourceQuery{Name: name, OrgId: c.OrgId}
|
2022-02-11 08:52:14 -06:00
|
|
|
if err := hs.DataSourcesService.GetDataSource(c.Req.Context(), getCmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Data source not found", nil)
|
2018-06-22 21:15:36 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete datasource", err)
|
2017-10-24 08:28:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if getCmd.Result.ReadOnly {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Cannot delete read-only data source", nil)
|
2017-10-24 08:28:39 -05:00
|
|
|
}
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
cmd := &models.DeleteDataSourceCommand{Name: name, OrgID: c.OrgId}
|
2022-02-11 08:52:14 -06:00
|
|
|
err := hs.DataSourcesService.DeleteDataSource(c.Req.Context(), cmd)
|
2014-12-29 06:36:08 -06:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete datasource", err)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2021-05-18 13:39:56 -05:00
|
|
|
hs.Live.HandleDatasourceDelete(c.OrgId, getCmd.Result.Uid)
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{
|
2020-07-31 01:22:09 -05:00
|
|
|
"message": "Data source deleted",
|
|
|
|
"id": getCmd.Result.Id,
|
|
|
|
})
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2022-01-31 09:39:55 -06:00
|
|
|
func validateURL(cmdType string, url string) response.Response {
|
|
|
|
if _, err := datasource.ValidateURL(cmdType, url); err != nil {
|
|
|
|
return response.Error(400, fmt.Sprintf("Validation error, invalid URL: %q", url), err)
|
2020-04-22 03:30:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:22:43 -06:00
|
|
|
// POST /api/datasources/
|
2022-02-09 07:01:32 -06:00
|
|
|
func (hs *HTTPServer) AddDataSource(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
cmd := models.AddDataSourceCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-31 09:39:55 -06:00
|
|
|
|
2020-06-17 04:17:11 -05:00
|
|
|
datasourcesLogger.Debug("Received command to add data source", "url", cmd.Url)
|
2015-02-23 13:07:49 -06:00
|
|
|
cmd.OrgId = c.OrgId
|
2022-03-21 11:16:05 -05:00
|
|
|
cmd.UserId = c.UserId
|
2022-01-31 09:39:55 -06:00
|
|
|
if cmd.Url != "" {
|
|
|
|
if resp := validateURL(cmd.Type, cmd.Url); resp != nil {
|
|
|
|
return resp
|
|
|
|
}
|
2020-04-22 03:30:06 -05:00
|
|
|
}
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
if err := hs.DataSourcesService.AddDataSource(c.Req.Context(), &cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceNameExists) || errors.Is(err, models.ErrDataSourceUidExists) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(409, err.Error(), err)
|
2016-10-01 09:41:27 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to add datasource", err)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
ds := hs.convertModelToDtos(c.Req.Context(), cmd.Result)
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{
|
2017-10-19 10:28:54 -05:00
|
|
|
"message": "Datasource added",
|
|
|
|
"id": cmd.Result.Id,
|
|
|
|
"name": cmd.Result.Name,
|
|
|
|
"datasource": ds,
|
|
|
|
})
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 17:22:43 -06:00
|
|
|
// PUT /api/datasources/:id
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) UpdateDataSource(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.UpdateDataSourceCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2020-06-17 04:17:11 -05:00
|
|
|
datasourcesLogger.Debug("Received command to update data source", "url", cmd.Url)
|
2015-02-23 13:07:49 -06:00
|
|
|
cmd.OrgId = c.OrgId
|
2022-01-14 10:55:57 -06:00
|
|
|
var err error
|
2022-01-31 09:39:55 -06:00
|
|
|
if cmd.Id, err = strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64); err != nil {
|
2022-01-14 10:55:57 -06:00
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2020-06-17 04:17:11 -05:00
|
|
|
if resp := validateURL(cmd.Type, cmd.Url); resp != nil {
|
2020-04-22 03:30:06 -05:00
|
|
|
return resp
|
|
|
|
}
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
ds, err := hs.getRawDataSourceById(c.Req.Context(), cmd.Id, cmd.OrgId)
|
2022-01-21 17:22:43 -06:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
|
|
|
return response.Error(404, "Data source not found", nil)
|
|
|
|
}
|
|
|
|
return response.Error(500, "Failed to update datasource", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ds.ReadOnly {
|
|
|
|
return response.Error(403, "Cannot update read-only data source", nil)
|
|
|
|
}
|
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
err = hs.DataSourcesService.UpdateDataSource(c.Req.Context(), &cmd)
|
2016-11-18 09:44:59 -06:00
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceUpdatingOldVersion) {
|
2021-03-29 09:03:15 -05:00
|
|
|
return response.Error(409, "Datasource has already been updated by someone else. Please reload and try again", err)
|
2017-10-19 10:28:54 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update datasource", err)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
2018-08-07 10:56:02 -05:00
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
query := models.GetDataSourceQuery{
|
2018-08-07 10:56:02 -05:00
|
|
|
Id: cmd.Id,
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
}
|
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
if err := hs.DataSourcesService.GetDataSource(c.Req.Context(), &query); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Data source not found", nil)
|
2018-08-07 10:56:02 -05:00
|
|
|
}
|
2021-05-18 13:39:56 -05:00
|
|
|
return response.Error(500, "Failed to query datasource", err)
|
2018-08-07 10:56:02 -05:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
datasourceDTO := hs.convertModelToDtos(c.Req.Context(), query.Result)
|
2021-05-18 13:39:56 -05:00
|
|
|
|
|
|
|
hs.Live.HandleDatasourceUpdate(c.OrgId, datasourceDTO.UID)
|
2018-08-07 10:56:02 -05:00
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{
|
2017-10-19 10:28:54 -05:00
|
|
|
"message": "Datasource updated",
|
|
|
|
"id": cmd.Id,
|
|
|
|
"name": cmd.Name,
|
2021-05-18 13:39:56 -05:00
|
|
|
"datasource": datasourceDTO,
|
2017-10-19 10:28:54 -05:00
|
|
|
})
|
2016-11-18 09:44:59 -06:00
|
|
|
}
|
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
func (hs *HTTPServer) getRawDataSourceById(ctx context.Context, id int64, orgID int64) (*models.DataSource, error) {
|
2021-01-13 12:16:27 -06:00
|
|
|
query := models.GetDataSourceQuery{
|
2016-11-18 09:44:59 -06:00
|
|
|
Id: id,
|
2018-03-22 06:37:35 -05:00
|
|
|
OrgId: orgID,
|
2016-11-18 09:44:59 -06:00
|
|
|
}
|
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
if err := hs.DataSourcesService.GetDataSource(ctx, &query); err != nil {
|
2016-11-18 09:44:59 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.Result, nil
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
2015-02-28 01:25:13 -06:00
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
func (hs *HTTPServer) getRawDataSourceByUID(ctx context.Context, uid string, orgID int64) (*models.DataSource, error) {
|
2021-01-13 12:16:27 -06:00
|
|
|
query := models.GetDataSourceQuery{
|
|
|
|
Uid: uid,
|
|
|
|
OrgId: orgID,
|
|
|
|
}
|
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
if err := hs.DataSourcesService.GetDataSource(ctx, &query); err != nil {
|
2021-01-13 12:16:27 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.Result, nil
|
|
|
|
}
|
|
|
|
|
2016-03-07 14:25:26 -06:00
|
|
|
// Get /api/datasources/name/:name
|
2022-02-09 07:01:32 -06:00
|
|
|
func (hs *HTTPServer) GetDataSourceByName(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
query := models.GetDataSourceQuery{Name: web.Params(c.Req)[":name"], OrgId: c.OrgId}
|
2016-03-07 14:25:26 -06:00
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
if err := hs.DataSourcesService.GetDataSource(c.Req.Context(), &query); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Data source not found", nil)
|
2016-03-07 14:25:26 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to query datasources", err)
|
2016-03-07 14:25:26 -06:00
|
|
|
}
|
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
filtered, err := hs.filterDatasourcesByQueryPermission(c.Req.Context(), c.SignedInUser, []*models.DataSource{query.Result})
|
2022-01-17 03:16:12 -06:00
|
|
|
if err != nil || len(filtered) != 1 {
|
|
|
|
return response.Error(404, "Data source not found", err)
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
dto := hs.convertModelToDtos(c.Req.Context(), filtered[0])
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &dto)
|
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
|
2022-02-09 07:01:32 -06:00
|
|
|
func (hs *HTTPServer) GetDataSourceIdByName(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
query := models.GetDataSourceQuery{Name: web.Params(c.Req)[":name"], OrgId: c.OrgId}
|
2016-03-10 03:31:10 -06:00
|
|
|
|
2022-02-11 08:52:14 -06:00
|
|
|
if err := hs.DataSourcesService.GetDataSource(c.Req.Context(), &query); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Data source not found", nil)
|
2016-03-10 03:31:10 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to query datasources", err)
|
2016-03-10 03:31:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ds := query.Result
|
|
|
|
dtos := dtos.AnyId{
|
|
|
|
Id: ds.Id,
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &dtos)
|
2016-03-10 03:31:10 -06:00
|
|
|
}
|
|
|
|
|
2020-01-31 04:15:50 -06:00
|
|
|
// /api/datasources/:id/resources/*
|
2020-03-04 05:57:20 -06:00
|
|
|
func (hs *HTTPServer) CallDatasourceResource(c *models.ReqContext) {
|
2022-01-14 10:55:57 -06:00
|
|
|
datasourceID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
return
|
|
|
|
}
|
2021-12-22 04:02:42 -06:00
|
|
|
ds, err := hs.DataSourceCache.GetDatasource(c.Req.Context(), datasourceID, c.SignedInUser, c.SkipCache)
|
2020-01-31 04:15:50 -06:00
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
2020-03-03 04:45:16 -06:00
|
|
|
c.JsonApiErr(403, "Access denied to datasource", err)
|
|
|
|
return
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
2020-03-03 04:45:16 -06:00
|
|
|
c.JsonApiErr(500, "Unable to load datasource meta data", err)
|
|
|
|
return
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
2021-11-17 05:04:22 -06:00
|
|
|
plugin, exists := hs.pluginStore.Plugin(c.Req.Context(), ds.Type)
|
|
|
|
if !exists {
|
2020-03-03 04:45:16 -06:00
|
|
|
c.JsonApiErr(500, "Unable to find datasource plugin", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-06 03:58:02 -05:00
|
|
|
hs.callPluginResourceWithDataSource(c, plugin.ID, ds)
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
func (hs *HTTPServer) convertModelToDtos(ctx context.Context, ds *models.DataSource) dtos.DataSource {
|
2016-11-18 09:44:59 -06:00
|
|
|
dto := dtos.DataSource{
|
2016-03-07 14:25:26 -06:00
|
|
|
Id: ds.Id,
|
2021-01-13 12:16:27 -06:00
|
|
|
UID: ds.Uid,
|
2016-03-07 14:25:26 -06:00
|
|
|
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,
|
2016-12-14 14:05:12 -06:00
|
|
|
SecureJsonFields: map[string]bool{},
|
2017-10-19 10:28:54 -05:00
|
|
|
Version: ds.Version,
|
2017-10-24 08:28:39 -05:00
|
|
|
ReadOnly: ds.ReadOnly,
|
2016-11-18 09:44:59 -06:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
secrets, err := hs.DataSourcesService.DecryptedValues(ctx, ds)
|
|
|
|
if err == nil {
|
|
|
|
for k, v := range secrets {
|
|
|
|
if len(v) > 0 {
|
|
|
|
dto.SecureJsonFields[k] = true
|
|
|
|
}
|
2016-11-24 09:24:47 -06:00
|
|
|
}
|
2022-04-25 13:12:44 -05:00
|
|
|
} else {
|
|
|
|
datasourcesLogger.Debug("Failed to retrieve datasource secrets to parse secure json fields", "error", err)
|
2016-11-24 09:24:47 -06:00
|
|
|
}
|
|
|
|
|
2016-11-18 09:44:59 -06:00
|
|
|
return dto
|
2016-03-07 14:25:26 -06:00
|
|
|
}
|
2020-03-13 06:31:44 -05:00
|
|
|
|
|
|
|
// CheckDatasourceHealth sends a health check request to the plugin datasource
|
|
|
|
// /api/datasource/:id/health
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) CheckDatasourceHealth(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
datasourceID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2020-03-13 06:31:44 -05:00
|
|
|
|
2021-12-22 04:02:42 -06:00
|
|
|
ds, err := hs.DataSourceCache.GetDatasource(c.Req.Context(), datasourceID, c.SignedInUser, c.SkipCache)
|
2020-03-13 06:31:44 -05:00
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
2022-01-27 11:06:38 -06:00
|
|
|
return response.Error(http.StatusForbidden, "Access denied to datasource", err)
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
2022-01-27 11:06:38 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Unable to load datasource metadata", err)
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
|
|
|
|
2021-11-17 05:04:22 -06:00
|
|
|
plugin, exists := hs.pluginStore.Plugin(c.Req.Context(), ds.Type)
|
|
|
|
if !exists {
|
2022-01-27 11:06:38 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Unable to find datasource plugin", err)
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
dsInstanceSettings, err := adapters.ModelToInstanceSettings(ds, hs.decryptSecureJsonDataFn(c.Req.Context()))
|
2020-04-23 13:08:21 -05:00
|
|
|
if err != nil {
|
2022-01-27 11:06:38 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Unable to get datasource model", err)
|
2020-04-23 13:08:21 -05:00
|
|
|
}
|
2021-11-01 04:53:33 -05:00
|
|
|
req := &backend.CheckHealthRequest{
|
|
|
|
PluginContext: backend.PluginContext{
|
|
|
|
User: adapters.BackendUserFromSignedInUser(c.SignedInUser),
|
|
|
|
OrgID: c.OrgId,
|
|
|
|
PluginID: plugin.ID,
|
|
|
|
DataSourceInstanceSettings: dsInstanceSettings,
|
|
|
|
},
|
2020-04-23 13:08:21 -05:00
|
|
|
}
|
|
|
|
|
2022-01-27 11:06:38 -06:00
|
|
|
var dsURL string
|
|
|
|
if req.PluginContext.DataSourceInstanceSettings != nil {
|
|
|
|
dsURL = req.PluginContext.DataSourceInstanceSettings.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
err = hs.PluginRequestValidator.Validate(dsURL, c.Req)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusForbidden, "Access denied", err)
|
|
|
|
}
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
resp, err := hs.pluginClient.CheckHealth(c.Req.Context(), req)
|
2020-03-13 06:31:44 -05:00
|
|
|
if err != nil {
|
2020-06-11 09:14:05 -05:00
|
|
|
return translatePluginRequestErrorToAPIError(err)
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
payload := map[string]interface{}{
|
2020-03-18 06:08:52 -05:00
|
|
|
"status": resp.Status.String(),
|
|
|
|
"message": resp.Message,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal JSONDetails if it's not empty.
|
|
|
|
if len(resp.JSONDetails) > 0 {
|
2020-04-21 09:16:41 -05:00
|
|
|
var jsonDetails map[string]interface{}
|
2020-03-18 06:08:52 -05:00
|
|
|
err = json.Unmarshal(resp.JSONDetails, &jsonDetails)
|
|
|
|
if err != nil {
|
2022-01-27 11:06:38 -06:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to unmarshal detailed response from backend plugin", err)
|
2020-03-18 06:08:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
payload["details"] = jsonDetails
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:14:05 -05:00
|
|
|
if resp.Status != backend.HealthStatusOk {
|
2022-01-27 11:06:38 -06:00
|
|
|
return response.JSON(http.StatusBadRequest, payload)
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
|
|
|
|
2022-01-27 11:06:38 -06:00
|
|
|
return response.JSON(http.StatusOK, payload)
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
2021-10-07 09:33:50 -05:00
|
|
|
|
2022-04-25 11:57:45 -05:00
|
|
|
func (hs *HTTPServer) decryptSecureJsonDataFn(ctx context.Context) func(ds *models.DataSource) map[string]string {
|
|
|
|
return func(ds *models.DataSource) map[string]string {
|
|
|
|
decryptedJsonData, err := hs.DataSourcesService.DecryptedValues(ctx, ds)
|
2021-10-07 09:33:50 -05:00
|
|
|
if err != nil {
|
|
|
|
hs.log.Error("Failed to decrypt secure json data", "error", err)
|
|
|
|
}
|
|
|
|
return decryptedJsonData
|
|
|
|
}
|
|
|
|
}
|
2022-01-17 03:16:12 -06:00
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
func (hs *HTTPServer) filterDatasourcesByQueryPermission(ctx context.Context, user *models.SignedInUser, datasources []*models.DataSource) ([]*models.DataSource, error) {
|
2022-01-17 03:16:12 -06:00
|
|
|
query := models.DatasourcesPermissionFilterQuery{
|
|
|
|
User: user,
|
|
|
|
Datasources: datasources,
|
|
|
|
}
|
2022-02-09 07:01:32 -06:00
|
|
|
query.Result = datasources
|
2022-01-17 03:16:12 -06:00
|
|
|
|
2022-02-09 07:01:32 -06:00
|
|
|
if err := hs.DatasourcePermissionsService.FilterDatasourcesBasedOnQueryPermissions(ctx, &query); err != nil {
|
2022-03-02 04:04:29 -06:00
|
|
|
if !errors.Is(err, permissions.ErrNotImplemented) {
|
2022-01-17 03:16:12 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return datasources, nil
|
|
|
|
}
|
|
|
|
|
2022-02-01 08:00:05 -06:00
|
|
|
return query.Result, nil
|
2022-01-17 03:16:12 -06:00
|
|
|
}
|