Refactor plugin context initialization (#88343)

This commit is contained in:
Andres Martinez Gotor
2024-05-28 16:59:06 +03:00
committed by GitHub
parent ecfe50439b
commit 60ce523b72
9 changed files with 164 additions and 159 deletions
@@ -5,11 +5,9 @@ import (
"encoding/json"
"errors"
"fmt"
"runtime"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/useragent"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/infra/localcache"
@@ -33,26 +31,24 @@ func ProvideService(cfg *setting.Cfg, cacheService *localcache.CacheService, plu
dataSourceCache datasources.CacheService, dataSourceService datasources.DataSourceService,
pluginSettingsService pluginsettings.Service, pluginRequestConfigProvider pluginconfig.PluginRequestConfigProvider) *Provider {
return &Provider{
cfg: cfg,
cacheService: cacheService,
pluginStore: pluginStore,
dataSourceCache: dataSourceCache,
dataSourceService: dataSourceService,
pluginSettingsService: pluginSettingsService,
pluginRequestConfigProvider: pluginRequestConfigProvider,
logger: log.New("plugin.context"),
BaseProvider: newBaseProvider(cfg, pluginRequestConfigProvider),
cacheService: cacheService,
pluginStore: pluginStore,
dataSourceCache: dataSourceCache,
dataSourceService: dataSourceService,
pluginSettingsService: pluginSettingsService,
logger: log.New("plugin.context"),
}
}
type Provider struct {
cfg *setting.Cfg
pluginRequestConfigProvider pluginconfig.PluginRequestConfigProvider
cacheService *localcache.CacheService
pluginStore pluginstore.Store
dataSourceCache datasources.CacheService
dataSourceService datasources.DataSourceService
pluginSettingsService pluginsettings.Service
logger log.Logger
*BaseProvider
cacheService *localcache.CacheService
pluginStore pluginstore.Store
dataSourceCache datasources.CacheService
dataSourceService datasources.DataSourceService
pluginSettingsService pluginsettings.Service
logger log.Logger
}
// Get will retrieve plugin context by the provided pluginID and orgID.
@@ -65,16 +61,7 @@ func (p *Provider) Get(ctx context.Context, pluginID string, user identity.Reque
return backend.PluginContext{}, plugins.ErrPluginNotRegistered
}
pCtx := backend.PluginContext{
PluginID: plugin.ID,
PluginVersion: plugin.Info.Version,
APIVersion: plugin.APIVersion,
}
if user != nil && !user.IsNil() {
pCtx.OrgID = user.GetOrgID()
pCtx.User = adapters.BackendUserFromSignedInUser(user)
}
pCtx := p.GetBasePluginContext(ctx, plugin, user)
if plugin.IsApp() {
appSettings, err := p.appInstanceSettings(ctx, pluginID, orgID)
if err != nil {
@@ -83,15 +70,6 @@ func (p *Provider) Get(ctx context.Context, pluginID string, user identity.Reque
pCtx.AppInstanceSettings = appSettings
}
settings := p.pluginRequestConfigProvider.PluginRequestConfig(ctx, pluginID, plugin.ExternalService)
pCtx.GrafanaConfig = backend.NewGrafanaCfg(settings)
ua, err := useragent.New(p.cfg.BuildVersion, runtime.GOOS, runtime.GOARCH)
if err != nil {
p.logger.Warn("Could not create user agent", "error", err)
}
pCtx.UserAgent = ua
return pCtx, nil
}
@@ -105,15 +83,7 @@ func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user
return backend.PluginContext{}, plugins.ErrPluginNotRegistered
}
pCtx := backend.PluginContext{
PluginID: plugin.ID,
PluginVersion: plugin.Info.Version,
APIVersion: plugin.APIVersion,
}
if user != nil && !user.IsNil() {
pCtx.OrgID = user.GetOrgID()
pCtx.User = adapters.BackendUserFromSignedInUser(user)
}
pCtx := p.GetBasePluginContext(ctx, plugin, user)
datasourceSettings, err := adapters.ModelToInstanceSettings(ds, p.decryptSecureJsonDataFn(ctx))
if err != nil {
@@ -121,15 +91,6 @@ func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user
}
pCtx.DataSourceInstanceSettings = datasourceSettings
settings := p.pluginRequestConfigProvider.PluginRequestConfig(ctx, pluginID, plugin.ExternalService)
pCtx.GrafanaConfig = backend.NewGrafanaCfg(settings)
ua, err := useragent.New(p.cfg.BuildVersion, runtime.GOOS, runtime.GOARCH)
if err != nil {
p.logger.Warn("Could not create user agent", "error", err)
}
pCtx.UserAgent = ua
return pCtx, nil
}
@@ -158,27 +119,9 @@ func (p *Provider) PluginContextForDataSource(ctx context.Context, datasourceSet
if err != nil {
return backend.PluginContext{}, err
}
pCtx := backend.PluginContext{
PluginID: plugin.ID,
PluginVersion: plugin.Info.Version,
APIVersion: plugin.APIVersion,
}
if user != nil && !user.IsNil() {
pCtx.OrgID = user.GetOrgID()
pCtx.User = adapters.BackendUserFromSignedInUser(user)
}
pCtx := p.GetBasePluginContext(ctx, plugin, user)
pCtx.DataSourceInstanceSettings = datasourceSettings
settings := p.pluginRequestConfigProvider.PluginRequestConfig(ctx, pluginID, plugin.ExternalService)
pCtx.GrafanaConfig = backend.NewGrafanaCfg(settings)
ua, err := useragent.New(p.cfg.BuildVersion, runtime.GOOS, runtime.GOARCH)
if err != nil {
p.logger.Warn("Could not create user agent", "error", err)
}
pCtx.UserAgent = ua
return pCtx, nil
}