Plugins: Introduce plugin asset provider (#108063)
* introduce plugin asset provider * simply with PR feedback * fix linter
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/plugins/auth"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
"github.com/grafana/grafana/pkg/plugins/pluginassets"
|
||||
"github.com/grafana/grafana/pkg/plugins/repo"
|
||||
"github.com/grafana/grafana/pkg/plugins/storage"
|
||||
)
|
||||
@@ -378,6 +379,7 @@ type FakeLicensingService struct {
|
||||
TokenRaw string
|
||||
LicensePath string
|
||||
LicenseAppURL string
|
||||
CDNPrefix string
|
||||
}
|
||||
|
||||
func NewFakeLicensingService() *FakeLicensingService {
|
||||
@@ -400,6 +402,10 @@ func (s *FakeLicensingService) Environment() []string {
|
||||
return []string{fmt.Sprintf("GF_ENTERPRISE_LICENSE_TEXT=%s", s.TokenRaw)}
|
||||
}
|
||||
|
||||
func (s *FakeLicensingService) ContentDeliveryPrefix() string {
|
||||
return s.CDNPrefix
|
||||
}
|
||||
|
||||
type FakeRoleRegistry struct {
|
||||
ExpectedErr error
|
||||
}
|
||||
@@ -665,3 +671,26 @@ func (p *FakeBackendPlugin) Target() backendplugin.Target {
|
||||
func (p *FakeBackendPlugin) Logger() log.Logger {
|
||||
return log.NewTestLogger()
|
||||
}
|
||||
|
||||
type AssetProvider struct {
|
||||
ModuleFunc func(plugin pluginassets.PluginInfo) (string, error)
|
||||
AssetPathFunc func(plugin pluginassets.PluginInfo, assetPath ...string) (string, error)
|
||||
}
|
||||
|
||||
func NewFakeAssetProvider() *AssetProvider {
|
||||
return &AssetProvider{}
|
||||
}
|
||||
|
||||
func (p *AssetProvider) Module(plugin pluginassets.PluginInfo) (string, error) {
|
||||
if p.ModuleFunc != nil {
|
||||
return p.ModuleFunc(plugin)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (p *AssetProvider) AssetPath(plugin pluginassets.PluginInfo, assetPath ...string) (string, error) {
|
||||
if p.AssetPathFunc != nil {
|
||||
return p.AssetPathFunc(plugin, assetPath...)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/fakes"
|
||||
"github.com/grafana/grafana/pkg/plugins/pluginassets"
|
||||
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
|
||||
)
|
||||
|
||||
@@ -16,82 +18,77 @@ import (
|
||||
// It supports core plugins, external plugins stored on the local filesystem, and external plugins stored
|
||||
// on the plugins CDN, and it will switch to the correct implementation depending on the plugin and the config.
|
||||
type Service struct {
|
||||
cdn *pluginscdn.Service
|
||||
cfg *config.PluginManagementCfg
|
||||
cdn *pluginscdn.Service
|
||||
cfg *config.PluginManagementCfg
|
||||
assetProvider pluginassets.Provider
|
||||
}
|
||||
|
||||
func ProvideService(cfg *config.PluginManagementCfg, cdn *pluginscdn.Service) *Service {
|
||||
return &Service{cfg: cfg, cdn: cdn}
|
||||
}
|
||||
|
||||
type PluginInfo struct {
|
||||
pluginJSON plugins.JSONData
|
||||
class plugins.Class
|
||||
fs plugins.FS
|
||||
parent *PluginInfo
|
||||
}
|
||||
|
||||
func NewPluginInfo(pluginJSON plugins.JSONData, class plugins.Class, fs plugins.FS, parent *PluginInfo) PluginInfo {
|
||||
return PluginInfo{
|
||||
pluginJSON: pluginJSON,
|
||||
class: class,
|
||||
fs: fs,
|
||||
parent: parent,
|
||||
}
|
||||
func ProvideService(cfg *config.PluginManagementCfg, cdn *pluginscdn.Service, assetProvider pluginassets.Provider) *Service {
|
||||
return &Service{cfg: cfg, cdn: cdn, assetProvider: assetProvider}
|
||||
}
|
||||
|
||||
func DefaultService(cfg *config.PluginManagementCfg) *Service {
|
||||
return &Service{cfg: cfg, cdn: pluginscdn.ProvideService(cfg)}
|
||||
return &Service{cfg: cfg, cdn: pluginscdn.ProvideService(cfg), assetProvider: fakes.NewFakeAssetProvider()}
|
||||
}
|
||||
|
||||
// Base returns the base path for the specified plugin.
|
||||
func (s *Service) Base(n PluginInfo) (string, error) {
|
||||
if n.class == plugins.ClassCDN {
|
||||
return n.fs.Base(), nil
|
||||
func (s *Service) Base(n pluginassets.PluginInfo) (string, error) {
|
||||
if s.cfg.Features.PluginAssetProvider {
|
||||
return s.assetProvider.AssetPath(n)
|
||||
}
|
||||
if s.cdn.PluginSupported(n.pluginJSON.ID) {
|
||||
return s.cdn.AssetURL(n.pluginJSON.ID, n.pluginJSON.Info.Version, "")
|
||||
|
||||
if n.Class == plugins.ClassCDN {
|
||||
return n.FS.Base(), nil
|
||||
}
|
||||
if n.parent != nil {
|
||||
relPath, err := n.parent.fs.Rel(n.fs.Base())
|
||||
if s.cdn.PluginSupported(n.JsonData.ID) {
|
||||
return s.cdn.AssetURL(n.JsonData.ID, n.JsonData.Info.Version, "")
|
||||
}
|
||||
if n.Parent != nil {
|
||||
relPath, err := n.Parent.FS.Rel(n.FS.Base())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if s.cdn.PluginSupported(n.parent.pluginJSON.ID) {
|
||||
return s.cdn.AssetURL(n.parent.pluginJSON.ID, n.parent.pluginJSON.Info.Version, relPath)
|
||||
if s.cdn.PluginSupported(n.Parent.JsonData.ID) {
|
||||
return s.cdn.AssetURL(n.Parent.JsonData.ID, n.Parent.JsonData.Info.Version, relPath)
|
||||
}
|
||||
}
|
||||
|
||||
return path.Join("public/plugins", n.pluginJSON.ID), nil
|
||||
return path.Join("public/plugins", n.JsonData.ID), nil
|
||||
}
|
||||
|
||||
// Module returns the module.js path for the specified plugin.
|
||||
func (s *Service) Module(n PluginInfo) (string, error) {
|
||||
if n.class == plugins.ClassCore {
|
||||
if filepath.Base(n.fs.Base()) != "dist" {
|
||||
return path.Join("core:plugin", filepath.Base(n.fs.Base())), nil
|
||||
}
|
||||
func (s *Service) Module(n pluginassets.PluginInfo) (string, error) {
|
||||
if s.cfg.Features.PluginAssetProvider {
|
||||
return s.assetProvider.Module(n)
|
||||
}
|
||||
|
||||
if n.Class == plugins.ClassCore && filepath.Base(n.FS.Base()) != "dist" {
|
||||
return path.Join("core:plugin", filepath.Base(n.FS.Base())), nil
|
||||
}
|
||||
|
||||
return s.RelativeURL(n, "module.js")
|
||||
}
|
||||
|
||||
// RelativeURL returns the relative URL for an arbitrary plugin asset.
|
||||
func (s *Service) RelativeURL(n PluginInfo, pathStr string) (string, error) {
|
||||
if n.class == plugins.ClassCDN {
|
||||
return pluginscdn.JoinPath(n.fs.Base(), pathStr)
|
||||
func (s *Service) RelativeURL(n pluginassets.PluginInfo, pathStr string) (string, error) {
|
||||
if s.cfg.Features.PluginAssetProvider {
|
||||
return s.assetProvider.AssetPath(n, pathStr)
|
||||
}
|
||||
|
||||
if s.cdn.PluginSupported(n.pluginJSON.ID) {
|
||||
return s.cdn.NewCDNURLConstructor(n.pluginJSON.ID, n.pluginJSON.Info.Version).StringPath(pathStr)
|
||||
if n.Class == plugins.ClassCDN {
|
||||
return pluginscdn.JoinPath(n.FS.Base(), pathStr)
|
||||
}
|
||||
if n.parent != nil {
|
||||
if s.cdn.PluginSupported(n.parent.pluginJSON.ID) {
|
||||
relPath, err := n.parent.fs.Rel(n.fs.Base())
|
||||
|
||||
if s.cdn.PluginSupported(n.JsonData.ID) {
|
||||
return s.cdn.NewCDNURLConstructor(n.JsonData.ID, n.JsonData.Info.Version).StringPath(pathStr)
|
||||
}
|
||||
if n.Parent != nil {
|
||||
if s.cdn.PluginSupported(n.Parent.JsonData.ID) {
|
||||
relPath, err := n.Parent.FS.Rel(n.FS.Base())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return s.cdn.AssetURL(n.parent.pluginJSON.ID, n.parent.pluginJSON.Info.Version, path.Join(relPath, pathStr))
|
||||
return s.cdn.AssetURL(n.Parent.JsonData.ID, n.Parent.JsonData.Info.Version, path.Join(relPath, pathStr))
|
||||
}
|
||||
}
|
||||
// Local
|
||||
@@ -120,7 +117,7 @@ func (s *Service) DefaultLogoPath(pluginType plugins.Type) string {
|
||||
return path.Join("public/img", fmt.Sprintf("icn-%s.svg", string(pluginType)))
|
||||
}
|
||||
|
||||
func (s *Service) GetTranslations(n PluginInfo) (map[string]string, error) {
|
||||
func (s *Service) GetTranslations(n pluginassets.PluginInfo) (map[string]string, error) {
|
||||
pathToTranslations, err := s.RelativeURL(n, "locales")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get locales: %w", err)
|
||||
@@ -128,8 +125,8 @@ func (s *Service) GetTranslations(n PluginInfo) (map[string]string, error) {
|
||||
|
||||
// loop through all the languages specified in the plugin.json and add them to the list
|
||||
translations := map[string]string{}
|
||||
for _, language := range n.pluginJSON.Languages {
|
||||
file := fmt.Sprintf("%s.json", n.pluginJSON.ID)
|
||||
for _, language := range n.JsonData.Languages {
|
||||
file := fmt.Sprintf("%s.json", n.JsonData.ID)
|
||||
translations[language], err = url.JoinPath(pathToTranslations, language, file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("join path: %w", err)
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/fakes"
|
||||
"github.com/grafana/grafana/pkg/plugins/pluginassets"
|
||||
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
|
||||
)
|
||||
|
||||
@@ -44,7 +45,7 @@ func TestService(t *testing.T) {
|
||||
"two": {},
|
||||
},
|
||||
}
|
||||
svc := ProvideService(cfg, pluginscdn.ProvideService(cfg))
|
||||
svc := ProvideService(cfg, pluginscdn.ProvideService(cfg), fakes.NewFakeAssetProvider())
|
||||
|
||||
tableOldFS := fakes.NewFakePluginFS("/grafana/public/app/plugins/panel/table-old")
|
||||
jsonData := map[string]plugins.JSONData{
|
||||
@@ -61,22 +62,22 @@ func TestService(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Base", func(t *testing.T) {
|
||||
base, err := svc.Base(NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil))
|
||||
base, err := svc.Base(pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil))
|
||||
require.NoError(t, err)
|
||||
|
||||
oneCDNURL, err := url.JoinPath(tc.cdnBaseURL, "/one/1.0.0/public/plugins/one")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, oneCDNURL, base)
|
||||
|
||||
base, err = svc.Base(NewPluginInfo(jsonData["one"], plugins.ClassCDN, pluginFS(oneCDNURL), nil))
|
||||
base, err = svc.Base(pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassCDN, pluginFS(oneCDNURL), nil))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, oneCDNURL, base)
|
||||
|
||||
base, err = svc.Base(NewPluginInfo(jsonData["two"], plugins.ClassExternal, pluginFS("two"), nil))
|
||||
base, err = svc.Base(pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, pluginFS("two"), nil))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "public/plugins/two", base)
|
||||
|
||||
base, err = svc.Base(NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS, nil))
|
||||
base, err = svc.Base(pluginassets.NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS, nil))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "public/plugins/table-old", base)
|
||||
|
||||
@@ -84,8 +85,8 @@ func TestService(t *testing.T) {
|
||||
parentFS.RelFunc = func(_ string) (string, error) {
|
||||
return "child-plugins/two", nil
|
||||
}
|
||||
parent := NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil)
|
||||
child := NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent)
|
||||
parent := pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil)
|
||||
child := pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent)
|
||||
base, err = svc.Base(child)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -95,7 +96,7 @@ func TestService(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Module", func(t *testing.T) {
|
||||
module, err := svc.Module(NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil))
|
||||
module, err := svc.Module(pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil))
|
||||
require.NoError(t, err)
|
||||
|
||||
oneCDNURL, err := url.JoinPath(tc.cdnBaseURL, "/one/1.0.0/public/plugins/one")
|
||||
@@ -106,15 +107,15 @@ func TestService(t *testing.T) {
|
||||
require.Equal(t, oneCDNModuleURL, module)
|
||||
|
||||
fs := pluginFS("one")
|
||||
module, err = svc.Module(NewPluginInfo(jsonData["one"], plugins.ClassCDN, fs, nil))
|
||||
module, err = svc.Module(pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassCDN, fs, nil))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, path.Join(fs.Base(), "module.js"), module)
|
||||
|
||||
module, err = svc.Module(NewPluginInfo(jsonData["two"], plugins.ClassExternal, pluginFS("two"), nil))
|
||||
module, err = svc.Module(pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, pluginFS("two"), nil))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "public/plugins/two/module.js", module)
|
||||
|
||||
module, err = svc.Module(NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS, nil))
|
||||
module, err = svc.Module(pluginassets.NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS, nil))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "core:plugin/table-old", module)
|
||||
|
||||
@@ -122,8 +123,8 @@ func TestService(t *testing.T) {
|
||||
parentFS.RelFunc = func(_ string) (string, error) {
|
||||
return "child-plugins/two", nil
|
||||
}
|
||||
parent := NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil)
|
||||
child := NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent)
|
||||
parent := pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil)
|
||||
child := pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent)
|
||||
module, err = svc.Module(child)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -142,29 +143,29 @@ func TestService(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
u, err := svc.RelativeURL(NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil), "")
|
||||
u, err := svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil), "")
|
||||
require.NoError(t, err)
|
||||
// given an empty path, base URL will be returned
|
||||
baseURL, err := svc.Base(NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil))
|
||||
baseURL, err := svc.Base(pluginassets.NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, baseURL, u)
|
||||
|
||||
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil), "path/to/file.txt")
|
||||
u, err = svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassExternal, pluginFS("one"), nil), "path/to/file.txt")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, strings.TrimRight(tc.cdnBaseURL, "/")+"/one/1.0.0/public/plugins/one/path/to/file.txt", u)
|
||||
|
||||
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, pluginFS("two"), nil), "path/to/file.txt")
|
||||
u, err = svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, pluginFS("two"), nil), "path/to/file.txt")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "public/plugins/two/path/to/file.txt", u)
|
||||
|
||||
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, pluginFS("two"), nil), "default")
|
||||
u, err = svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, pluginFS("two"), nil), "default")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "public/plugins/two/default", u)
|
||||
|
||||
oneCDNURL, err := url.JoinPath(tc.cdnBaseURL, "/one/1.0.0/public/plugins/one")
|
||||
require.NoError(t, err)
|
||||
|
||||
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassCDN, pluginFS(oneCDNURL), nil), "path/to/file.txt")
|
||||
u, err = svc.RelativeURL(pluginassets.NewPluginInfo(pluginsMap["one"].JSONData, plugins.ClassCDN, pluginFS(oneCDNURL), nil), "path/to/file.txt")
|
||||
require.NoError(t, err)
|
||||
|
||||
oneCDNRelativeURL, err := url.JoinPath(oneCDNURL, "path/to/file.txt")
|
||||
@@ -175,8 +176,8 @@ func TestService(t *testing.T) {
|
||||
parentFS.RelFunc = func(_ string) (string, error) {
|
||||
return "child-plugins/two", nil
|
||||
}
|
||||
parent := NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil)
|
||||
child := NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent)
|
||||
parent := pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, parentFS, nil)
|
||||
child := pluginassets.NewPluginInfo(jsonData["two"], plugins.ClassExternal, fakes.NewFakePluginFS(""), &parent)
|
||||
u, err = svc.RelativeURL(child, "path/to/file.txt")
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -192,8 +193,8 @@ func TestService(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("GetTranslations", func(t *testing.T) {
|
||||
pluginInfo := NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil)
|
||||
pluginInfo.pluginJSON.Languages = []string{"en-US", "pt-BR"}
|
||||
pluginInfo := pluginassets.NewPluginInfo(jsonData["one"], plugins.ClassExternal, pluginFS("one"), nil)
|
||||
pluginInfo.JsonData.Languages = []string{"en-US", "pt-BR"}
|
||||
translations, err := svc.GetTranslations(pluginInfo)
|
||||
require.NoError(t, err)
|
||||
oneCDNURL, err := url.JoinPath(tc.cdnBaseURL, "one", "1.0.0", "public", "plugins", "one")
|
||||
@@ -219,14 +220,14 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
tcs := []struct {
|
||||
name string
|
||||
cfg *config.PluginManagementCfg
|
||||
pluginInfo func() PluginInfo
|
||||
pluginInfo func() pluginassets.PluginInfo
|
||||
expected expected
|
||||
}{
|
||||
{
|
||||
name: "Local FS external plugin",
|
||||
cfg: &config.PluginManagementCfg{},
|
||||
pluginInfo: func() PluginInfo {
|
||||
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
pluginInfo: func() pluginassets.PluginInfo {
|
||||
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
},
|
||||
expected: expected{
|
||||
module: "public/plugins/parent/module.js",
|
||||
@@ -237,9 +238,9 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
{
|
||||
name: "Local FS external plugin with child",
|
||||
cfg: &config.PluginManagementCfg{},
|
||||
pluginInfo: func() PluginInfo {
|
||||
parentInfo := NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
childInfo := NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent/child"), &parentInfo)
|
||||
pluginInfo: func() pluginassets.PluginInfo {
|
||||
parentInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
childInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent/child"), &parentInfo)
|
||||
return childInfo
|
||||
},
|
||||
expected: expected{
|
||||
@@ -251,8 +252,8 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
{
|
||||
name: "Local FS core plugin",
|
||||
cfg: &config.PluginManagementCfg{},
|
||||
pluginInfo: func() PluginInfo {
|
||||
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCore, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
pluginInfo: func() pluginassets.PluginInfo {
|
||||
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCore, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
},
|
||||
expected: expected{
|
||||
module: "core:plugin/parent",
|
||||
@@ -263,8 +264,8 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
{
|
||||
name: "Externally-built Local FS core plugin",
|
||||
cfg: &config.PluginManagementCfg{},
|
||||
pluginInfo: func() PluginInfo {
|
||||
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCore, plugins.NewLocalFS("/plugins/parent/dist"), nil)
|
||||
pluginInfo: func() pluginassets.PluginInfo {
|
||||
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCore, plugins.NewLocalFS("/plugins/parent/dist"), nil)
|
||||
},
|
||||
expected: expected{
|
||||
module: "public/plugins/parent/module.js",
|
||||
@@ -277,8 +278,8 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
cfg: &config.PluginManagementCfg{
|
||||
PluginsCDNURLTemplate: "https://cdn.example.com",
|
||||
},
|
||||
pluginInfo: func() PluginInfo {
|
||||
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/plugins/parent"), nil)
|
||||
pluginInfo: func() pluginassets.PluginInfo {
|
||||
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/plugins/parent"), nil)
|
||||
},
|
||||
expected: expected{
|
||||
module: "https://cdn.example.com/plugins/parent/module.js",
|
||||
@@ -291,10 +292,10 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
cfg: &config.PluginManagementCfg{
|
||||
PluginsCDNURLTemplate: "https://cdn.example.com",
|
||||
},
|
||||
pluginInfo: func() PluginInfo {
|
||||
pluginInfo: func() pluginassets.PluginInfo {
|
||||
// Note: fake plugin FS is the most convenient way to mock the plugin FS for CDN plugins
|
||||
parentInfo := NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/parent"), nil)
|
||||
childInfo := NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/parent/some/other/dir/child"), &parentInfo)
|
||||
parentInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/parent"), nil)
|
||||
childInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassCDN, pluginFS("https://cdn.example.com/parent/some/other/dir/child"), &parentInfo)
|
||||
return childInfo
|
||||
},
|
||||
expected: expected{
|
||||
@@ -311,8 +312,8 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
"parent": {"cdn": "true"},
|
||||
},
|
||||
},
|
||||
pluginInfo: func() PluginInfo {
|
||||
return NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
pluginInfo: func() pluginassets.PluginInfo {
|
||||
return pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
},
|
||||
expected: expected{
|
||||
module: "https://cdn.example.com/parent/1.0.0/public/plugins/parent/module.js",
|
||||
@@ -328,9 +329,9 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
"parent": {"cdn": "true"},
|
||||
},
|
||||
},
|
||||
pluginInfo: func() PluginInfo {
|
||||
parentInfo := NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
childInfo := NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent/child"), &parentInfo)
|
||||
pluginInfo: func() pluginassets.PluginInfo {
|
||||
parentInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "parent", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent"), nil)
|
||||
childInfo := pluginassets.NewPluginInfo(plugins.JSONData{ID: "child", Info: plugins.Info{Version: "1.0.0"}}, plugins.ClassExternal, plugins.NewLocalFS("/plugins/parent/child"), &parentInfo)
|
||||
return childInfo
|
||||
},
|
||||
expected: expected{
|
||||
@@ -342,7 +343,7 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
}
|
||||
for _, tc := range tcs {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
svc := ProvideService(tc.cfg, pluginscdn.ProvideService(tc.cfg))
|
||||
svc := ProvideService(tc.cfg, pluginscdn.ProvideService(tc.cfg), fakes.NewFakeAssetProvider())
|
||||
|
||||
module, err := svc.Module(tc.pluginInfo())
|
||||
require.NoError(t, err)
|
||||
@@ -358,3 +359,72 @@ func TestService_ChildPlugins(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_AssetProviderPrecedence(t *testing.T) {
|
||||
cfg := &config.PluginManagementCfg{
|
||||
PluginsCDNURLTemplate: "https://cdn.example.com",
|
||||
PluginSettings: map[string]map[string]string{
|
||||
"test-plugin": {"cdn": "true"},
|
||||
},
|
||||
Features: config.Features{PluginAssetProvider: true},
|
||||
}
|
||||
cdn := pluginscdn.ProvideService(cfg)
|
||||
|
||||
pluginInfo := pluginassets.NewPluginInfo(
|
||||
plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}},
|
||||
plugins.ClassExternal,
|
||||
pluginFS("/plugins/test-plugin"),
|
||||
nil,
|
||||
)
|
||||
|
||||
t.Run("Asset provider enabled takes precedence when feature enabled", func(t *testing.T) {
|
||||
assetProvider := fakes.NewFakeAssetProvider()
|
||||
assetProvider.AssetPathFunc = func(n pluginassets.PluginInfo, pathElems ...string) (string, error) {
|
||||
return "from-asset-provider", nil
|
||||
}
|
||||
assetProvider.ModuleFunc = func(n pluginassets.PluginInfo) (string, error) {
|
||||
return "module-from-asset-provider", nil
|
||||
}
|
||||
|
||||
svc := ProvideService(cfg, cdn, assetProvider)
|
||||
|
||||
base, err := svc.Base(pluginInfo)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "from-asset-provider", base)
|
||||
|
||||
module, err := svc.Module(pluginInfo)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "module-from-asset-provider", module)
|
||||
|
||||
relURL, err := svc.RelativeURL(pluginInfo, "path/to/file.txt")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "from-asset-provider", relURL)
|
||||
})
|
||||
|
||||
t.Run("GetTranslations uses asset provider when feature enabled", func(t *testing.T) {
|
||||
assetProvider := fakes.NewFakeAssetProvider()
|
||||
assetProvider.AssetPathFunc = func(n pluginassets.PluginInfo, pathElems ...string) (string, error) {
|
||||
return path.Join("translation-path", path.Join(pathElems...)), nil
|
||||
}
|
||||
|
||||
pluginWithLangs := pluginassets.NewPluginInfo(
|
||||
plugins.JSONData{
|
||||
ID: "multilang-plugin",
|
||||
Info: plugins.Info{Version: "1.0.0"},
|
||||
Languages: []string{"en-US", "es-ES", "fr-FR"},
|
||||
},
|
||||
plugins.ClassExternal,
|
||||
pluginFS("/plugins/multilang-plugin"),
|
||||
nil,
|
||||
)
|
||||
|
||||
svc := ProvideService(cfg, cdn, assetProvider)
|
||||
translations, err := svc.GetTranslations(pluginWithLangs)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, translations, 3)
|
||||
// All translation paths should come from the asset provider
|
||||
require.Equal(t, "translation-path/locales/en-US/multilang-plugin.json", translations["en-US"])
|
||||
require.Equal(t, "translation-path/locales/es-ES/multilang-plugin.json", translations["es-ES"])
|
||||
require.Equal(t, "translation-path/locales/fr-FR/multilang-plugin.json", translations["fr-FR"])
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/loader/assetpath"
|
||||
"github.com/grafana/grafana/pkg/plugins/pluginassets"
|
||||
)
|
||||
|
||||
type pluginFactoryFunc func(p *plugins.FoundBundle, pluginClass plugins.Class, sig plugins.Signature) (*plugins.Plugin, error)
|
||||
@@ -27,7 +28,7 @@ func NewDefaultPluginFactory(features *config.Features, assetPath *assetpath.Ser
|
||||
|
||||
func (f *DefaultPluginFactory) createPlugin(bundle *plugins.FoundBundle, class plugins.Class,
|
||||
sig plugins.Signature) (*plugins.Plugin, error) {
|
||||
parentInfo := assetpath.NewPluginInfo(bundle.Primary.JSONData, class, bundle.Primary.FS, nil)
|
||||
parentInfo := pluginassets.NewPluginInfo(bundle.Primary.JSONData, class, bundle.Primary.FS, nil)
|
||||
plugin, err := f.newPlugin(bundle.Primary, class, sig, parentInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -39,7 +40,7 @@ func (f *DefaultPluginFactory) createPlugin(bundle *plugins.FoundBundle, class p
|
||||
|
||||
plugin.Children = make([]*plugins.Plugin, 0, len(bundle.Children))
|
||||
for _, child := range bundle.Children {
|
||||
childInfo := assetpath.NewPluginInfo(child.JSONData, class, child.FS, &parentInfo)
|
||||
childInfo := pluginassets.NewPluginInfo(child.JSONData, class, child.FS, &parentInfo)
|
||||
cp, err := f.newPlugin(*child, class, sig, childInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -52,7 +53,7 @@ func (f *DefaultPluginFactory) createPlugin(bundle *plugins.FoundBundle, class p
|
||||
}
|
||||
|
||||
func (f *DefaultPluginFactory) newPlugin(p plugins.FoundPlugin, class plugins.Class, sig plugins.Signature,
|
||||
info assetpath.PluginInfo) (*plugins.Plugin, error) {
|
||||
info pluginassets.PluginInfo) (*plugins.Plugin, error) {
|
||||
baseURL, err := f.assetPath.Base(info)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("base url: %w", err)
|
||||
@@ -86,7 +87,7 @@ func (f *DefaultPluginFactory) newPlugin(p plugins.FoundPlugin, class plugins.Cl
|
||||
return plugin, nil
|
||||
}
|
||||
|
||||
func setImages(p *plugins.Plugin, assetPath *assetpath.Service, info assetpath.PluginInfo) error {
|
||||
func setImages(p *plugins.Plugin, assetPath *assetpath.Service, info pluginassets.PluginInfo) error {
|
||||
var err error
|
||||
for _, dst := range []*string{&p.Info.Logos.Small, &p.Info.Logos.Large} {
|
||||
if len(*dst) == 0 {
|
||||
@@ -109,7 +110,7 @@ func setImages(p *plugins.Plugin, assetPath *assetpath.Service, info assetpath.P
|
||||
return nil
|
||||
}
|
||||
|
||||
func setTranslations(p *plugins.Plugin, assetPath *assetpath.Service, info assetpath.PluginInfo) error {
|
||||
func setTranslations(p *plugins.Plugin, assetPath *assetpath.Service, info pluginassets.PluginInfo) error {
|
||||
translations, err := assetPath.GetTranslations(info)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set translations: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user