Basic streaming plugin support (#31940)

This pull request migrates testdata to coreplugin streaming capabilities,
this is mostly a working concept of streaming plugins at the moment, 
the work will continue in the following pull requests.
This commit is contained in:
Alexander Emelin
2021-03-23 20:24:08 +03:00
committed by GitHub
parent 1cd8981be4
commit 336bc559a3
31 changed files with 1204 additions and 290 deletions

View File

@ -5,7 +5,6 @@ import (
"errors"
"net/http"
"sort"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
@ -14,53 +13,9 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/adapters"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"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) getPluginContext(pluginID string, user *models.SignedInUser) (backend.PluginContext, error) {
pc := backend.PluginContext{}
plugin := hs.PluginManager.GetPlugin(pluginID)
if plugin == nil {
return pc, ErrPluginNotFound
}
jsonData := json.RawMessage{}
decryptedSecureJSONData := map[string]string{}
var updated time.Time
ps, err := hs.getCachedPluginSettings(pluginID, user)
if err != nil {
// 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.
if !errors.Is(err, models.ErrPluginSettingNotFound) {
return pc, errutil.Wrap("Failed to get plugin settings", err)
}
} else {
jsonData, err = json.Marshal(ps.JsonData)
if err != nil {
return pc, errutil.Wrap("Failed to unmarshal plugin json data", err)
}
decryptedSecureJSONData = ps.DecryptedValues()
updated = ps.Updated
}
return backend.PluginContext{
OrgID: user.OrgId,
PluginID: plugin.Id,
User: adapters.BackendUserFromSignedInUser(user),
AppInstanceSettings: &backend.AppInstanceSettings{
JSONData: jsonData,
DecryptedSecureJSONData: decryptedSecureJSONData,
Updated: updated,
},
}, nil
}
func (hs *HTTPServer) GetPluginList(c *models.ReqContext) response.Response {
typeFilter := c.Query("type")
enabledFilter := c.Query("enabled")
@ -285,14 +240,13 @@ func (hs *HTTPServer) CollectPluginMetrics(c *models.ReqContext) response.Respon
func (hs *HTTPServer) CheckHealth(c *models.ReqContext) response.Response {
pluginID := c.Params("pluginId")
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
pCtx, found, err := hs.PluginContextProvider.Get(pluginID, "", c.SignedInUser)
if err != nil {
if errors.Is(err, ErrPluginNotFound) {
return response.Error(404, "Plugin not found", nil)
}
return response.Error(500, "Failed to get plugin settings", err)
}
if !found {
return response.Error(404, "Plugin not found", nil)
}
resp, err := hs.BackendPluginManager.CheckHealth(c.Req.Context(), pCtx)
if err != nil {
@ -328,39 +282,19 @@ func (hs *HTTPServer) CheckHealth(c *models.ReqContext) response.Response {
func (hs *HTTPServer) CallResource(c *models.ReqContext) {
pluginID := c.Params("pluginId")
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
pCtx, found, err := hs.PluginContextProvider.Get(pluginID, "", c.SignedInUser)
if err != nil {
if errors.Is(err, ErrPluginNotFound) {
c.JsonApiErr(404, "Plugin not found", nil)
return
}
c.JsonApiErr(500, "Failed to get plugin settings", err)
return
}
if !found {
c.JsonApiErr(404, "Plugin not found", nil)
return
}
hs.BackendPluginManager.CallResource(pCtx, c, c.Params("*"))
}
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
}
func (hs *HTTPServer) GetPluginErrorsList(c *models.ReqContext) response.Response {
func (hs *HTTPServer) GetPluginErrorsList(_ *models.ReqContext) response.Response {
return response.JSON(200, hs.PluginManager.ScanningErrors())
}