DataSources: Add datasource fetching + querying interface (#80749)
* first pass * separate oss + enterprise * tidy things up * add ctx * fix tests * use standalone svcs * mv plugin context provide * fix wire * fix import
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"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"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
@@ -30,12 +31,13 @@ const (
|
||||
)
|
||||
|
||||
func ProvideService(cfg *setting.Cfg, cacheService *localcache.CacheService, pluginStore pluginstore.Store,
|
||||
dataSourceService datasources.DataSourceService, pluginSettingsService pluginsettings.Service,
|
||||
licensing plugins.Licensing, pCfg *config.Cfg) *Provider {
|
||||
dataSourceCache datasources.CacheService, dataSourceService datasources.DataSourceService,
|
||||
pluginSettingsService pluginsettings.Service, licensing plugins.Licensing, pCfg *config.Cfg) *Provider {
|
||||
return &Provider{
|
||||
cfg: cfg,
|
||||
cacheService: cacheService,
|
||||
pluginStore: pluginStore,
|
||||
dataSourceCache: dataSourceCache,
|
||||
dataSourceService: dataSourceService,
|
||||
pluginSettingsService: pluginSettingsService,
|
||||
pluginEnvVars: envvars.NewProvider(pCfg, licensing),
|
||||
@@ -48,6 +50,7 @@ type Provider struct {
|
||||
pluginEnvVars *envvars.Service
|
||||
cacheService *localcache.CacheService
|
||||
pluginStore pluginstore.Store
|
||||
dataSourceCache datasources.CacheService
|
||||
dataSourceService datasources.DataSourceService
|
||||
pluginSettingsService pluginsettings.Service
|
||||
logger log.Logger
|
||||
@@ -128,6 +131,48 @@ func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user
|
||||
return pCtx, nil
|
||||
}
|
||||
|
||||
func (p *Provider) PluginContextForDataSource(ctx context.Context, pluginID, name string) (backend.PluginContext, error) {
|
||||
plugin, exists := p.pluginStore.Plugin(ctx, pluginID)
|
||||
if !exists {
|
||||
return backend.PluginContext{}, plugins.ErrPluginNotRegistered
|
||||
}
|
||||
|
||||
user, err := appcontext.User(ctx)
|
||||
if err != nil {
|
||||
return backend.PluginContext{}, err
|
||||
}
|
||||
ds, err := p.dataSourceCache.GetDatasourceByUID(ctx, name, user, false)
|
||||
if err != nil {
|
||||
return backend.PluginContext{}, err
|
||||
}
|
||||
|
||||
pCtx := backend.PluginContext{
|
||||
PluginID: plugin.ID,
|
||||
PluginVersion: plugin.Info.Version,
|
||||
}
|
||||
if user != nil && !user.IsNil() {
|
||||
pCtx.OrgID = user.GetOrgID()
|
||||
pCtx.User = adapters.BackendUserFromSignedInUser(user)
|
||||
}
|
||||
|
||||
datasourceSettings, err := adapters.ModelToInstanceSettings(ds, p.decryptSecureJsonDataFn(ctx))
|
||||
if err != nil {
|
||||
return pCtx, err
|
||||
}
|
||||
pCtx.DataSourceInstanceSettings = datasourceSettings
|
||||
|
||||
settings := p.pluginEnvVars.GetConfigMap(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
|
||||
}
|
||||
|
||||
func (p *Provider) appInstanceSettings(ctx context.Context, pluginID string, orgID int64) (*backend.AppInstanceSettings, error) {
|
||||
jsonData := json.RawMessage{}
|
||||
decryptedSecureJSONData := map[string]string{}
|
||||
|
||||
@@ -42,7 +42,7 @@ func TestGet(t *testing.T) {
|
||||
ds := &fakeDatasources.FakeDataSourceService{}
|
||||
db := &dbtest.FakeDB{ExpectedError: pluginsettings.ErrPluginSettingNotFound}
|
||||
pcp := plugincontext.ProvideService(cfg, localcache.ProvideService(),
|
||||
pluginstore.New(preg, &pluginFakes.FakeLoader{}),
|
||||
pluginstore.New(preg, &pluginFakes.FakeLoader{}), &fakeDatasources.FakeCacheService{},
|
||||
ds, pluginSettings.ProvideService(db, secretstest.NewFakeSecretsService()), pluginFakes.NewFakeLicensingService(), &config.Cfg{},
|
||||
)
|
||||
identity := &user.SignedInUser{OrgID: int64(1), Login: "admin"}
|
||||
|
||||
@@ -42,7 +42,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/licensing"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/loader"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pipeline"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/plugincontext"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginerrs"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings"
|
||||
pluginSettings "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings/service"
|
||||
@@ -94,7 +93,6 @@ var WireSet = wire.NewSet(
|
||||
wire.Bind(new(registry.Service), new(*registry.InMemory)),
|
||||
repo.ProvideService,
|
||||
wire.Bind(new(repo.Service), new(*repo.Manager)),
|
||||
plugincontext.ProvideService,
|
||||
licensing.ProvideLicensing,
|
||||
wire.Bind(new(plugins.Licensing), new(*licensing.Service)),
|
||||
wire.Bind(new(sources.Registry), new(*sources.Service)),
|
||||
|
||||
Reference in New Issue
Block a user