2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-12-17 09:53:58 -06:00
|
|
|
//"github.com/grafana/grafana/pkg/log"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-02-28 01:25:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2015-06-01 05:15:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetDataSources(c *middleware.Context) {
|
2015-02-23 13:07:49 -06:00
|
|
|
query := m.GetDataSourcesQuery{OrgId: c.OrgId}
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2015-02-14 03:04:27 -06:00
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2014-12-29 06:36:08 -06:00
|
|
|
c.JsonApiErr(500, "Failed to query datasources", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*dtos.DataSource, len(query.Result))
|
|
|
|
for i, ds := range query.Result {
|
|
|
|
result[i] = &dtos.DataSource{
|
|
|
|
Id: ds.Id,
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId: ds.OrgId,
|
2014-12-29 06:36:08 -06:00
|
|
|
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,
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, result)
|
|
|
|
}
|
|
|
|
|
2015-11-13 02:43:25 -06:00
|
|
|
func GetDataSourceById(c *middleware.Context) Response {
|
2015-02-14 03:04:27 -06:00
|
|
|
query := m.GetDataSourceByIdQuery{
|
2015-02-23 13:07:49 -06:00
|
|
|
Id: c.ParamsInt64(":id"),
|
|
|
|
OrgId: c.OrgId,
|
2015-02-14 03:04:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2015-11-13 02:43:25 -06:00
|
|
|
if err == m.ErrDataSourceNotFound {
|
|
|
|
return ApiError(404, "Data source not found", nil)
|
|
|
|
}
|
|
|
|
return ApiError(500, "Failed to query datasources", err)
|
2015-02-14 03:04:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ds := query.Result
|
|
|
|
|
2015-11-13 02:43:25 -06:00
|
|
|
return Json(200, &dtos.DataSource{
|
2015-03-02 02:58:35 -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,
|
2015-12-08 21:37:08 -06:00
|
|
|
WithCredentials: ds.WithCredentials,
|
2015-03-02 02:58:35 -06:00
|
|
|
IsDefault: ds.IsDefault,
|
|
|
|
JsonData: ds.JsonData,
|
2015-02-14 03:04:27 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:36:08 -06:00
|
|
|
func DeleteDataSource(c *middleware.Context) {
|
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
|
|
|
|
if id <= 0 {
|
|
|
|
c.JsonApiErr(400, "Missing valid datasource id", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
cmd := &m.DeleteDataSourceCommand{Id: id, OrgId: c.OrgId}
|
2014-12-29 06:36:08 -06:00
|
|
|
|
|
|
|
err := bus.Dispatch(cmd)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(500, "Failed to delete datasource", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JsonOK("Data source deleted")
|
|
|
|
}
|
|
|
|
|
2015-03-02 02:58:35 -06:00
|
|
|
func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) {
|
2015-02-23 13:07:49 -06:00
|
|
|
cmd.OrgId = c.OrgId
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2015-01-09 04:01:37 -06:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2014-12-29 06:36:08 -06:00
|
|
|
c.JsonApiErr(500, "Failed to add datasource", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-01 05:15:49 -05:00
|
|
|
c.JSON(200, util.DynMap{"message": "Datasource added", "id": cmd.Result.Id})
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2015-02-28 10:27:30 -06:00
|
|
|
func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) {
|
2015-02-23 13:07:49 -06:00
|
|
|
cmd.OrgId = c.OrgId
|
2015-06-01 05:15:49 -05:00
|
|
|
cmd.Id = c.ParamsInt64(":id")
|
2014-12-29 06:36:08 -06:00
|
|
|
|
|
|
|
err := bus.Dispatch(&cmd)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(500, "Failed to update datasource", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JsonOK("Datasource updated")
|
|
|
|
}
|
2015-02-28 01:25:13 -06:00
|
|
|
|
|
|
|
func GetDataSourcePlugins(c *middleware.Context) {
|
2015-12-17 09:53:58 -06:00
|
|
|
dsList := make(map[string]*plugins.DataSourcePlugin)
|
2015-12-03 09:43:55 -06:00
|
|
|
|
2016-01-10 14:37:11 -06:00
|
|
|
if enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId); err != nil {
|
2015-12-21 16:09:27 -06:00
|
|
|
c.JsonApiErr(500, "Failed to get org apps", err)
|
2016-01-10 14:37:11 -06:00
|
|
|
return
|
|
|
|
} else {
|
2015-08-16 13:52:30 -05:00
|
|
|
|
2016-01-10 14:37:11 -06:00
|
|
|
for key, value := range enabledPlugins.DataSources {
|
|
|
|
if !value.BuiltIn {
|
|
|
|
dsList[key] = value
|
|
|
|
}
|
2015-08-16 13:52:30 -05:00
|
|
|
}
|
|
|
|
|
2016-01-10 14:37:11 -06:00
|
|
|
c.JSON(200, dsList)
|
|
|
|
}
|
2015-02-28 01:25:13 -06:00
|
|
|
}
|
2016-03-07 14:25:26 -06:00
|
|
|
|
|
|
|
// Get /api/datasources/name/:name
|
|
|
|
func GetDataSourceByName(c *middleware.Context) Response {
|
|
|
|
query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
if err == m.ErrDataSourceNotFound {
|
|
|
|
return ApiError(404, "Data source not found", nil)
|
|
|
|
}
|
|
|
|
return ApiError(500, "Failed to query datasources", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ds := query.Result
|
|
|
|
|
|
|
|
return Json(200, &dtos.DataSource{
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|