mirror of https://github.com/grafana/grafana
Plugins: Refactor loader + finder to support multiple sourcing methods (#64735)
* it's cdn time * tidy body closing * auto signed * fix close * update log name * remove commentspull/65047/head
parent
eba2c7b522
commit
ee2dd62a1f
@ -1,44 +0,0 @@ |
||||
package finder |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/plugins" |
||||
"github.com/grafana/grafana/pkg/plugins/log" |
||||
) |
||||
|
||||
type Service struct { |
||||
local *FS |
||||
log log.Logger |
||||
} |
||||
|
||||
func NewService() *Service { |
||||
logger := log.New("plugin.finder") |
||||
return &Service{ |
||||
local: newFS(logger), |
||||
log: logger, |
||||
} |
||||
} |
||||
|
||||
func (f *Service) Find(ctx context.Context, pluginPaths ...string) ([]*plugins.FoundBundle, error) { |
||||
if len(pluginPaths) == 0 { |
||||
return []*plugins.FoundBundle{}, nil |
||||
} |
||||
|
||||
fbs := make(map[string][]*plugins.FoundBundle) |
||||
for _, path := range pluginPaths { |
||||
local, err := f.local.Find(ctx, path) |
||||
if err != nil { |
||||
f.log.Warn("Error occurred when trying to find plugin", "path", path) |
||||
continue |
||||
} |
||||
fbs[path] = local |
||||
} |
||||
|
||||
var found []*plugins.FoundBundle |
||||
for _, fb := range fbs { |
||||
found = append(found, fb...) |
||||
} |
||||
|
||||
return found, nil |
||||
} |
||||
@ -0,0 +1,47 @@ |
||||
package finder |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"io" |
||||
|
||||
"github.com/grafana/grafana/pkg/plugins" |
||||
"github.com/grafana/grafana/pkg/services/org" |
||||
) |
||||
|
||||
func ReadPluginJSON(reader io.Reader) (plugins.JSONData, error) { |
||||
plugin := plugins.JSONData{} |
||||
if err := json.NewDecoder(reader).Decode(&plugin); err != nil { |
||||
return plugins.JSONData{}, err |
||||
} |
||||
|
||||
if err := validatePluginJSON(plugin); err != nil { |
||||
return plugins.JSONData{}, err |
||||
} |
||||
|
||||
if plugin.ID == "grafana-piechart-panel" { |
||||
plugin.Name = "Pie Chart (old)" |
||||
} |
||||
|
||||
if len(plugin.Dependencies.Plugins) == 0 { |
||||
plugin.Dependencies.Plugins = []plugins.Dependency{} |
||||
} |
||||
|
||||
if plugin.Dependencies.GrafanaVersion == "" { |
||||
plugin.Dependencies.GrafanaVersion = "*" |
||||
} |
||||
|
||||
for _, include := range plugin.Includes { |
||||
if include.Role == "" { |
||||
include.Role = org.RoleViewer |
||||
} |
||||
} |
||||
|
||||
return plugin, nil |
||||
} |
||||
|
||||
func validatePluginJSON(data plugins.JSONData) error { |
||||
if data.ID == "" || !data.Type.IsValid() { |
||||
return ErrInvalidPluginJSON |
||||
} |
||||
return nil |
||||
} |
||||
@ -0,0 +1,38 @@ |
||||
package sources |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/plugins" |
||||
) |
||||
|
||||
type LocalSource struct { |
||||
paths []string |
||||
class plugins.Class |
||||
} |
||||
|
||||
func NewLocalSource(class plugins.Class, paths []string) *LocalSource { |
||||
return &LocalSource{ |
||||
class: class, |
||||
paths: paths, |
||||
} |
||||
} |
||||
|
||||
func (s *LocalSource) PluginClass(_ context.Context) plugins.Class { |
||||
return s.class |
||||
} |
||||
|
||||
func (s *LocalSource) PluginURIs(_ context.Context) []string { |
||||
return s.paths |
||||
} |
||||
|
||||
func (s *LocalSource) DefaultSignature(_ context.Context) (plugins.Signature, bool) { |
||||
switch s.class { |
||||
case plugins.Core: |
||||
return plugins.Signature{ |
||||
Status: plugins.SignatureInternal, |
||||
}, true |
||||
default: |
||||
return plugins.Signature{}, false |
||||
} |
||||
} |
||||
Loading…
Reference in new issue