Plugins: Add plugin settings DTO (#46283)
* add clearer service layer * re-order frontend settings for clarity * fix fetch fail * fix API response * fix mockstore * in -> where
This commit is contained in:
@@ -48,27 +48,28 @@ func (ap *PluginProvisioner) apply(ctx context.Context, cfg *pluginsAsConfig) er
|
||||
app.OrgID = 1
|
||||
}
|
||||
|
||||
query := &models.GetPluginSettingByIdQuery{OrgId: app.OrgID, PluginId: app.PluginID}
|
||||
err := ap.pluginSettings.GetPluginSettingById(ctx, query)
|
||||
ps, err := ap.pluginSettings.GetPluginSettingByPluginID(ctx, &pluginsettings.GetByPluginIDArgs{
|
||||
OrgID: app.OrgID,
|
||||
PluginID: app.PluginID,
|
||||
})
|
||||
if err != nil {
|
||||
if !errors.Is(err, models.ErrPluginSettingNotFound) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
app.PluginVersion = query.Result.PluginVersion
|
||||
app.PluginVersion = ps.PluginVersion
|
||||
}
|
||||
|
||||
ap.log.Info("Updating app from configuration ", "type", app.PluginID, "enabled", app.Enabled)
|
||||
cmd := &models.UpdatePluginSettingCmd{
|
||||
OrgId: app.OrgID,
|
||||
PluginId: app.PluginID,
|
||||
if err := ap.pluginSettings.UpdatePluginSetting(ctx, &pluginsettings.UpdateArgs{
|
||||
OrgID: app.OrgID,
|
||||
PluginID: app.PluginID,
|
||||
Enabled: app.Enabled,
|
||||
Pinned: app.Pinned,
|
||||
JsonData: app.JSONData,
|
||||
SecureJsonData: app.SecureJSONData,
|
||||
JSONData: app.JSONData,
|
||||
SecureJSONData: app.SecureJSONData,
|
||||
PluginVersion: app.PluginVersion,
|
||||
}
|
||||
if err := ap.pluginSettings.UpdatePluginSetting(ctx, cmd); err != nil {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
@@ -25,8 +27,8 @@ func TestPluginProvisioner(t *testing.T) {
|
||||
Apps: []*appFromConfig{
|
||||
{PluginID: "test-plugin", OrgID: 2, Enabled: true},
|
||||
{PluginID: "test-plugin-2", OrgID: 3, Enabled: false},
|
||||
{PluginID: "test-plugin", OrgName: "Org 4", Enabled: true},
|
||||
{PluginID: "test-plugin-2", OrgID: 1, Enabled: true},
|
||||
{PluginID: "test-plugin", OrgName: "Org 4", Enabled: true, SecureJSONData: map[string]string{"token": "secret"}},
|
||||
{PluginID: "test-plugin-2", OrgID: 1, Enabled: true, JSONData: map[string]interface{}{"test": true}},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -36,27 +38,31 @@ func TestPluginProvisioner(t *testing.T) {
|
||||
|
||||
err := ap.applyChanges(context.Background(), "")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, store.sentCommands, 4)
|
||||
require.Len(t, store.updateRequests, 4)
|
||||
|
||||
testCases := []struct {
|
||||
ExpectedPluginID string
|
||||
ExpectedOrgID int64
|
||||
ExpectedEnabled bool
|
||||
ExpectedPluginVersion string
|
||||
ExpectedPluginID string
|
||||
ExpectedOrgID int64
|
||||
ExpectedEnabled bool
|
||||
ExpectedPluginVersion string
|
||||
ExpectedJSONData map[string]interface{}
|
||||
ExpectedSecureJSONData map[string]string
|
||||
}{
|
||||
{ExpectedPluginID: "test-plugin", ExpectedOrgID: 2, ExpectedEnabled: true, ExpectedPluginVersion: "2.0.1"},
|
||||
{ExpectedPluginID: "test-plugin-2", ExpectedOrgID: 3, ExpectedEnabled: false},
|
||||
{ExpectedPluginID: "test-plugin", ExpectedOrgID: 4, ExpectedEnabled: true},
|
||||
{ExpectedPluginID: "test-plugin-2", ExpectedOrgID: 1, ExpectedEnabled: true},
|
||||
{ExpectedPluginID: "test-plugin", ExpectedOrgID: 4, ExpectedEnabled: true, ExpectedSecureJSONData: map[string]string{"token": "secret"}},
|
||||
{ExpectedPluginID: "test-plugin-2", ExpectedOrgID: 1, ExpectedEnabled: true, ExpectedJSONData: map[string]interface{}{"test": true}},
|
||||
}
|
||||
|
||||
for index, tc := range testCases {
|
||||
cmd := store.sentCommands[index]
|
||||
cmd := store.updateRequests[index]
|
||||
require.NotNil(t, cmd)
|
||||
require.Equal(t, tc.ExpectedPluginID, cmd.PluginId)
|
||||
require.Equal(t, tc.ExpectedOrgID, cmd.OrgId)
|
||||
require.Equal(t, tc.ExpectedPluginID, cmd.PluginID)
|
||||
require.Equal(t, tc.ExpectedOrgID, cmd.OrgID)
|
||||
require.Equal(t, tc.ExpectedEnabled, cmd.Enabled)
|
||||
require.Equal(t, tc.ExpectedPluginVersion, cmd.PluginVersion)
|
||||
require.Equal(t, tc.ExpectedJSONData, cmd.JSONData)
|
||||
require.Equal(t, tc.ExpectedSecureJSONData, cmd.SecureJSONData)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -66,45 +72,44 @@ type testConfigReader struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (tcr *testConfigReader) readConfig(ctx context.Context, path string) ([]*pluginsAsConfig, error) {
|
||||
func (tcr *testConfigReader) readConfig(_ context.Context, _ string) ([]*pluginsAsConfig, error) {
|
||||
return tcr.result, tcr.err
|
||||
}
|
||||
|
||||
type mockStore struct {
|
||||
sentCommands []*models.UpdatePluginSettingCmd
|
||||
updateRequests []*pluginsettings.UpdateArgs
|
||||
}
|
||||
|
||||
func (m *mockStore) GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
||||
func (m *mockStore) GetOrgByNameHandler(_ context.Context, query *models.GetOrgByNameQuery) error {
|
||||
if query.Name == "Org 4" {
|
||||
query.Result = &models.Org{Id: 4}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockStore) GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
||||
if query.PluginId == "test-plugin" && query.OrgId == 2 {
|
||||
query.Result = &models.PluginSetting{
|
||||
func (m *mockStore) GetPluginSettingByPluginID(_ context.Context, args *pluginsettings.GetByPluginIDArgs) (*pluginsettings.DTO, error) {
|
||||
if args.PluginID == "test-plugin" && args.OrgID == 2 {
|
||||
return &pluginsettings.DTO{
|
||||
PluginVersion: "2.0.1",
|
||||
}
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
return models.ErrPluginSettingNotFound
|
||||
return nil, models.ErrPluginSettingNotFound
|
||||
}
|
||||
|
||||
func (m *mockStore) UpdatePluginSetting(_ context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
||||
m.sentCommands = append(m.sentCommands, cmd)
|
||||
func (m *mockStore) UpdatePluginSetting(_ context.Context, args *pluginsettings.UpdateArgs) error {
|
||||
m.updateRequests = append(m.updateRequests, args)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockStore) UpdatePluginSettingVersion(_ context.Context, _ *models.UpdatePluginSettingVersionCmd) error {
|
||||
func (m *mockStore) UpdatePluginSettingPluginVersion(_ context.Context, _ *pluginsettings.UpdatePluginVersionArgs) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockStore) GetPluginSettings(_ context.Context, _ int64) ([]*models.PluginSettingInfoDTO, error) {
|
||||
func (m *mockStore) GetPluginSettings(_ context.Context, _ *pluginsettings.GetArgs) ([]*pluginsettings.DTO, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *mockStore) DecryptedValues(_ *models.PluginSetting) map[string]string {
|
||||
func (m *mockStore) DecryptedValues(_ *pluginsettings.DTO) map[string]string {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user