2016-02-25 07:55:31 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-03-15 09:52:29 -05:00
|
|
|
"sort"
|
|
|
|
|
2016-02-25 07:55:31 -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"
|
2016-05-03 12:00:42 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-02-25 07:55:31 -06:00
|
|
|
)
|
|
|
|
|
2018-11-15 04:10:47 -06:00
|
|
|
func (hs *HTTPServer) GetPluginList(c *m.ReqContext) Response {
|
2016-03-08 11:17:47 -06:00
|
|
|
typeFilter := c.Query("type")
|
|
|
|
enabledFilter := c.Query("enabled")
|
|
|
|
embeddedFilter := c.Query("embedded")
|
2016-04-08 15:42:33 -05:00
|
|
|
coreFilter := c.Query("core")
|
2016-03-08 11:17:47 -06:00
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
pluginSettingsMap, err := plugins.GetPluginSettings(c.OrgId)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to get list of plugins", err)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2016-03-15 09:52:29 -05:00
|
|
|
result := make(dtos.PluginList, 0)
|
2016-02-25 07:55:31 -06:00
|
|
|
for _, pluginDef := range plugins.Plugins {
|
2016-03-08 11:17:47 -06:00
|
|
|
// filter out app sub plugins
|
|
|
|
if embeddedFilter == "0" && pluginDef.IncludedInAppId != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-04-08 15:42:33 -05:00
|
|
|
// filter out core plugins
|
|
|
|
if coreFilter == "0" && pluginDef.IsCorePlugin {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-08 11:17:47 -06:00
|
|
|
// filter on type
|
|
|
|
if typeFilter != "" && typeFilter != pluginDef.Type {
|
2016-03-08 04:29:36 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-15 04:10:47 -06:00
|
|
|
if pluginDef.State == plugins.PluginStateAlpha && !hs.Cfg.EnableAlphaPanels {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-15 09:52:29 -05:00
|
|
|
listItem := dtos.PluginListItem{
|
2016-04-11 11:47:04 -05:00
|
|
|
Id: pluginDef.Id,
|
|
|
|
Name: pluginDef.Name,
|
|
|
|
Type: pluginDef.Type,
|
|
|
|
Info: &pluginDef.Info,
|
|
|
|
LatestVersion: pluginDef.GrafanaNetVersion,
|
|
|
|
HasUpdate: pluginDef.GrafanaNetHasUpdate,
|
2016-05-03 12:00:42 -05:00
|
|
|
DefaultNavUrl: pluginDef.DefaultNavUrl,
|
2017-04-07 05:00:03 -05:00
|
|
|
State: pluginDef.State,
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if pluginSetting, exists := pluginSettingsMap[pluginDef.Id]; exists {
|
|
|
|
listItem.Enabled = pluginSetting.Enabled
|
|
|
|
listItem.Pinned = pluginSetting.Pinned
|
|
|
|
}
|
|
|
|
|
2016-05-03 12:00:42 -05:00
|
|
|
if listItem.DefaultNavUrl == "" || !listItem.Enabled {
|
|
|
|
listItem.DefaultNavUrl = setting.AppSubUrl + "/plugins/" + listItem.Id + "/edit"
|
|
|
|
}
|
|
|
|
|
2016-03-08 11:17:47 -06:00
|
|
|
// filter out disabled
|
|
|
|
if enabledFilter == "1" && !listItem.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-14 06:00:56 -05:00
|
|
|
// filter out built in data sources
|
|
|
|
if ds, exists := plugins.DataSources[pluginDef.Id]; exists {
|
|
|
|
if ds.BuiltIn {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
result = append(result, listItem)
|
|
|
|
}
|
|
|
|
|
2016-03-15 09:52:29 -05:00
|
|
|
sort.Sort(result)
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, result)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
func GetPluginSettingByID(c *m.ReqContext) Response {
|
|
|
|
pluginID := c.Params(":pluginId")
|
2016-02-25 07:55:31 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
def, exists := plugins.Plugins[pluginID]
|
|
|
|
if !exists {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "Plugin not found, no installed plugin with that id", nil)
|
2018-03-22 06:37:35 -05:00
|
|
|
}
|
2016-03-08 11:17:47 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
dto := &dtos.PluginSetting{
|
|
|
|
Type: def.Type,
|
|
|
|
Id: def.Id,
|
|
|
|
Name: def.Name,
|
|
|
|
Info: &def.Info,
|
|
|
|
Dependencies: &def.Dependencies,
|
|
|
|
Includes: def.Includes,
|
|
|
|
BaseUrl: def.BaseUrl,
|
|
|
|
Module: def.Module,
|
|
|
|
DefaultNavUrl: def.DefaultNavUrl,
|
|
|
|
LatestVersion: def.GrafanaNetVersion,
|
|
|
|
HasUpdate: def.GrafanaNetHasUpdate,
|
|
|
|
State: def.State,
|
|
|
|
}
|
2016-02-25 08:56:28 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
query := m.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: c.OrgId}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
if err != m.ErrPluginSettingNotFound {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to get login settings", nil)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
} else {
|
|
|
|
dto.Enabled = query.Result.Enabled
|
|
|
|
dto.Pinned = query.Result.Pinned
|
|
|
|
dto.JsonData = query.Result.JsonData
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, dto)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func UpdatePluginSetting(c *m.ReqContext, cmd m.UpdatePluginSettingCmd) Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
pluginID := c.Params(":pluginId")
|
2016-02-25 07:55:31 -06:00
|
|
|
|
|
|
|
cmd.OrgId = c.OrgId
|
2018-03-22 06:37:35 -05:00
|
|
|
cmd.PluginId = pluginID
|
2016-02-25 07:55:31 -06:00
|
|
|
|
|
|
|
if _, ok := plugins.Apps[cmd.PluginId]; !ok {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "Plugin not installed.", nil)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to update plugin setting", err)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Success("Plugin settings updated")
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
2016-03-11 02:57:20 -06:00
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func GetPluginDashboards(c *m.ReqContext) Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
pluginID := c.Params(":pluginId")
|
2016-03-11 02:57:20 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
list, err := plugins.GetPluginDashboards(c.OrgId, pluginID)
|
|
|
|
if err != nil {
|
2016-03-11 02:57:20 -06:00
|
|
|
if notfound, ok := err.(plugins.PluginNotFoundError); ok {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, notfound.Error(), nil)
|
2016-03-11 02:57:20 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to get plugin dashboards", err)
|
2016-03-11 02:57:20 -06:00
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, list)
|
2016-03-11 02:57:20 -06:00
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func GetPluginMarkdown(c *m.ReqContext) Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
pluginID := c.Params(":pluginId")
|
2017-08-31 07:05:52 -05:00
|
|
|
name := c.Params(":name")
|
2016-03-13 13:21:44 -05:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
content, err := plugins.GetPluginMarkdown(pluginID, name)
|
|
|
|
if err != nil {
|
2016-03-13 13:21:44 -05:00
|
|
|
if notfound, ok := err.(plugins.PluginNotFoundError); ok {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, notfound.Error(), nil)
|
2016-03-13 13:21:44 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Could not get markdown file", err)
|
2016-03-13 13:21:44 -05:00
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
|
|
|
|
resp := Respond(200, content)
|
|
|
|
resp.Header("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
return resp
|
2016-03-13 13:21:44 -05:00
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Response {
|
2016-03-11 02:57:20 -06:00
|
|
|
|
2016-03-11 10:31:57 -06:00
|
|
|
cmd := plugins.ImportDashboardCommand{
|
2016-04-26 05:52:44 -05:00
|
|
|
OrgId: c.OrgId,
|
2018-02-19 04:12:56 -06:00
|
|
|
User: c.SignedInUser,
|
2016-04-26 05:52:44 -05:00
|
|
|
PluginId: apiCmd.PluginId,
|
|
|
|
Path: apiCmd.Path,
|
|
|
|
Inputs: apiCmd.Inputs,
|
|
|
|
Overwrite: apiCmd.Overwrite,
|
2018-06-04 13:29:14 -05:00
|
|
|
FolderId: apiCmd.FolderId,
|
2016-05-14 03:00:43 -05:00
|
|
|
Dashboard: apiCmd.Dashboard,
|
2016-03-11 02:57:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to import dashboard", err)
|
2016-03-11 02:57:20 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, cmd.Result)
|
2016-03-11 02:57:20 -06:00
|
|
|
}
|