2021-03-17 10:06:10 -05:00
|
|
|
package api
|
|
|
|
|
2021-11-17 05:04:22 -06:00
|
|
|
import (
|
|
|
|
"context"
|
2022-09-09 02:44:50 -05:00
|
|
|
"time"
|
2021-11-17 05:04:22 -06:00
|
|
|
|
2022-09-09 02:44:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-11-17 05:04:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2022-09-09 02:44:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
2021-11-17 05:04:22 -06:00
|
|
|
)
|
2021-03-17 10:06:10 -05:00
|
|
|
|
2022-07-06 09:22:59 -05:00
|
|
|
type fakePluginManager struct {
|
2022-08-30 10:30:43 -05:00
|
|
|
plugins.Manager
|
|
|
|
|
2022-07-06 09:22:59 -05:00
|
|
|
plugins map[string]fakePlugin
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakePlugin struct {
|
|
|
|
pluginID string
|
|
|
|
version string
|
|
|
|
}
|
|
|
|
|
2022-08-23 04:50:50 -05:00
|
|
|
func (pm *fakePluginManager) Add(_ context.Context, pluginID, version string, _ plugins.CompatOpts) error {
|
2022-07-06 09:22:59 -05:00
|
|
|
pm.plugins[pluginID] = fakePlugin{
|
|
|
|
pluginID: pluginID,
|
|
|
|
version: version,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *fakePluginManager) Remove(_ context.Context, pluginID string) error {
|
|
|
|
delete(pm.plugins, pluginID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
type fakePluginStore struct {
|
|
|
|
plugins.Store
|
2021-11-17 05:04:22 -06:00
|
|
|
|
|
|
|
plugins map[string]plugins.PluginDTO
|
2021-03-17 10:06:10 -05:00
|
|
|
}
|
|
|
|
|
2021-11-17 05:04:22 -06:00
|
|
|
func (pr fakePluginStore) Plugin(_ context.Context, pluginID string) (plugins.PluginDTO, bool) {
|
|
|
|
p, exists := pr.plugins[pluginID]
|
|
|
|
|
|
|
|
return p, exists
|
2021-03-17 10:06:10 -05:00
|
|
|
}
|
|
|
|
|
2021-11-17 05:04:22 -06:00
|
|
|
func (pr fakePluginStore) Plugins(_ context.Context, pluginTypes ...plugins.Type) []plugins.PluginDTO {
|
|
|
|
var result []plugins.PluginDTO
|
2022-09-09 02:44:50 -05:00
|
|
|
if len(pluginTypes) == 0 {
|
|
|
|
pluginTypes = plugins.PluginTypes
|
|
|
|
}
|
2021-11-17 05:04:22 -06:00
|
|
|
for _, v := range pr.plugins {
|
|
|
|
for _, t := range pluginTypes {
|
|
|
|
if v.Type == t {
|
|
|
|
result = append(result, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2021-03-17 10:06:10 -05:00
|
|
|
}
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
type fakeRendererManager struct {
|
|
|
|
plugins.RendererManager
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:20:10 -05:00
|
|
|
func (ps *fakeRendererManager) Renderer(_ context.Context) *plugins.Plugin {
|
2021-03-17 10:06:10 -05:00
|
|
|
return nil
|
|
|
|
}
|
2021-03-18 07:53:01 -05:00
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
type fakePluginStaticRouteResolver struct {
|
|
|
|
plugins.StaticRouteResolver
|
|
|
|
|
|
|
|
routes []*plugins.StaticRoute
|
|
|
|
}
|
|
|
|
|
|
|
|
func (psrr *fakePluginStaticRouteResolver) Routes() []*plugins.StaticRoute {
|
|
|
|
return psrr.routes
|
2021-03-18 07:53:01 -05:00
|
|
|
}
|
2022-09-09 02:44:50 -05:00
|
|
|
|
|
|
|
type fakePluginSettings struct {
|
|
|
|
pluginsettings.Service
|
|
|
|
|
|
|
|
plugins map[string]*pluginsettings.DTO
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPluginSettings returns all Plugin Settings for the provided Org
|
2022-09-21 05:20:11 -05:00
|
|
|
func (ps *fakePluginSettings) GetPluginSettings(_ context.Context, _ *pluginsettings.GetArgs) ([]*pluginsettings.InfoDTO, error) {
|
|
|
|
res := []*pluginsettings.InfoDTO{}
|
2022-09-09 02:44:50 -05:00
|
|
|
for _, dto := range ps.plugins {
|
2022-09-21 05:20:11 -05:00
|
|
|
res = append(res, &pluginsettings.InfoDTO{
|
|
|
|
PluginID: dto.PluginID,
|
|
|
|
OrgID: dto.OrgID,
|
|
|
|
Enabled: dto.Enabled,
|
|
|
|
Pinned: dto.Pinned,
|
|
|
|
PluginVersion: dto.PluginVersion,
|
|
|
|
})
|
2022-09-09 02:44:50 -05:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPluginSettingByPluginID returns a Plugin Settings by Plugin ID
|
|
|
|
func (ps *fakePluginSettings) GetPluginSettingByPluginID(ctx context.Context, args *pluginsettings.GetByPluginIDArgs) (*pluginsettings.DTO, error) {
|
|
|
|
if res, ok := ps.plugins[args.PluginID]; ok {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
return nil, models.ErrPluginSettingNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePluginSetting updates a Plugin Setting
|
|
|
|
func (ps *fakePluginSettings) UpdatePluginSetting(ctx context.Context, args *pluginsettings.UpdateArgs) error {
|
|
|
|
var secureData map[string][]byte
|
|
|
|
if args.SecureJSONData != nil {
|
|
|
|
secureData := map[string][]byte{}
|
|
|
|
for k, v := range args.SecureJSONData {
|
|
|
|
secureData[k] = ([]byte)(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// save
|
|
|
|
ps.plugins[args.PluginID] = &pluginsettings.DTO{
|
|
|
|
ID: int64(len(ps.plugins)),
|
|
|
|
OrgID: args.OrgID,
|
|
|
|
PluginID: args.PluginID,
|
|
|
|
PluginVersion: args.PluginVersion,
|
|
|
|
JSONData: args.JSONData,
|
|
|
|
SecureJSONData: secureData,
|
|
|
|
Enabled: args.Enabled,
|
|
|
|
Pinned: args.Pinned,
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePluginSettingPluginVersion updates a Plugin Setting's plugin version
|
|
|
|
func (ps *fakePluginSettings) UpdatePluginSettingPluginVersion(ctx context.Context, args *pluginsettings.UpdatePluginVersionArgs) error {
|
|
|
|
if res, ok := ps.plugins[args.PluginID]; ok {
|
|
|
|
res.PluginVersion = args.PluginVersion
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return models.ErrPluginSettingNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptedValues decrypts the encrypted secureJSONData of the provided plugin setting and
|
|
|
|
// returns the decrypted values.
|
|
|
|
func (ps *fakePluginSettings) DecryptedValues(dto *pluginsettings.DTO) map[string]string {
|
|
|
|
// TODO: Implement
|
|
|
|
return nil
|
|
|
|
}
|