mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 12:53:12 +08:00

Rename externalPlugin to apiPlugin Rename bundle to app Move js, css, menuItem and staticRoot to be properties os App Add "app" field to panel, datasource and api plugin models. If populated then the plugin is only enabled if the specific app is enabled for the Org. If app is "", then the plugin is enabled for all orgs and can't be disabled.
135 lines
3.1 KiB
Go
135 lines
3.1 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
//"github.com/grafana/grafana/pkg/log"
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
)
|
|
|
|
func GetDataSources(c *middleware.Context) {
|
|
query := m.GetDataSourcesQuery{OrgId: c.OrgId}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
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,
|
|
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,
|
|
IsDefault: ds.IsDefault,
|
|
}
|
|
}
|
|
|
|
c.JSON(200, result)
|
|
}
|
|
|
|
func GetDataSourceById(c *middleware.Context) Response {
|
|
query := m.GetDataSourceByIdQuery{
|
|
Id: c.ParamsInt64(":id"),
|
|
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,
|
|
IsDefault: ds.IsDefault,
|
|
JsonData: ds.JsonData,
|
|
})
|
|
}
|
|
|
|
func DeleteDataSource(c *middleware.Context) {
|
|
id := c.ParamsInt64(":id")
|
|
|
|
if id <= 0 {
|
|
c.JsonApiErr(400, "Missing valid datasource id", nil)
|
|
return
|
|
}
|
|
|
|
cmd := &m.DeleteDataSourceCommand{Id: id, OrgId: c.OrgId}
|
|
|
|
err := bus.Dispatch(cmd)
|
|
if err != nil {
|
|
c.JsonApiErr(500, "Failed to delete datasource", err)
|
|
return
|
|
}
|
|
|
|
c.JsonOK("Data source deleted")
|
|
}
|
|
|
|
func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) {
|
|
cmd.OrgId = c.OrgId
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
c.JsonApiErr(500, "Failed to add datasource", err)
|
|
return
|
|
}
|
|
|
|
c.JSON(200, util.DynMap{"message": "Datasource added", "id": cmd.Result.Id})
|
|
}
|
|
|
|
func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) {
|
|
cmd.OrgId = c.OrgId
|
|
cmd.Id = c.ParamsInt64(":id")
|
|
|
|
err := bus.Dispatch(&cmd)
|
|
if err != nil {
|
|
c.JsonApiErr(500, "Failed to update datasource", err)
|
|
return
|
|
}
|
|
|
|
c.JsonOK("Datasource updated")
|
|
}
|
|
|
|
func GetDataSourcePlugins(c *middleware.Context) {
|
|
dsList := make(map[string]*plugins.DataSourcePlugin)
|
|
|
|
orgApps := m.GetAppPluginsQuery{OrgId: c.OrgId}
|
|
err := bus.Dispatch(&orgApps)
|
|
if err != nil {
|
|
c.JsonApiErr(500, "Failed to get org plugin Bundles", err)
|
|
}
|
|
enabledPlugins := plugins.GetEnabledPlugins(orgApps.Result)
|
|
|
|
for key, value := range enabledPlugins.DataSourcePlugins {
|
|
if !value.BuiltIn {
|
|
dsList[key] = value
|
|
}
|
|
}
|
|
|
|
c.JSON(200, dsList)
|
|
}
|