mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 10:02:33 +08:00
chore: move plugins models into pluginsettings svc (#61944)
This commit is contained in:
@ -15,6 +15,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
"github.com/grafana/grafana/pkg/api/response"
|
"github.com/grafana/grafana/pkg/api/response"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@ -202,7 +203,7 @@ func (hs *HTTPServer) GetPluginSettingByID(c *models.ReqContext) response.Respon
|
|||||||
OrgID: c.OrgID,
|
OrgID: c.OrgID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(err, models.ErrPluginSettingNotFound) {
|
if !errors.Is(err, pluginsettings.ErrPluginSettingNotFound) {
|
||||||
return response.Error(http.StatusInternalServerError, "Failed to get plugin settings", nil)
|
return response.Error(http.StatusInternalServerError, "Failed to get plugin settings", nil)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -227,7 +228,7 @@ func (hs *HTTPServer) GetPluginSettingByID(c *models.ReqContext) response.Respon
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (hs *HTTPServer) UpdatePluginSetting(c *models.ReqContext) response.Response {
|
func (hs *HTTPServer) UpdatePluginSetting(c *models.ReqContext) response.Response {
|
||||||
cmd := models.UpdatePluginSettingCmd{}
|
cmd := pluginsettings.UpdatePluginSettingCmd{}
|
||||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||||
}
|
}
|
||||||
|
@ -1,70 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrPluginSettingNotFound = errors.New("plugin setting not found")
|
|
||||||
)
|
|
||||||
|
|
||||||
type PluginSetting struct {
|
|
||||||
Id int64
|
|
||||||
PluginId string
|
|
||||||
OrgId int64
|
|
||||||
Enabled bool
|
|
||||||
Pinned bool
|
|
||||||
JsonData map[string]interface{}
|
|
||||||
SecureJsonData map[string][]byte
|
|
||||||
PluginVersion string
|
|
||||||
|
|
||||||
Created time.Time
|
|
||||||
Updated time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
type PluginSettingInfo struct {
|
|
||||||
PluginID string `xorm:"plugin_id"`
|
|
||||||
OrgID int64 `xorm:"org_id"`
|
|
||||||
Enabled bool `xorm:"enabled"`
|
|
||||||
Pinned bool `xorm:"pinned"`
|
|
||||||
PluginVersion string `xorm:"plugin_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------
|
|
||||||
// COMMANDS
|
|
||||||
|
|
||||||
// Also acts as api DTO
|
|
||||||
type UpdatePluginSettingCmd struct {
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
Pinned bool `json:"pinned"`
|
|
||||||
JsonData map[string]interface{} `json:"jsonData"`
|
|
||||||
SecureJsonData map[string]string `json:"secureJsonData"`
|
|
||||||
PluginVersion string `json:"version"`
|
|
||||||
|
|
||||||
PluginId string `json:"-"`
|
|
||||||
OrgId int64 `json:"-"`
|
|
||||||
EncryptedSecureJsonData map[string][]byte `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// specific command, will only update version
|
|
||||||
type UpdatePluginSettingVersionCmd struct {
|
|
||||||
PluginVersion string
|
|
||||||
PluginId string `json:"-"`
|
|
||||||
OrgId int64 `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------
|
|
||||||
// QUERIES
|
|
||||||
|
|
||||||
type GetPluginSettingByIdQuery struct {
|
|
||||||
PluginId string
|
|
||||||
OrgId int64
|
|
||||||
Result *PluginSetting
|
|
||||||
}
|
|
||||||
|
|
||||||
type PluginStateChangedEvent struct {
|
|
||||||
PluginId string
|
|
||||||
OrgId int64
|
|
||||||
Enabled bool
|
|
||||||
}
|
|
@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/localcache"
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/plugins/adapters"
|
"github.com/grafana/grafana/pkg/plugins/adapters"
|
||||||
"github.com/grafana/grafana/pkg/services/datasources"
|
"github.com/grafana/grafana/pkg/services/datasources"
|
||||||
@ -80,9 +79,9 @@ func (p *Provider) pluginContext(ctx context.Context, pluginID string, user *use
|
|||||||
|
|
||||||
ps, err := p.getCachedPluginSettings(ctx, pluginID, user)
|
ps, err := p.getCachedPluginSettings(ctx, pluginID, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// models.ErrPluginSettingNotFound is expected if there's no row found for plugin setting in database (if non-app plugin).
|
// pluginsettings.ErrPluginSettingNotFound is expected if there's no row found for plugin setting in database (if non-app plugin).
|
||||||
// If it's not this expected error something is wrong with cache or database and we return the error to the client.
|
// If it's not this expected error something is wrong with cache or database and we return the error to the client.
|
||||||
if !errors.Is(err, models.ErrPluginSettingNotFound) {
|
if !errors.Is(err, pluginsettings.ErrPluginSettingNotFound) {
|
||||||
return backend.PluginContext{}, false, fmt.Errorf("%v: %w", "Failed to get plugin settings", err)
|
return backend.PluginContext{}, false, fmt.Errorf("%v: %w", "Failed to get plugin settings", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||||
@ -132,7 +131,7 @@ func (du *DashboardUpdater) syncPluginDashboards(ctx context.Context, plugin plu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (du *DashboardUpdater) handlePluginStateChanged(ctx context.Context, event *models.PluginStateChangedEvent) error {
|
func (du *DashboardUpdater) handlePluginStateChanged(ctx context.Context, event *pluginsettings.PluginStateChangedEvent) error {
|
||||||
du.logger.Info("Plugin state changed", "pluginId", event.PluginId, "enabled", event.Enabled)
|
du.logger.Info("Plugin state changed", "pluginId", event.PluginId, "enabled", event.Enabled)
|
||||||
|
|
||||||
if event.Enabled {
|
if event.Enabled {
|
||||||
|
@ -5,9 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||||
@ -15,7 +16,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings/service"
|
"github.com/grafana/grafana/pkg/services/pluginsettings/service"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDashboardUpdater(t *testing.T) {
|
func TestDashboardUpdater(t *testing.T) {
|
||||||
@ -202,7 +202,7 @@ func TestDashboardUpdater(t *testing.T) {
|
|||||||
t.Run("handlePluginStateChanged", func(t *testing.T) {
|
t.Run("handlePluginStateChanged", func(t *testing.T) {
|
||||||
scenario(t, "When app plugin is disabled that doesn't have any imported dashboards shouldn't delete any",
|
scenario(t, "When app plugin is disabled that doesn't have any imported dashboards shouldn't delete any",
|
||||||
scenarioInput{}, func(ctx *scenarioContext) {
|
scenarioInput{}, func(ctx *scenarioContext) {
|
||||||
err := ctx.bus.Publish(context.Background(), &models.PluginStateChangedEvent{
|
err := ctx.bus.Publish(context.Background(), &pluginsettings.PluginStateChangedEvent{
|
||||||
PluginId: "test",
|
PluginId: "test",
|
||||||
OrgId: 2,
|
OrgId: 2,
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
@ -250,7 +250,7 @@ func TestDashboardUpdater(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, func(ctx *scenarioContext) {
|
}, func(ctx *scenarioContext) {
|
||||||
err := ctx.bus.Publish(context.Background(), &models.PluginStateChangedEvent{
|
err := ctx.bus.Publish(context.Background(), &pluginsettings.PluginStateChangedEvent{
|
||||||
PluginId: "test",
|
PluginId: "test",
|
||||||
OrgId: 2,
|
OrgId: 2,
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
@ -307,7 +307,7 @@ func TestDashboardUpdater(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, func(ctx *scenarioContext) {
|
}, func(ctx *scenarioContext) {
|
||||||
err := ctx.bus.Publish(context.Background(), &models.PluginStateChangedEvent{
|
err := ctx.bus.Publish(context.Background(), &pluginsettings.PluginStateChangedEvent{
|
||||||
PluginId: "test",
|
PluginId: "test",
|
||||||
OrgId: 2,
|
OrgId: 2,
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
@ -478,8 +478,8 @@ type scenarioContext struct {
|
|||||||
dashboardPluginService *dashboardPluginServiceMock
|
dashboardPluginService *dashboardPluginServiceMock
|
||||||
dashboardService *dashboardServiceMock
|
dashboardService *dashboardServiceMock
|
||||||
importDashboardArgs []*dashboardimport.ImportDashboardRequest
|
importDashboardArgs []*dashboardimport.ImportDashboardRequest
|
||||||
getPluginSettingsByIdArgs []*models.GetPluginSettingByIdQuery
|
getPluginSettingsByIdArgs []*pluginsettings.GetPluginSettingByIdQuery
|
||||||
updatePluginSettingVersionArgs []*models.UpdatePluginSettingVersionCmd
|
updatePluginSettingVersionArgs []*pluginsettings.UpdatePluginSettingVersionCmd
|
||||||
dashboardUpdater *DashboardUpdater
|
dashboardUpdater *DashboardUpdater
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -492,8 +492,8 @@ func scenario(t *testing.T, desc string, input scenarioInput, f func(ctx *scenar
|
|||||||
t: t,
|
t: t,
|
||||||
bus: bus.ProvideBus(tracer),
|
bus: bus.ProvideBus(tracer),
|
||||||
importDashboardArgs: []*dashboardimport.ImportDashboardRequest{},
|
importDashboardArgs: []*dashboardimport.ImportDashboardRequest{},
|
||||||
getPluginSettingsByIdArgs: []*models.GetPluginSettingByIdQuery{},
|
getPluginSettingsByIdArgs: []*pluginsettings.GetPluginSettingByIdQuery{},
|
||||||
updatePluginSettingVersionArgs: []*models.UpdatePluginSettingVersionCmd{},
|
updatePluginSettingVersionArgs: []*pluginsettings.UpdatePluginSettingVersionCmd{},
|
||||||
}
|
}
|
||||||
|
|
||||||
getPlugin := func(ctx context.Context, pluginID string) (plugins.PluginDTO, bool) {
|
getPlugin := func(ctx context.Context, pluginID string) (plugins.PluginDTO, bool) {
|
||||||
|
@ -3,8 +3,6 @@ package pluginsettings
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakePluginSettings struct {
|
type FakePluginSettings struct {
|
||||||
@ -33,7 +31,7 @@ func (ps *FakePluginSettings) GetPluginSettingByPluginID(ctx context.Context, ar
|
|||||||
if res, ok := ps.Plugins[args.PluginID]; ok {
|
if res, ok := ps.Plugins[args.PluginID]; ok {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
return nil, models.ErrPluginSettingNotFound
|
return nil, ErrPluginSettingNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdatePluginSetting updates a Plugin Setting
|
// UpdatePluginSetting updates a Plugin Setting
|
||||||
@ -66,7 +64,7 @@ func (ps *FakePluginSettings) UpdatePluginSettingPluginVersion(ctx context.Conte
|
|||||||
res.PluginVersion = args.PluginVersion
|
res.PluginVersion = args.PluginVersion
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return models.ErrPluginSettingNotFound
|
return ErrPluginSettingNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecryptedValues decrypts the encrypted secureJSONData of the provided plugin setting and
|
// DecryptedValues decrypts the encrypted secureJSONData of the provided plugin setting and
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
package pluginsettings
|
package pluginsettings
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrPluginSettingNotFound = errors.New("plugin setting not found")
|
||||||
|
)
|
||||||
|
|
||||||
type DTO struct {
|
type DTO struct {
|
||||||
ID int64
|
ID int64
|
||||||
OrgID int64
|
OrgID int64
|
||||||
@ -49,3 +54,63 @@ type GetByPluginIDArgs struct {
|
|||||||
PluginID string
|
PluginID string
|
||||||
OrgID int64
|
OrgID int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PluginSetting struct {
|
||||||
|
Id int64
|
||||||
|
PluginId string
|
||||||
|
OrgId int64
|
||||||
|
Enabled bool
|
||||||
|
Pinned bool
|
||||||
|
JsonData map[string]interface{}
|
||||||
|
SecureJsonData map[string][]byte
|
||||||
|
PluginVersion string
|
||||||
|
|
||||||
|
Created time.Time
|
||||||
|
Updated time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type PluginSettingInfo struct {
|
||||||
|
PluginID string `xorm:"plugin_id"`
|
||||||
|
OrgID int64 `xorm:"org_id"`
|
||||||
|
Enabled bool `xorm:"enabled"`
|
||||||
|
Pinned bool `xorm:"pinned"`
|
||||||
|
PluginVersion string `xorm:"plugin_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------
|
||||||
|
// COMMANDS
|
||||||
|
|
||||||
|
// Also acts as api DTO
|
||||||
|
type UpdatePluginSettingCmd struct {
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Pinned bool `json:"pinned"`
|
||||||
|
JsonData map[string]interface{} `json:"jsonData"`
|
||||||
|
SecureJsonData map[string]string `json:"secureJsonData"`
|
||||||
|
PluginVersion string `json:"version"`
|
||||||
|
|
||||||
|
PluginId string `json:"-"`
|
||||||
|
OrgId int64 `json:"-"`
|
||||||
|
EncryptedSecureJsonData map[string][]byte `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// specific command, will only update version
|
||||||
|
type UpdatePluginSettingVersionCmd struct {
|
||||||
|
PluginVersion string
|
||||||
|
PluginId string `json:"-"`
|
||||||
|
OrgId int64 `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------
|
||||||
|
// QUERIES
|
||||||
|
|
||||||
|
type GetPluginSettingByIdQuery struct {
|
||||||
|
PluginId string
|
||||||
|
OrgId int64
|
||||||
|
Result *PluginSetting
|
||||||
|
}
|
||||||
|
|
||||||
|
type PluginStateChangedEvent struct {
|
||||||
|
PluginId string
|
||||||
|
OrgId int64
|
||||||
|
Enabled bool
|
||||||
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/db"
|
"github.com/grafana/grafana/pkg/infra/db"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||||
"github.com/grafana/grafana/pkg/services/secrets"
|
"github.com/grafana/grafana/pkg/services/secrets"
|
||||||
)
|
)
|
||||||
@ -64,7 +63,7 @@ func (s *Service) GetPluginSettings(ctx context.Context, args *pluginsettings.Ge
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetPluginSettingByPluginID(ctx context.Context, args *pluginsettings.GetByPluginIDArgs) (*pluginsettings.DTO, error) {
|
func (s *Service) GetPluginSettingByPluginID(ctx context.Context, args *pluginsettings.GetByPluginIDArgs) (*pluginsettings.DTO, error) {
|
||||||
query := &models.GetPluginSettingByIdQuery{
|
query := &pluginsettings.GetPluginSettingByIdQuery{
|
||||||
OrgId: args.OrgID,
|
OrgId: args.OrgID,
|
||||||
PluginId: args.PluginID,
|
PluginId: args.PluginID,
|
||||||
}
|
}
|
||||||
@ -93,7 +92,7 @@ func (s *Service) UpdatePluginSetting(ctx context.Context, args *pluginsettings.
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.updatePluginSetting(ctx, &models.UpdatePluginSettingCmd{
|
return s.updatePluginSetting(ctx, &pluginsettings.UpdatePluginSettingCmd{
|
||||||
Enabled: args.Enabled,
|
Enabled: args.Enabled,
|
||||||
Pinned: args.Pinned,
|
Pinned: args.Pinned,
|
||||||
JsonData: args.JSONData,
|
JsonData: args.JSONData,
|
||||||
@ -106,7 +105,7 @@ func (s *Service) UpdatePluginSetting(ctx context.Context, args *pluginsettings.
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) UpdatePluginSettingPluginVersion(ctx context.Context, args *pluginsettings.UpdatePluginVersionArgs) error {
|
func (s *Service) UpdatePluginSettingPluginVersion(ctx context.Context, args *pluginsettings.UpdatePluginVersionArgs) error {
|
||||||
return s.updatePluginSettingVersion(ctx, &models.UpdatePluginSettingVersionCmd{
|
return s.updatePluginSettingVersion(ctx, &pluginsettings.UpdatePluginSettingVersionCmd{
|
||||||
PluginVersion: args.PluginVersion,
|
PluginVersion: args.PluginVersion,
|
||||||
PluginId: args.PluginID,
|
PluginId: args.PluginID,
|
||||||
OrgId: args.OrgID,
|
OrgId: args.OrgID,
|
||||||
@ -135,7 +134,7 @@ func (s *Service) DecryptedValues(ps *pluginsettings.DTO) map[string]string {
|
|||||||
return json
|
return json
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) getPluginSettingsInfo(ctx context.Context, orgID int64) ([]*models.PluginSettingInfo, error) {
|
func (s *Service) getPluginSettingsInfo(ctx context.Context, orgID int64) ([]*pluginsettings.PluginSettingInfo, error) {
|
||||||
sql := `SELECT org_id, plugin_id, enabled, pinned, plugin_version FROM plugin_setting `
|
sql := `SELECT org_id, plugin_id, enabled, pinned, plugin_version FROM plugin_setting `
|
||||||
params := make([]interface{}, 0)
|
params := make([]interface{}, 0)
|
||||||
|
|
||||||
@ -144,7 +143,7 @@ func (s *Service) getPluginSettingsInfo(ctx context.Context, orgID int64) ([]*mo
|
|||||||
params = append(params, orgID)
|
params = append(params, orgID)
|
||||||
}
|
}
|
||||||
|
|
||||||
var rslt []*models.PluginSettingInfo
|
var rslt []*pluginsettings.PluginSettingInfo
|
||||||
err := s.db.WithDbSession(ctx, func(sess *db.Session) error {
|
err := s.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||||
return sess.SQL(sql, params...).Find(&rslt)
|
return sess.SQL(sql, params...).Find(&rslt)
|
||||||
})
|
})
|
||||||
@ -155,23 +154,23 @@ func (s *Service) getPluginSettingsInfo(ctx context.Context, orgID int64) ([]*mo
|
|||||||
return rslt, nil
|
return rslt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) getPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
func (s *Service) getPluginSettingById(ctx context.Context, query *pluginsettings.GetPluginSettingByIdQuery) error {
|
||||||
return s.db.WithDbSession(ctx, func(sess *db.Session) error {
|
return s.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||||
pluginSetting := models.PluginSetting{OrgId: query.OrgId, PluginId: query.PluginId}
|
pluginSetting := pluginsettings.PluginSetting{OrgId: query.OrgId, PluginId: query.PluginId}
|
||||||
has, err := sess.Get(&pluginSetting)
|
has, err := sess.Get(&pluginSetting)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !has {
|
} else if !has {
|
||||||
return models.ErrPluginSettingNotFound
|
return pluginsettings.ErrPluginSettingNotFound
|
||||||
}
|
}
|
||||||
query.Result = &pluginSetting
|
query.Result = &pluginSetting
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) updatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
func (s *Service) updatePluginSetting(ctx context.Context, cmd *pluginsettings.UpdatePluginSettingCmd) error {
|
||||||
return s.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
return s.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
||||||
var pluginSetting models.PluginSetting
|
var pluginSetting pluginsettings.PluginSetting
|
||||||
|
|
||||||
exists, err := sess.Where("org_id=? and plugin_id=?", cmd.OrgId, cmd.PluginId).Get(&pluginSetting)
|
exists, err := sess.Where("org_id=? and plugin_id=?", cmd.OrgId, cmd.PluginId).Get(&pluginSetting)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -180,7 +179,7 @@ func (s *Service) updatePluginSetting(ctx context.Context, cmd *models.UpdatePlu
|
|||||||
sess.UseBool("enabled")
|
sess.UseBool("enabled")
|
||||||
sess.UseBool("pinned")
|
sess.UseBool("pinned")
|
||||||
if !exists {
|
if !exists {
|
||||||
pluginSetting = models.PluginSetting{
|
pluginSetting = pluginsettings.PluginSetting{
|
||||||
PluginId: cmd.PluginId,
|
PluginId: cmd.PluginId,
|
||||||
OrgId: cmd.OrgId,
|
OrgId: cmd.OrgId,
|
||||||
Enabled: cmd.Enabled,
|
Enabled: cmd.Enabled,
|
||||||
@ -193,7 +192,7 @@ func (s *Service) updatePluginSetting(ctx context.Context, cmd *models.UpdatePlu
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add state change event on commit success
|
// add state change event on commit success
|
||||||
sess.PublishAfterCommit(&models.PluginStateChangedEvent{
|
sess.PublishAfterCommit(&pluginsettings.PluginStateChangedEvent{
|
||||||
PluginId: cmd.PluginId,
|
PluginId: cmd.PluginId,
|
||||||
OrgId: cmd.OrgId,
|
OrgId: cmd.OrgId,
|
||||||
Enabled: cmd.Enabled,
|
Enabled: cmd.Enabled,
|
||||||
@ -209,7 +208,7 @@ func (s *Service) updatePluginSetting(ctx context.Context, cmd *models.UpdatePlu
|
|||||||
|
|
||||||
// add state change event on commit success
|
// add state change event on commit success
|
||||||
if pluginSetting.Enabled != cmd.Enabled {
|
if pluginSetting.Enabled != cmd.Enabled {
|
||||||
sess.PublishAfterCommit(&models.PluginStateChangedEvent{
|
sess.PublishAfterCommit(&pluginsettings.PluginStateChangedEvent{
|
||||||
PluginId: cmd.PluginId,
|
PluginId: cmd.PluginId,
|
||||||
OrgId: cmd.OrgId,
|
OrgId: cmd.OrgId,
|
||||||
Enabled: cmd.Enabled,
|
Enabled: cmd.Enabled,
|
||||||
@ -227,7 +226,7 @@ func (s *Service) updatePluginSetting(ctx context.Context, cmd *models.UpdatePlu
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) updatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error {
|
func (s *Service) updatePluginSettingVersion(ctx context.Context, cmd *pluginsettings.UpdatePluginSettingVersionCmd) error {
|
||||||
return s.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
return s.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
||||||
_, err := sess.Exec("UPDATE plugin_setting SET plugin_version=? WHERE org_id=? AND plugin_id=?", cmd.PluginVersion, cmd.OrgId, cmd.PluginId)
|
_, err := sess.Exec("UPDATE plugin_setting SET plugin_version=? WHERE org_id=? AND plugin_id=?", cmd.PluginVersion, cmd.OrgId, cmd.PluginId)
|
||||||
return err
|
return err
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/db"
|
"github.com/grafana/grafana/pkg/infra/db"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||||
"github.com/grafana/grafana/pkg/services/secrets"
|
"github.com/grafana/grafana/pkg/services/secrets"
|
||||||
"github.com/grafana/grafana/pkg/services/secrets/fakes"
|
"github.com/grafana/grafana/pkg/services/secrets/fakes"
|
||||||
@ -106,7 +105,7 @@ func TestIntegrationPluginSettings(t *testing.T) {
|
|||||||
secureJsonData, err := secretsService.EncryptJsonData(context.Background(), map[string]string{"secureKey": "secureValue"}, secrets.WithoutScope())
|
secureJsonData, err := secretsService.EncryptJsonData(context.Background(), map[string]string{"secureKey": "secureValue"}, secrets.WithoutScope())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
existing := models.PluginSetting{
|
existing := pluginsettings.PluginSetting{
|
||||||
OrgId: 1,
|
OrgId: 1,
|
||||||
PluginId: "existing",
|
PluginId: "existing",
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
@ -167,8 +166,8 @@ func TestIntegrationPluginSettings(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("UpdatePluginSetting should update existing plugin settings and publish PluginStateChangedEvent", func(t *testing.T) {
|
t.Run("UpdatePluginSetting should update existing plugin settings and publish PluginStateChangedEvent", func(t *testing.T) {
|
||||||
var pluginStateChangedEvent *models.PluginStateChangedEvent
|
var pluginStateChangedEvent *pluginsettings.PluginStateChangedEvent
|
||||||
store.Bus().AddEventListener(func(_ context.Context, evt *models.PluginStateChangedEvent) error {
|
store.Bus().AddEventListener(func(_ context.Context, evt *pluginsettings.PluginStateChangedEvent) error {
|
||||||
pluginStateChangedEvent = evt
|
pluginStateChangedEvent = evt
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -225,8 +224,8 @@ func TestIntegrationPluginSettings(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("Non-existing plugin settings", func(t *testing.T) {
|
t.Run("Non-existing plugin settings", func(t *testing.T) {
|
||||||
t.Run("UpdatePluginSetting should insert plugin settings and publish PluginStateChangedEvent", func(t *testing.T) {
|
t.Run("UpdatePluginSetting should insert plugin settings and publish PluginStateChangedEvent", func(t *testing.T) {
|
||||||
var pluginStateChangedEvent *models.PluginStateChangedEvent
|
var pluginStateChangedEvent *pluginsettings.PluginStateChangedEvent
|
||||||
store.Bus().AddEventListener(func(_ context.Context, evt *models.PluginStateChangedEvent) error {
|
store.Bus().AddEventListener(func(_ context.Context, evt *pluginsettings.PluginStateChangedEvent) error {
|
||||||
pluginStateChangedEvent = evt
|
pluginStateChangedEvent = evt
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -8,9 +8,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type configReader interface {
|
type configReader interface {
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
// Code generated by mockery v2.10.0. DO NOT EDIT.
|
|
||||||
|
|
||||||
package mocks
|
|
||||||
|
|
||||||
import (
|
|
||||||
context "context"
|
|
||||||
|
|
||||||
models "github.com/grafana/grafana/pkg/models"
|
|
||||||
"github.com/grafana/grafana/pkg/services/org"
|
|
||||||
mock "github.com/stretchr/testify/mock"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Store is an autogenerated mock type for the Store type
|
|
||||||
type Store struct {
|
|
||||||
mock.Mock
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetOrgByNameHandler provides a mock function with given fields: ctx, query
|
|
||||||
func (_m *Store) GetOrgByNameHandler(ctx context.Context, query *org.GetOrgByNameQuery) error {
|
|
||||||
ret := _m.Called(ctx, query)
|
|
||||||
|
|
||||||
var r0 error
|
|
||||||
if rf, ok := ret.Get(0).(func(context.Context, *org.GetOrgByNameQuery) error); ok {
|
|
||||||
r0 = rf(ctx, query)
|
|
||||||
} else {
|
|
||||||
r0 = ret.Error(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetPluginSettingById provides a mock function with given fields: ctx, query
|
|
||||||
func (_m *Store) GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
|
||||||
ret := _m.Called(ctx, query)
|
|
||||||
|
|
||||||
var r0 error
|
|
||||||
if rf, ok := ret.Get(0).(func(context.Context, *models.GetPluginSettingByIdQuery) error); ok {
|
|
||||||
r0 = rf(ctx, query)
|
|
||||||
} else {
|
|
||||||
r0 = ret.Error(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdatePluginSetting provides a mock function with given fields: ctx, cmd
|
|
||||||
func (_m *Store) UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
|
||||||
ret := _m.Called(ctx, cmd)
|
|
||||||
|
|
||||||
var r0 error
|
|
||||||
if rf, ok := ret.Get(0).(func(context.Context, *models.UpdatePluginSettingCmd) error); ok {
|
|
||||||
r0 = rf(ctx, cmd)
|
|
||||||
} else {
|
|
||||||
r0 = ret.Error(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
@ -5,7 +5,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/services/org"
|
"github.com/grafana/grafana/pkg/services/org"
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||||
@ -51,7 +50,7 @@ func (ap *PluginProvisioner) apply(ctx context.Context, cfg *pluginsAsConfig) er
|
|||||||
PluginID: app.PluginID,
|
PluginID: app.PluginID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(err, models.ErrPluginSettingNotFound) {
|
if !errors.Is(err, pluginsettings.ErrPluginSettingNotFound) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -5,13 +5,12 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/services/org"
|
"github.com/grafana/grafana/pkg/services/org"
|
||||||
"github.com/grafana/grafana/pkg/services/org/orgtest"
|
"github.com/grafana/grafana/pkg/services/org/orgtest"
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPluginProvisioner(t *testing.T) {
|
func TestPluginProvisioner(t *testing.T) {
|
||||||
@ -91,7 +90,7 @@ func (m *mockStore) GetPluginSettingByPluginID(_ context.Context, args *pluginse
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, models.ErrPluginSettingNotFound
|
return nil, pluginsettings.ErrPluginSettingNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockStore) UpdatePluginSetting(_ context.Context, args *pluginsettings.UpdateArgs) error {
|
func (m *mockStore) UpdatePluginSetting(_ context.Context, args *pluginsettings.UpdateArgs) error {
|
||||||
|
Reference in New Issue
Block a user