2016-02-25 07:55:31 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-04-21 09:16:41 -05:00
|
|
|
"encoding/json"
|
2020-03-13 06:31:44 -05:00
|
|
|
"errors"
|
2020-03-18 06:08:20 -05:00
|
|
|
"net/http"
|
2016-03-15 09:52:29 -05:00
|
|
|
"sort"
|
2020-02-19 12:17:05 -06:00
|
|
|
"time"
|
2016-03-15 09:52:29 -05:00
|
|
|
|
2020-04-23 13:08:21 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2020-11-05 08:37:11 -06:00
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-02-19 12:17:05 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-02-25 07:55:31 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2020-03-03 04:45:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
2020-04-23 13:08:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/datasource/wrapper"
|
2016-05-03 12:00:42 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2020-04-23 13:08:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2016-02-25 07:55:31 -06:00
|
|
|
)
|
|
|
|
|
2020-03-13 06:31:44 -05:00
|
|
|
// ErrPluginNotFound is returned when an requested plugin is not installed.
|
|
|
|
var ErrPluginNotFound error = errors.New("plugin not found, no installed plugin with that id")
|
|
|
|
|
2020-04-23 13:08:21 -05:00
|
|
|
func (hs *HTTPServer) getPluginContext(pluginID string, user *models.SignedInUser) (backend.PluginContext, error) {
|
|
|
|
pc := backend.PluginContext{}
|
2020-03-13 06:31:44 -05:00
|
|
|
plugin, exists := plugins.Plugins[pluginID]
|
|
|
|
if !exists {
|
2020-04-23 13:08:21 -05:00
|
|
|
return pc, ErrPluginNotFound
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
|
|
|
|
2020-05-05 04:54:50 -05:00
|
|
|
jsonData := json.RawMessage{}
|
|
|
|
decryptedSecureJSONData := map[string]string{}
|
2020-03-13 06:31:44 -05:00
|
|
|
var updated time.Time
|
|
|
|
|
|
|
|
ps, err := hs.getCachedPluginSettings(pluginID, user)
|
|
|
|
if err != nil {
|
2020-05-05 04:54:50 -05:00
|
|
|
// models.ErrPluginSettingNotFound is expected if there's no row found for plugin setting in database (if non-app plugin).
|
|
|
|
// If it's not this expected error something is wrong with cache or database and we return the error to the client.
|
2020-11-19 06:34:28 -06:00
|
|
|
if !errors.Is(err, models.ErrPluginSettingNotFound) {
|
2020-04-23 13:08:21 -05:00
|
|
|
return pc, errutil.Wrap("Failed to get plugin settings", err)
|
|
|
|
}
|
2020-05-05 04:54:50 -05:00
|
|
|
} else {
|
2020-04-23 13:08:21 -05:00
|
|
|
jsonData, err = json.Marshal(ps.JsonData)
|
|
|
|
if err != nil {
|
|
|
|
return pc, errutil.Wrap("Failed to unmarshal plugin json data", err)
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
|
|
|
decryptedSecureJSONData = ps.DecryptedValues()
|
|
|
|
updated = ps.Updated
|
|
|
|
}
|
|
|
|
|
2020-04-23 13:08:21 -05:00
|
|
|
return backend.PluginContext{
|
|
|
|
OrgID: user.OrgId,
|
|
|
|
PluginID: plugin.Id,
|
|
|
|
User: wrapper.BackendUserFromSignedInUser(user),
|
|
|
|
AppInstanceSettings: &backend.AppInstanceSettings{
|
|
|
|
JSONData: jsonData,
|
|
|
|
DecryptedSecureJSONData: decryptedSecureJSONData,
|
|
|
|
Updated: updated,
|
|
|
|
},
|
2020-03-13 06:31:44 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-02-19 12:17:05 -06:00
|
|
|
func (hs *HTTPServer) GetPluginList(c *models.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
|
|
|
|
2020-03-16 09:40:46 -05:00
|
|
|
// For users with viewer role we only return core plugins
|
|
|
|
if !c.HasRole(models.ROLE_ADMIN) {
|
|
|
|
coreFilter = "1"
|
|
|
|
}
|
|
|
|
|
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
|
2020-03-16 09:40:46 -05:00
|
|
|
if (coreFilter == "0" && pluginDef.IsCorePlugin) || (coreFilter == "1" && !pluginDef.IsCorePlugin) {
|
2016-04-08 15:42:33 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-12 06:46:42 -05:00
|
|
|
if pluginDef.State == plugins.PluginStateAlpha && !hs.Cfg.PluginsEnableAlpha {
|
2018-11-15 04:10:47 -06:00
|
|
|
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,
|
2019-05-09 04:45:39 -05:00
|
|
|
Category: pluginDef.Category,
|
2016-04-11 11:47:04 -05:00
|
|
|
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,
|
2020-04-09 02:00:16 -05:00
|
|
|
Signature: pluginDef.Signature,
|
2020-12-11 05:57:57 -06:00
|
|
|
SignatureType: pluginDef.SignatureType,
|
|
|
|
SignatureOrg: pluginDef.SignatureOrg,
|
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 {
|
2019-05-02 12:15:39 -05:00
|
|
|
listItem.DefaultNavUrl = setting.AppSubUrl + "/plugins/" + listItem.Id + "/"
|
2016-05-03 12:00:42 -05:00
|
|
|
}
|
|
|
|
|
2020-10-23 09:45:43 -05:00
|
|
|
// filter out disabled plugins
|
2016-03-08 11:17:47 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-19 12:17:05 -06:00
|
|
|
func GetPluginSettingByID(c *models.ReqContext) Response {
|
2018-03-22 06:37:35 -05:00
|
|
|
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,
|
2020-04-09 02:00:16 -05:00
|
|
|
Signature: def.Signature,
|
2020-12-11 05:57:57 -06:00
|
|
|
SignatureType: def.SignatureType,
|
|
|
|
SignatureOrg: def.SignatureOrg,
|
2018-03-22 06:37:35 -05:00
|
|
|
}
|
2016-02-25 08:56:28 -06:00
|
|
|
|
2020-02-19 12:17:05 -06:00
|
|
|
query := models.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: c.OrgId}
|
2018-03-22 06:37:35 -05:00
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if !errors.Is(err, models.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
|
|
|
}
|
|
|
|
|
2020-02-19 12:17:05 -06:00
|
|
|
func UpdatePluginSetting(c *models.ReqContext, cmd models.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
|
|
|
|
2020-02-19 12:17:05 -06:00
|
|
|
func GetPluginDashboards(c *models.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 {
|
2020-11-19 06:34:28 -06:00
|
|
|
var notFound plugins.PluginNotFoundError
|
|
|
|
if errors.As(err, ¬Found) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-19 12:17:05 -06:00
|
|
|
func GetPluginMarkdown(c *models.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 {
|
2020-11-19 06:34:28 -06:00
|
|
|
var notFound plugins.PluginNotFoundError
|
|
|
|
if errors.As(err, ¬Found) {
|
|
|
|
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
|
|
|
|
2018-12-19 03:53:14 -06:00
|
|
|
// fallback try readme
|
|
|
|
if len(content) == 0 {
|
|
|
|
content, err = plugins.GetPluginMarkdown(pluginID, "readme")
|
|
|
|
if err != nil {
|
|
|
|
return Error(501, "Could not get markdown file", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-19 12:17:05 -06:00
|
|
|
func ImportDashboard(c *models.ReqContext, apiCmd dtos.ImportDashboardCommand) Response {
|
2020-01-15 05:10:02 -06:00
|
|
|
if apiCmd.PluginId == "" && apiCmd.Dashboard == nil {
|
|
|
|
return Error(422, "Dashboard must be set", nil)
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-20 04:42:47 -06:00
|
|
|
return dashboardSaveErrorToApiResponse(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
|
|
|
}
|
2020-01-31 04:15:50 -06:00
|
|
|
|
2020-03-18 06:08:20 -05:00
|
|
|
// CollectPluginMetrics collect metrics from a plugin.
|
|
|
|
//
|
|
|
|
// /api/plugins/:pluginId/metrics
|
|
|
|
func (hs *HTTPServer) CollectPluginMetrics(c *models.ReqContext) Response {
|
|
|
|
pluginID := c.Params("pluginId")
|
|
|
|
plugin, exists := plugins.Plugins[pluginID]
|
|
|
|
if !exists {
|
2020-06-11 09:14:05 -05:00
|
|
|
return Error(404, "Plugin not found", nil)
|
2020-03-18 06:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := hs.BackendPluginManager.CollectMetrics(c.Req.Context(), plugin.Id)
|
|
|
|
if err != nil {
|
2020-06-11 09:14:05 -05:00
|
|
|
return translatePluginRequestErrorToAPIError(err)
|
2020-03-18 06:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
headers := make(http.Header)
|
|
|
|
headers.Set("Content-Type", "text/plain")
|
|
|
|
|
|
|
|
return &NormalResponse{
|
|
|
|
header: headers,
|
|
|
|
body: resp.PrometheusMetrics,
|
|
|
|
status: http.StatusOK,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:37:36 -06:00
|
|
|
// CheckHealth returns the health of a plugin.
|
2020-01-31 04:15:50 -06:00
|
|
|
// /api/plugins/:pluginId/health
|
2020-02-19 12:17:05 -06:00
|
|
|
func (hs *HTTPServer) CheckHealth(c *models.ReqContext) Response {
|
2020-01-31 04:15:50 -06:00
|
|
|
pluginID := c.Params("pluginId")
|
2020-03-13 06:31:44 -05:00
|
|
|
|
2020-04-23 13:08:21 -05:00
|
|
|
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
|
2020-03-13 06:31:44 -05:00
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, ErrPluginNotFound) {
|
2020-06-11 09:14:05 -05:00
|
|
|
return Error(404, "Plugin not found", nil)
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Error(500, "Failed to get plugin settings", err)
|
|
|
|
}
|
|
|
|
|
2020-04-23 13:08:21 -05:00
|
|
|
resp, err := hs.BackendPluginManager.CheckHealth(c.Req.Context(), pCtx)
|
2020-01-31 04:15:50 -06:00
|
|
|
if err != nil {
|
2020-06-11 09:14:05 -05:00
|
|
|
return translatePluginRequestErrorToAPIError(err)
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
payload := map[string]interface{}{
|
2020-04-21 09:16:41 -05:00
|
|
|
"status": resp.Status.String(),
|
|
|
|
"message": resp.Message,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal JSONDetails if it's not empty.
|
|
|
|
if len(resp.JSONDetails) > 0 {
|
|
|
|
var jsonDetails map[string]interface{}
|
|
|
|
err = json.Unmarshal(resp.JSONDetails, &jsonDetails)
|
|
|
|
if err != nil {
|
|
|
|
return Error(500, "Failed to unmarshal detailed response from backend plugin", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
payload["details"] = jsonDetails
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:14:05 -05:00
|
|
|
if resp.Status != backend.HealthStatusOk {
|
2020-01-31 04:15:50 -06:00
|
|
|
return JSON(503, payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON(200, payload)
|
|
|
|
}
|
|
|
|
|
2020-03-13 06:31:44 -05:00
|
|
|
// CallResource passes a resource call from a plugin to the backend plugin.
|
|
|
|
//
|
2020-01-31 04:15:50 -06:00
|
|
|
// /api/plugins/:pluginId/resources/*
|
2020-03-03 04:45:16 -06:00
|
|
|
func (hs *HTTPServer) CallResource(c *models.ReqContext) {
|
2020-01-31 04:15:50 -06:00
|
|
|
pluginID := c.Params("pluginId")
|
2020-02-19 12:17:05 -06:00
|
|
|
|
2020-04-23 13:08:21 -05:00
|
|
|
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
|
2020-02-19 12:17:05 -06:00
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, ErrPluginNotFound) {
|
2020-06-11 09:14:05 -05:00
|
|
|
c.JsonApiErr(404, "Plugin not found", nil)
|
2020-03-03 04:45:16 -06:00
|
|
|
return
|
2020-02-19 12:17:05 -06:00
|
|
|
}
|
|
|
|
|
2020-03-13 06:31:44 -05:00
|
|
|
c.JsonApiErr(500, "Failed to get plugin settings", err)
|
|
|
|
return
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
2020-04-23 13:08:21 -05:00
|
|
|
hs.BackendPluginManager.CallResource(pCtx, c, c.Params("*"))
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
2020-02-19 12:17:05 -06:00
|
|
|
|
|
|
|
func (hs *HTTPServer) getCachedPluginSettings(pluginID string, user *models.SignedInUser) (*models.PluginSetting, error) {
|
|
|
|
cacheKey := "plugin-setting-" + pluginID
|
|
|
|
|
|
|
|
if cached, found := hs.CacheService.Get(cacheKey); found {
|
|
|
|
ps := cached.(*models.PluginSetting)
|
|
|
|
if ps.OrgId == user.OrgId {
|
|
|
|
return ps, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
query := models.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: user.OrgId}
|
|
|
|
if err := hs.Bus.Dispatch(&query); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hs.CacheService.Set(cacheKey, query.Result, time.Second*5)
|
|
|
|
return query.Result, nil
|
|
|
|
}
|
2020-06-11 09:14:05 -05:00
|
|
|
|
2020-10-23 09:45:43 -05:00
|
|
|
func (hs *HTTPServer) GetPluginErrorsList(c *models.ReqContext) Response {
|
|
|
|
return JSON(200, plugins.ScanningErrors())
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:14:05 -05:00
|
|
|
func translatePluginRequestErrorToAPIError(err error) Response {
|
|
|
|
if errors.Is(err, backendplugin.ErrPluginNotRegistered) {
|
|
|
|
return Error(404, "Plugin not found", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, backendplugin.ErrMethodNotImplemented) {
|
|
|
|
return Error(404, "Not found", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, backendplugin.ErrHealthCheckFailed) {
|
|
|
|
return Error(500, "Plugin health check failed", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, backendplugin.ErrPluginUnavailable) {
|
|
|
|
return Error(503, "Plugin unavailable", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error(500, "Plugin request failed", err)
|
|
|
|
}
|