mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Refactor creation of plugin context to dedicated service (#66451)
* first pass * fix tests * return errs * change signature * tidy * delete unnecessary fields from test * tidy * fix tests * simplify * separate error check in API * apply nits
This commit is contained in:
@@ -11,68 +11,78 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/adapters"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
var ErrPluginNotFound = errors.New("plugin not found")
|
||||
|
||||
func ProvideService(cacheService *localcache.CacheService, pluginStore plugins.Store,
|
||||
dataSourceCache datasources.CacheService, dataSourceService datasources.DataSourceService,
|
||||
pluginSettingsService pluginsettings.Service) *Provider {
|
||||
dataSourceService datasources.DataSourceService, pluginSettingsService pluginsettings.Service) *Provider {
|
||||
return &Provider{
|
||||
cacheService: cacheService,
|
||||
pluginStore: pluginStore,
|
||||
dataSourceCache: dataSourceCache,
|
||||
dataSourceService: dataSourceService,
|
||||
pluginSettingsService: pluginSettingsService,
|
||||
logger: log.New("plugincontext"),
|
||||
}
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
cacheService *localcache.CacheService
|
||||
pluginStore plugins.Store
|
||||
dataSourceCache datasources.CacheService
|
||||
dataSourceService datasources.DataSourceService
|
||||
pluginSettingsService pluginsettings.Service
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
// Get allows getting plugin context by its ID. If datasourceUID is not empty string
|
||||
// then PluginContext.DataSourceInstanceSettings will be resolved and appended to
|
||||
// returned context.
|
||||
func (p *Provider) Get(ctx context.Context, pluginID string, user *user.SignedInUser) (backend.PluginContext, bool, error) {
|
||||
return p.pluginContext(ctx, pluginID, user)
|
||||
func (p *Provider) Get(ctx context.Context, pluginID string, user *user.SignedInUser) (backend.PluginContext, error) {
|
||||
plugin, exists := p.pluginStore.Plugin(ctx, pluginID)
|
||||
if !exists {
|
||||
return backend.PluginContext{}, ErrPluginNotFound
|
||||
}
|
||||
|
||||
pCtx := backend.PluginContext{
|
||||
OrgID: user.OrgID,
|
||||
PluginID: pluginID,
|
||||
User: adapters.BackendUserFromSignedInUser(user),
|
||||
}
|
||||
|
||||
if plugin.IsApp() {
|
||||
appSettings, err := p.appInstanceSettings(ctx, pluginID, user)
|
||||
if err != nil {
|
||||
return backend.PluginContext{}, err
|
||||
}
|
||||
pCtx.AppInstanceSettings = appSettings
|
||||
}
|
||||
|
||||
return pCtx, nil
|
||||
}
|
||||
|
||||
// GetWithDataSource allows getting plugin context by its ID and PluginContext.DataSourceInstanceSettings will be
|
||||
// resolved and appended to the returned context.
|
||||
func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user *user.SignedInUser, ds *datasources.DataSource) (backend.PluginContext, bool, error) {
|
||||
pCtx, exists, err := p.pluginContext(ctx, pluginID, user)
|
||||
func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user *user.SignedInUser, ds *datasources.DataSource) (backend.PluginContext, error) {
|
||||
pCtx, err := p.Get(ctx, pluginID, user)
|
||||
if err != nil {
|
||||
return pCtx, exists, err
|
||||
return backend.PluginContext{}, err
|
||||
}
|
||||
|
||||
datasourceSettings, err := adapters.ModelToInstanceSettings(ds, p.decryptSecureJsonDataFn(ctx))
|
||||
if err != nil {
|
||||
return pCtx, exists, fmt.Errorf("%v: %w", "Failed to convert datasource", err)
|
||||
return pCtx, err
|
||||
}
|
||||
pCtx.DataSourceInstanceSettings = datasourceSettings
|
||||
|
||||
return pCtx, true, nil
|
||||
return pCtx, nil
|
||||
}
|
||||
|
||||
const pluginSettingsCacheTTL = 5 * time.Second
|
||||
const pluginSettingsCachePrefix = "plugin-setting-"
|
||||
|
||||
func (p *Provider) pluginContext(ctx context.Context, pluginID string, user *user.SignedInUser) (backend.PluginContext, bool, error) {
|
||||
plugin, exists := p.pluginStore.Plugin(ctx, pluginID)
|
||||
if !exists {
|
||||
return backend.PluginContext{}, false, nil
|
||||
}
|
||||
|
||||
func (p *Provider) appInstanceSettings(ctx context.Context, pluginID string, user *user.SignedInUser) (*backend.AppInstanceSettings, error) {
|
||||
jsonData := json.RawMessage{}
|
||||
decryptedSecureJSONData := map[string]string{}
|
||||
var updated time.Time
|
||||
@@ -80,29 +90,28 @@ func (p *Provider) pluginContext(ctx context.Context, pluginID string, user *use
|
||||
ps, err := p.getCachedPluginSettings(ctx, pluginID, user)
|
||||
if err != nil {
|
||||
// pluginsettings.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.
|
||||
// Otherwise, something is wrong with cache or database, and we return the error to the client.
|
||||
if !errors.Is(err, pluginsettings.ErrPluginSettingNotFound) {
|
||||
return backend.PluginContext{}, false, fmt.Errorf("%v: %w", "Failed to get plugin settings", err)
|
||||
return nil, fmt.Errorf("%v: %w", "Failed to get plugin settings", err)
|
||||
}
|
||||
} else {
|
||||
jsonData, err = json.Marshal(ps.JSONData)
|
||||
if err != nil {
|
||||
return backend.PluginContext{}, false, fmt.Errorf("%v: %w", "Failed to unmarshal plugin json data", err)
|
||||
return nil, fmt.Errorf("%v: %w", "Failed to unmarshal plugin json data", err)
|
||||
}
|
||||
decryptedSecureJSONData = p.pluginSettingsService.DecryptedValues(ps)
|
||||
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,
|
||||
},
|
||||
}, true, nil
|
||||
return &backend.AppInstanceSettings{
|
||||
JSONData: jsonData,
|
||||
DecryptedSecureJSONData: decryptedSecureJSONData,
|
||||
Updated: updated,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Provider) InvalidateSettingsCache(_ context.Context, pluginID string) {
|
||||
p.cacheService.Delete(getCacheKey(pluginID))
|
||||
}
|
||||
|
||||
func (p *Provider) getCachedPluginSettings(ctx context.Context, pluginID string, user *user.SignedInUser) (*pluginsettings.DTO, error) {
|
||||
@@ -127,10 +136,6 @@ func (p *Provider) getCachedPluginSettings(ctx context.Context, pluginID string,
|
||||
return ps, nil
|
||||
}
|
||||
|
||||
func (p *Provider) InvalidateSettingsCache(_ context.Context, pluginID string) {
|
||||
p.cacheService.Delete(getCacheKey(pluginID))
|
||||
}
|
||||
|
||||
func (p *Provider) decryptSecureJsonDataFn(ctx context.Context) func(ds *datasources.DataSource) (map[string]string, error) {
|
||||
return func(ds *datasources.DataSource) (map[string]string, error) {
|
||||
return p.dataSourceService.DecryptedValues(ctx, ds)
|
||||
|
||||
Reference in New Issue
Block a user