2022-01-14 06:30:39 -06:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
2022-01-20 11:16:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/coreplugin"
|
2022-01-14 06:30:39 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/grpcplugin"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
2022-06-09 12:19:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/secretsmanagerplugin"
|
2022-01-14 06:30:39 -06:00
|
|
|
)
|
|
|
|
|
2022-01-27 11:06:38 -06:00
|
|
|
// PluginBackendProvider is a function type for initializing a Plugin backend.
|
|
|
|
type PluginBackendProvider func(_ context.Context, _ *plugins.Plugin) backendplugin.PluginFactoryFunc
|
|
|
|
|
2022-01-20 11:16:22 -06:00
|
|
|
type Service struct {
|
2022-01-27 11:06:38 -06:00
|
|
|
providerChain []PluginBackendProvider
|
2022-01-20 11:16:22 -06:00
|
|
|
}
|
2022-01-14 06:30:39 -06:00
|
|
|
|
2022-01-27 11:06:38 -06:00
|
|
|
func New(providers ...PluginBackendProvider) *Service {
|
|
|
|
if len(providers) == 0 {
|
2022-06-09 12:19:27 -05:00
|
|
|
return New(RendererProvider, SecretsManagerProvider, DefaultProvider)
|
2022-01-27 11:06:38 -06:00
|
|
|
}
|
2022-01-20 11:16:22 -06:00
|
|
|
return &Service{
|
2022-01-27 11:06:38 -06:00
|
|
|
providerChain: providers,
|
2022-01-20 11:16:22 -06:00
|
|
|
}
|
2022-01-14 06:30:39 -06:00
|
|
|
}
|
|
|
|
|
2022-01-27 11:06:38 -06:00
|
|
|
func ProvideService(coreRegistry *coreplugin.Registry) *Service {
|
2022-06-09 12:19:27 -05:00
|
|
|
return New(coreRegistry.BackendFactoryProvider(), RendererProvider, SecretsManagerProvider, DefaultProvider)
|
2022-01-27 11:06:38 -06:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:16:22 -06:00
|
|
|
func (s *Service) BackendFactory(ctx context.Context, p *plugins.Plugin) backendplugin.PluginFactoryFunc {
|
2022-01-27 11:06:38 -06:00
|
|
|
for _, provider := range s.providerChain {
|
2022-01-14 06:30:39 -06:00
|
|
|
if factory := provider(ctx, p); factory != nil {
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var RendererProvider PluginBackendProvider = func(_ context.Context, p *plugins.Plugin) backendplugin.PluginFactoryFunc {
|
|
|
|
if !p.IsRenderer() {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-02 06:46:55 -06:00
|
|
|
return grpcplugin.NewRendererPlugin(p.ID, p.ExecutablePath(),
|
2022-01-14 06:30:39 -06:00
|
|
|
func(pluginID string, renderer pluginextensionv2.RendererPlugin, logger log.Logger) error {
|
|
|
|
p.Renderer = renderer
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-09 12:19:27 -05:00
|
|
|
var SecretsManagerProvider PluginBackendProvider = func(_ context.Context, p *plugins.Plugin) backendplugin.PluginFactoryFunc {
|
|
|
|
if !p.IsSecretsManager() {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-02 06:46:55 -06:00
|
|
|
return grpcplugin.NewSecretsManagerPlugin(p.ID, p.ExecutablePath(),
|
2022-06-09 12:19:27 -05:00
|
|
|
func(pluginID string, secretsmanager secretsmanagerplugin.SecretsManagerPlugin, logger log.Logger) error {
|
|
|
|
p.SecretsManager = secretsmanager
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-14 06:30:39 -06:00
|
|
|
var DefaultProvider PluginBackendProvider = func(_ context.Context, p *plugins.Plugin) backendplugin.PluginFactoryFunc {
|
2022-12-02 06:46:55 -06:00
|
|
|
return grpcplugin.NewBackendPlugin(p.ID, p.ExecutablePath())
|
2022-06-09 12:19:27 -05:00
|
|
|
}
|