chore: move plugins models into pluginsettings svc (#61944)

This commit is contained in:
Kristin Laemmert 2023-01-23 13:56:20 -05:00 committed by GitHub
parent 48b620231e
commit 6e9eb0d931
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 108 additions and 177 deletions

View File

@ -15,6 +15,7 @@ import (
"strings"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
@ -202,7 +203,7 @@ func (hs *HTTPServer) GetPluginSettingByID(c *models.ReqContext) response.Respon
OrgID: c.OrgID,
})
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)
}
} else {
@ -227,7 +228,7 @@ func (hs *HTTPServer) GetPluginSettingByID(c *models.ReqContext) response.Respon
}
func (hs *HTTPServer) UpdatePluginSetting(c *models.ReqContext) response.Response {
cmd := models.UpdatePluginSettingCmd{}
cmd := pluginsettings.UpdatePluginSettingCmd{}
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}

View File

@ -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
}

View File

@ -11,7 +11,6 @@ import (
"github.com/grafana/grafana/pkg/infra/localcache"
"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/adapters"
"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)
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 !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)
}
} else {

View File

@ -6,7 +6,6 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"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)
if event.Enabled {

View File

@ -5,9 +5,10 @@ import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/dashboardimport"
"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/pluginsettings"
"github.com/grafana/grafana/pkg/services/pluginsettings/service"
"github.com/stretchr/testify/require"
)
func TestDashboardUpdater(t *testing.T) {
@ -202,7 +202,7 @@ func TestDashboardUpdater(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",
scenarioInput{}, func(ctx *scenarioContext) {
err := ctx.bus.Publish(context.Background(), &models.PluginStateChangedEvent{
err := ctx.bus.Publish(context.Background(), &pluginsettings.PluginStateChangedEvent{
PluginId: "test",
OrgId: 2,
Enabled: false,
@ -250,7 +250,7 @@ func TestDashboardUpdater(t *testing.T) {
},
},
}, func(ctx *scenarioContext) {
err := ctx.bus.Publish(context.Background(), &models.PluginStateChangedEvent{
err := ctx.bus.Publish(context.Background(), &pluginsettings.PluginStateChangedEvent{
PluginId: "test",
OrgId: 2,
Enabled: false,
@ -307,7 +307,7 @@ func TestDashboardUpdater(t *testing.T) {
},
},
}, func(ctx *scenarioContext) {
err := ctx.bus.Publish(context.Background(), &models.PluginStateChangedEvent{
err := ctx.bus.Publish(context.Background(), &pluginsettings.PluginStateChangedEvent{
PluginId: "test",
OrgId: 2,
Enabled: true,
@ -478,8 +478,8 @@ type scenarioContext struct {
dashboardPluginService *dashboardPluginServiceMock
dashboardService *dashboardServiceMock
importDashboardArgs []*dashboardimport.ImportDashboardRequest
getPluginSettingsByIdArgs []*models.GetPluginSettingByIdQuery
updatePluginSettingVersionArgs []*models.UpdatePluginSettingVersionCmd
getPluginSettingsByIdArgs []*pluginsettings.GetPluginSettingByIdQuery
updatePluginSettingVersionArgs []*pluginsettings.UpdatePluginSettingVersionCmd
dashboardUpdater *DashboardUpdater
}
@ -492,8 +492,8 @@ func scenario(t *testing.T, desc string, input scenarioInput, f func(ctx *scenar
t: t,
bus: bus.ProvideBus(tracer),
importDashboardArgs: []*dashboardimport.ImportDashboardRequest{},
getPluginSettingsByIdArgs: []*models.GetPluginSettingByIdQuery{},
updatePluginSettingVersionArgs: []*models.UpdatePluginSettingVersionCmd{},
getPluginSettingsByIdArgs: []*pluginsettings.GetPluginSettingByIdQuery{},
updatePluginSettingVersionArgs: []*pluginsettings.UpdatePluginSettingVersionCmd{},
}
getPlugin := func(ctx context.Context, pluginID string) (plugins.PluginDTO, bool) {

View File

@ -3,8 +3,6 @@ package pluginsettings
import (
"context"
"time"
"github.com/grafana/grafana/pkg/models"
)
type FakePluginSettings struct {
@ -33,7 +31,7 @@ func (ps *FakePluginSettings) GetPluginSettingByPluginID(ctx context.Context, ar
if res, ok := ps.Plugins[args.PluginID]; ok {
return res, nil
}
return nil, models.ErrPluginSettingNotFound
return nil, ErrPluginSettingNotFound
}
// UpdatePluginSetting updates a Plugin Setting
@ -66,7 +64,7 @@ func (ps *FakePluginSettings) UpdatePluginSettingPluginVersion(ctx context.Conte
res.PluginVersion = args.PluginVersion
return nil
}
return models.ErrPluginSettingNotFound
return ErrPluginSettingNotFound
}
// DecryptedValues decrypts the encrypted secureJSONData of the provided plugin setting and

View File

@ -1,9 +1,14 @@
package pluginsettings
import (
"errors"
"time"
)
var (
ErrPluginSettingNotFound = errors.New("plugin setting not found")
)
type DTO struct {
ID int64
OrgID int64
@ -49,3 +54,63 @@ type GetByPluginIDArgs struct {
PluginID string
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
}

View File

@ -7,7 +7,6 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"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/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) {
query := &models.GetPluginSettingByIdQuery{
query := &pluginsettings.GetPluginSettingByIdQuery{
OrgId: args.OrgID,
PluginId: args.PluginID,
}
@ -93,7 +92,7 @@ func (s *Service) UpdatePluginSetting(ctx context.Context, args *pluginsettings.
return err
}
return s.updatePluginSetting(ctx, &models.UpdatePluginSettingCmd{
return s.updatePluginSetting(ctx, &pluginsettings.UpdatePluginSettingCmd{
Enabled: args.Enabled,
Pinned: args.Pinned,
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 {
return s.updatePluginSettingVersion(ctx, &models.UpdatePluginSettingVersionCmd{
return s.updatePluginSettingVersion(ctx, &pluginsettings.UpdatePluginSettingVersionCmd{
PluginVersion: args.PluginVersion,
PluginId: args.PluginID,
OrgId: args.OrgID,
@ -135,7 +134,7 @@ func (s *Service) DecryptedValues(ps *pluginsettings.DTO) map[string]string {
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 `
params := make([]interface{}, 0)
@ -144,7 +143,7 @@ func (s *Service) getPluginSettingsInfo(ctx context.Context, orgID int64) ([]*mo
params = append(params, orgID)
}
var rslt []*models.PluginSettingInfo
var rslt []*pluginsettings.PluginSettingInfo
err := s.db.WithDbSession(ctx, func(sess *db.Session) error {
return sess.SQL(sql, params...).Find(&rslt)
})
@ -155,23 +154,23 @@ func (s *Service) getPluginSettingsInfo(ctx context.Context, orgID int64) ([]*mo
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 {
pluginSetting := models.PluginSetting{OrgId: query.OrgId, PluginId: query.PluginId}
pluginSetting := pluginsettings.PluginSetting{OrgId: query.OrgId, PluginId: query.PluginId}
has, err := sess.Get(&pluginSetting)
if err != nil {
return err
} else if !has {
return models.ErrPluginSettingNotFound
return pluginsettings.ErrPluginSettingNotFound
}
query.Result = &pluginSetting
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 {
var pluginSetting models.PluginSetting
var pluginSetting pluginsettings.PluginSetting
exists, err := sess.Where("org_id=? and plugin_id=?", cmd.OrgId, cmd.PluginId).Get(&pluginSetting)
if err != nil {
@ -180,7 +179,7 @@ func (s *Service) updatePluginSetting(ctx context.Context, cmd *models.UpdatePlu
sess.UseBool("enabled")
sess.UseBool("pinned")
if !exists {
pluginSetting = models.PluginSetting{
pluginSetting = pluginsettings.PluginSetting{
PluginId: cmd.PluginId,
OrgId: cmd.OrgId,
Enabled: cmd.Enabled,
@ -193,7 +192,7 @@ func (s *Service) updatePluginSetting(ctx context.Context, cmd *models.UpdatePlu
}
// add state change event on commit success
sess.PublishAfterCommit(&models.PluginStateChangedEvent{
sess.PublishAfterCommit(&pluginsettings.PluginStateChangedEvent{
PluginId: cmd.PluginId,
OrgId: cmd.OrgId,
Enabled: cmd.Enabled,
@ -209,7 +208,7 @@ func (s *Service) updatePluginSetting(ctx context.Context, cmd *models.UpdatePlu
// add state change event on commit success
if pluginSetting.Enabled != cmd.Enabled {
sess.PublishAfterCommit(&models.PluginStateChangedEvent{
sess.PublishAfterCommit(&pluginsettings.PluginStateChangedEvent{
PluginId: cmd.PluginId,
OrgId: cmd.OrgId,
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 {
_, err := sess.Exec("UPDATE plugin_setting SET plugin_version=? WHERE org_id=? AND plugin_id=?", cmd.PluginVersion, cmd.OrgId, cmd.PluginId)
return err

View File

@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/require"
"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/secrets"
"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())
require.NoError(t, err)
existing := models.PluginSetting{
existing := pluginsettings.PluginSetting{
OrgId: 1,
PluginId: "existing",
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) {
var pluginStateChangedEvent *models.PluginStateChangedEvent
store.Bus().AddEventListener(func(_ context.Context, evt *models.PluginStateChangedEvent) error {
var pluginStateChangedEvent *pluginsettings.PluginStateChangedEvent
store.Bus().AddEventListener(func(_ context.Context, evt *pluginsettings.PluginStateChangedEvent) error {
pluginStateChangedEvent = evt
return nil
})
@ -225,8 +224,8 @@ func TestIntegrationPluginSettings(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) {
var pluginStateChangedEvent *models.PluginStateChangedEvent
store.Bus().AddEventListener(func(_ context.Context, evt *models.PluginStateChangedEvent) error {
var pluginStateChangedEvent *pluginsettings.PluginStateChangedEvent
store.Bus().AddEventListener(func(_ context.Context, evt *pluginsettings.PluginStateChangedEvent) error {
pluginStateChangedEvent = evt
return nil
})

View File

@ -8,9 +8,10 @@ import (
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/plugins"
"gopkg.in/yaml.v3"
)
type configReader interface {

View File

@ -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
}

View File

@ -5,7 +5,6 @@ import (
"errors"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/pluginsettings"
@ -51,7 +50,7 @@ func (ap *PluginProvisioner) apply(ctx context.Context, cfg *pluginsAsConfig) er
PluginID: app.PluginID,
})
if err != nil {
if !errors.Is(err, models.ErrPluginSettingNotFound) {
if !errors.Is(err, pluginsettings.ErrPluginSettingNotFound) {
return err
}
} else {

View File

@ -5,13 +5,12 @@ import (
"errors"
"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/orgtest"
"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) {
@ -91,7 +90,7 @@ func (m *mockStore) GetPluginSettingByPluginID(_ context.Context, args *pluginse
}, nil
}
return nil, models.ErrPluginSettingNotFound
return nil, pluginsettings.ErrPluginSettingNotFound
}
func (m *mockStore) UpdatePluginSetting(_ context.Context, args *pluginsettings.UpdateArgs) error {