mirror of
https://github.com/grafana/grafana.git
synced 2025-07-29 09:02:24 +08:00
Backend Plugins: Provide proper plugin config to plugins (#21985)
Properly provides plugin configs to backend plugins. Uses v0.16.0 of grafana-plugin-sdk-go- Ref #21512 Ref #19667
This commit is contained in:

committed by
GitHub

parent
f82a6aa0d0
commit
9d7c74ef91
@ -1,18 +1,20 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
|
||||
"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/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func (hs *HTTPServer) GetPluginList(c *m.ReqContext) Response {
|
||||
func (hs *HTTPServer) GetPluginList(c *models.ReqContext) Response {
|
||||
typeFilter := c.Query("type")
|
||||
enabledFilter := c.Query("enabled")
|
||||
embeddedFilter := c.Query("embedded")
|
||||
@ -85,7 +87,7 @@ func (hs *HTTPServer) GetPluginList(c *m.ReqContext) Response {
|
||||
return JSON(200, result)
|
||||
}
|
||||
|
||||
func GetPluginSettingByID(c *m.ReqContext) Response {
|
||||
func GetPluginSettingByID(c *models.ReqContext) Response {
|
||||
pluginID := c.Params(":pluginId")
|
||||
|
||||
def, exists := plugins.Plugins[pluginID]
|
||||
@ -108,9 +110,9 @@ func GetPluginSettingByID(c *m.ReqContext) Response {
|
||||
State: def.State,
|
||||
}
|
||||
|
||||
query := m.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: c.OrgId}
|
||||
query := models.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: c.OrgId}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
if err != m.ErrPluginSettingNotFound {
|
||||
if err != models.ErrPluginSettingNotFound {
|
||||
return Error(500, "Failed to get login settings", nil)
|
||||
}
|
||||
} else {
|
||||
@ -122,7 +124,7 @@ func GetPluginSettingByID(c *m.ReqContext) Response {
|
||||
return JSON(200, dto)
|
||||
}
|
||||
|
||||
func UpdatePluginSetting(c *m.ReqContext, cmd m.UpdatePluginSettingCmd) Response {
|
||||
func UpdatePluginSetting(c *models.ReqContext, cmd models.UpdatePluginSettingCmd) Response {
|
||||
pluginID := c.Params(":pluginId")
|
||||
|
||||
cmd.OrgId = c.OrgId
|
||||
@ -139,7 +141,7 @@ func UpdatePluginSetting(c *m.ReqContext, cmd m.UpdatePluginSettingCmd) Response
|
||||
return Success("Plugin settings updated")
|
||||
}
|
||||
|
||||
func GetPluginDashboards(c *m.ReqContext) Response {
|
||||
func GetPluginDashboards(c *models.ReqContext) Response {
|
||||
pluginID := c.Params(":pluginId")
|
||||
|
||||
list, err := plugins.GetPluginDashboards(c.OrgId, pluginID)
|
||||
@ -154,7 +156,7 @@ func GetPluginDashboards(c *m.ReqContext) Response {
|
||||
return JSON(200, list)
|
||||
}
|
||||
|
||||
func GetPluginMarkdown(c *m.ReqContext) Response {
|
||||
func GetPluginMarkdown(c *models.ReqContext) Response {
|
||||
pluginID := c.Params(":pluginId")
|
||||
name := c.Params(":name")
|
||||
|
||||
@ -180,7 +182,7 @@ func GetPluginMarkdown(c *m.ReqContext) Response {
|
||||
return resp
|
||||
}
|
||||
|
||||
func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Response {
|
||||
func ImportDashboard(c *models.ReqContext, apiCmd dtos.ImportDashboardCommand) Response {
|
||||
if apiCmd.PluginId == "" && apiCmd.Dashboard == nil {
|
||||
return Error(422, "Dashboard must be set", nil)
|
||||
}
|
||||
@ -204,7 +206,7 @@ func ImportDashboard(c *m.ReqContext, apiCmd dtos.ImportDashboardCommand) Respon
|
||||
}
|
||||
|
||||
// /api/plugins/:pluginId/health
|
||||
func (hs *HTTPServer) CheckHealth(c *m.ReqContext) Response {
|
||||
func (hs *HTTPServer) CheckHealth(c *models.ReqContext) Response {
|
||||
pluginID := c.Params("pluginId")
|
||||
resp, err := hs.BackendPluginManager.CheckHealth(c.Req.Context(), pluginID)
|
||||
if err != nil {
|
||||
@ -236,21 +238,45 @@ func (hs *HTTPServer) CheckHealth(c *m.ReqContext) Response {
|
||||
}
|
||||
|
||||
// /api/plugins/:pluginId/resources/*
|
||||
func (hs *HTTPServer) CallResource(c *m.ReqContext) Response {
|
||||
func (hs *HTTPServer) CallResource(c *models.ReqContext) Response {
|
||||
pluginID := c.Params("pluginId")
|
||||
_, exists := plugins.Plugins[pluginID]
|
||||
plugin, exists := plugins.Plugins[pluginID]
|
||||
if !exists {
|
||||
return Error(404, "Plugin not found, no installed plugin with that id", nil)
|
||||
}
|
||||
|
||||
var jsonDataBytes []byte
|
||||
var decryptedSecureJSONData map[string]string
|
||||
var updated time.Time
|
||||
|
||||
ps, err := hs.getCachedPluginSettings(pluginID, c.SignedInUser)
|
||||
if err != nil {
|
||||
if err != models.ErrPluginSettingNotFound {
|
||||
return Error(500, "Failed to get plugin settings", err)
|
||||
}
|
||||
} else {
|
||||
jsonDataBytes, err = json.Marshal(&ps.JsonData)
|
||||
if err != nil {
|
||||
return Error(500, "Failed to marshal JSON data to bytes", err)
|
||||
}
|
||||
|
||||
decryptedSecureJSONData = ps.DecryptedValues()
|
||||
updated = ps.Updated
|
||||
}
|
||||
|
||||
body, err := c.Req.Body().Bytes()
|
||||
if err != nil {
|
||||
return Error(500, "Failed to read request body", err)
|
||||
}
|
||||
|
||||
req := backendplugin.CallResourceRequest{
|
||||
Config: backendplugin.PluginConfig{
|
||||
OrgID: c.OrgId,
|
||||
PluginID: pluginID,
|
||||
OrgID: c.OrgId,
|
||||
PluginID: plugin.Id,
|
||||
PluginType: plugin.Type,
|
||||
JSONData: jsonDataBytes,
|
||||
DecryptedSecureJSONData: decryptedSecureJSONData,
|
||||
Updated: updated,
|
||||
},
|
||||
Path: c.Params("*"),
|
||||
Method: c.Req.Method,
|
||||
@ -273,3 +299,22 @@ func (hs *HTTPServer) CallResource(c *m.ReqContext) Response {
|
||||
header: resp.Headers,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user