mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 09:12:56 +08:00
Backend Plugins: (breaking change) Add PluginContext (#23788)
* breaking change for newer backend plugins * use exported protobuf converters and sdk types to reduce duplicate code * uses grafana-plugin-sdk-go@v0.54.0
This commit is contained in:
@ -7,49 +7,56 @@ import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/datasource/wrapper"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
)
|
||||
|
||||
// ErrPluginNotFound is returned when an requested plugin is not installed.
|
||||
var ErrPluginNotFound error = errors.New("plugin not found, no installed plugin with that id")
|
||||
|
||||
func (hs *HTTPServer) getPluginConfig(pluginID string, user *models.SignedInUser) (backendplugin.PluginConfig, error) {
|
||||
pluginConfig := backendplugin.PluginConfig{}
|
||||
func (hs *HTTPServer) getPluginContext(pluginID string, user *models.SignedInUser) (backend.PluginContext, error) {
|
||||
pc := backend.PluginContext{}
|
||||
plugin, exists := plugins.Plugins[pluginID]
|
||||
if !exists {
|
||||
return pluginConfig, ErrPluginNotFound
|
||||
return pc, ErrPluginNotFound
|
||||
}
|
||||
|
||||
var jsonData *simplejson.Json
|
||||
var jsonData json.RawMessage
|
||||
var decryptedSecureJSONData map[string]string
|
||||
var updated time.Time
|
||||
|
||||
ps, err := hs.getCachedPluginSettings(pluginID, user)
|
||||
if err != nil {
|
||||
if err != models.ErrPluginSettingNotFound {
|
||||
return pluginConfig, errutil.Wrap("Failed to get plugin settings", err)
|
||||
return pc, errutil.Wrap("Failed to get plugin settings", err)
|
||||
}
|
||||
jsonData, err = json.Marshal(ps.JsonData)
|
||||
if err != nil {
|
||||
return pc, errutil.Wrap("Failed to unmarshal plugin json data", err)
|
||||
}
|
||||
jsonData = simplejson.New()
|
||||
decryptedSecureJSONData = make(map[string]string)
|
||||
} else {
|
||||
decryptedSecureJSONData = ps.DecryptedValues()
|
||||
updated = ps.Updated
|
||||
}
|
||||
|
||||
return backendplugin.PluginConfig{
|
||||
OrgID: user.OrgId,
|
||||
PluginID: plugin.Id,
|
||||
JSONData: jsonData,
|
||||
DecryptedSecureJSONData: decryptedSecureJSONData,
|
||||
Updated: updated,
|
||||
return backend.PluginContext{
|
||||
OrgID: user.OrgId,
|
||||
PluginID: plugin.Id,
|
||||
User: wrapper.BackendUserFromSignedInUser(user),
|
||||
AppInstanceSettings: &backend.AppInstanceSettings{
|
||||
JSONData: jsonData,
|
||||
DecryptedSecureJSONData: decryptedSecureJSONData,
|
||||
Updated: updated,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -289,7 +296,7 @@ func (hs *HTTPServer) CollectPluginMetrics(c *models.ReqContext) Response {
|
||||
func (hs *HTTPServer) CheckHealth(c *models.ReqContext) Response {
|
||||
pluginID := c.Params("pluginId")
|
||||
|
||||
config, err := hs.getPluginConfig(pluginID, c.SignedInUser)
|
||||
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
|
||||
if err != nil {
|
||||
if err == ErrPluginNotFound {
|
||||
return Error(404, "Plugin not found, no installed plugin with that id", nil)
|
||||
@ -298,7 +305,7 @@ func (hs *HTTPServer) CheckHealth(c *models.ReqContext) Response {
|
||||
return Error(500, "Failed to get plugin settings", err)
|
||||
}
|
||||
|
||||
resp, err := hs.BackendPluginManager.CheckHealth(c.Req.Context(), &config)
|
||||
resp, err := hs.BackendPluginManager.CheckHealth(c.Req.Context(), pCtx)
|
||||
if err != nil {
|
||||
if err == backendplugin.ErrPluginNotRegistered {
|
||||
return Error(404, "Plugin not found", err)
|
||||
@ -346,7 +353,7 @@ func (hs *HTTPServer) CheckHealth(c *models.ReqContext) Response {
|
||||
func (hs *HTTPServer) CallResource(c *models.ReqContext) {
|
||||
pluginID := c.Params("pluginId")
|
||||
|
||||
config, err := hs.getPluginConfig(pluginID, c.SignedInUser)
|
||||
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
|
||||
if err != nil {
|
||||
if err == ErrPluginNotFound {
|
||||
c.JsonApiErr(404, "Plugin not found, no installed plugin with that id", nil)
|
||||
@ -356,8 +363,7 @@ func (hs *HTTPServer) CallResource(c *models.ReqContext) {
|
||||
c.JsonApiErr(500, "Failed to get plugin settings", err)
|
||||
return
|
||||
}
|
||||
|
||||
hs.BackendPluginManager.CallResource(config, c, c.Params("*"))
|
||||
hs.BackendPluginManager.CallResource(pCtx, c, c.Params("*"))
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) getCachedPluginSettings(pluginID string, user *models.SignedInUser) (*models.PluginSetting, error) {
|
||||
|
Reference in New Issue
Block a user