Plugins: Add simple plugin sources service (#63814)

add simple plugin sources svc
This commit is contained in:
Will Browne
2023-02-28 15:27:11 +01:00
committed by GitHub
parent b2c0175777
commit ab8de1a0e3
9 changed files with 143 additions and 50 deletions
+11
View File
@@ -0,0 +1,11 @@
package sources
import (
"context"
"github.com/grafana/grafana/pkg/plugins"
)
type Resolver interface {
List(context.Context) []plugins.PluginSource
}
+53
View File
@@ -0,0 +1,53 @@
package sources
import (
"context"
"path/filepath"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/config"
"github.com/grafana/grafana/pkg/setting"
)
type Service struct {
gCfg *setting.Cfg
cfg *config.Cfg
log log.Logger
}
func ProvideService(gCfg *setting.Cfg, cfg *config.Cfg) *Service {
return &Service{
gCfg: gCfg,
cfg: cfg,
log: log.New("plugin.sources"),
}
}
func (s *Service) List(_ context.Context) []plugins.PluginSource {
return []plugins.PluginSource{
{Class: plugins.Core, Paths: corePluginPaths(s.gCfg.StaticRootPath)},
{Class: plugins.Bundled, Paths: []string{s.gCfg.BundledPluginsPath}},
{Class: plugins.External, Paths: append([]string{s.cfg.PluginsPath}, pluginFSPaths(s.cfg.PluginSettings)...)},
}
}
// corePluginPaths provides a list of the Core plugin file system paths
func corePluginPaths(staticRootPath string) []string {
datasourcePaths := filepath.Join(staticRootPath, "app/plugins/datasource")
panelsPath := filepath.Join(staticRootPath, "app/plugins/panel")
return []string{datasourcePaths, panelsPath}
}
// pluginSettingPaths provides plugin file system paths defined in cfg.PluginSettings
func pluginFSPaths(ps map[string]map[string]string) []string {
var pluginSettingDirs []string
for _, s := range ps {
path, exists := s["path"]
if !exists || path == "" {
continue
}
pluginSettingDirs = append(pluginSettingDirs, path)
}
return pluginSettingDirs
}
@@ -0,0 +1,41 @@
package sources
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/config"
"github.com/grafana/grafana/pkg/setting"
)
func TestSources_List(t *testing.T) {
t.Run("Plugin sources are added in order", func(t *testing.T) {
cfg := &setting.Cfg{
BundledPluginsPath: "path1",
}
pCfg := &config.Cfg{
PluginsPath: "path2",
PluginSettings: setting.PluginSettings{
"foo": map[string]string{
"path": "path3",
},
"bar": map[string]string{
"url": "https://grafana.plugin",
},
},
}
s := ProvideService(cfg, pCfg)
srcs := s.List(context.Background())
expected := []plugins.PluginSource{
{Class: plugins.Core, Paths: []string{"app/plugins/datasource", "app/plugins/panel"}},
{Class: plugins.Bundled, Paths: []string{"path1"}},
{Class: plugins.External, Paths: []string{"path2", "path3"}},
}
require.Equal(t, expected, srcs)
})
}