Dashboards: Remove panel plugin provider from migrations (#110477)
This commit is contained in:
@@ -23,7 +23,7 @@ import (
|
||||
)
|
||||
|
||||
func TestDashboardAPIBuilder_Mutate(t *testing.T) {
|
||||
migration.Initialize(testutil.GetTestDataSourceProvider(), testutil.GetTestPanelProvider())
|
||||
migration.Initialize(testutil.GetTestDataSourceProvider())
|
||||
tests := []struct {
|
||||
name string
|
||||
inputObj runtime.Object
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
||||
)
|
||||
|
||||
type PluginStorePanelProvider struct {
|
||||
pluginStore pluginstore.Store
|
||||
buildVersion string
|
||||
}
|
||||
|
||||
func (p *PluginStorePanelProvider) GetPanels() []schemaversion.PanelPluginInfo {
|
||||
panelPlugins := p.pluginStore.Plugins(context.Background(), plugins.TypePanel)
|
||||
|
||||
panels := make([]schemaversion.PanelPluginInfo, len(panelPlugins))
|
||||
for i, plugin := range panelPlugins {
|
||||
version := plugin.Info.Version
|
||||
if version == "" {
|
||||
version = p.buildVersion
|
||||
}
|
||||
panels[i] = schemaversion.PanelPluginInfo{
|
||||
ID: plugin.ID,
|
||||
Version: version,
|
||||
}
|
||||
}
|
||||
return panels
|
||||
}
|
||||
|
||||
func (p *PluginStorePanelProvider) GetPanelPlugin(id string) schemaversion.PanelPluginInfo {
|
||||
for _, plugin := range p.GetPanels() {
|
||||
if plugin.ID == id {
|
||||
return plugin
|
||||
}
|
||||
}
|
||||
|
||||
return schemaversion.PanelPluginInfo{}
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPluginStorePanelProvider_GetPanels(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
plugins []pluginstore.Plugin
|
||||
buildVersion string
|
||||
expectedPanels []schemaversion.PanelPluginInfo
|
||||
}{
|
||||
{
|
||||
name: "should return all panel plugins with their versions",
|
||||
plugins: []pluginstore.Plugin{
|
||||
{
|
||||
JSONData: plugins.JSONData{ID: "gauge", Info: plugins.Info{Version: "1.0.0"}},
|
||||
},
|
||||
{
|
||||
JSONData: plugins.JSONData{ID: "stat", Info: plugins.Info{Version: "2.0.0"}},
|
||||
},
|
||||
{
|
||||
JSONData: plugins.JSONData{ID: "timeseries", Info: plugins.Info{Version: "3.0.0"}},
|
||||
},
|
||||
},
|
||||
buildVersion: "10.0.0",
|
||||
expectedPanels: []schemaversion.PanelPluginInfo{
|
||||
{ID: "gauge", Version: "1.0.0"},
|
||||
{ID: "stat", Version: "2.0.0"},
|
||||
{ID: "timeseries", Version: "3.0.0"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should use build version when plugin version is empty",
|
||||
plugins: []pluginstore.Plugin{
|
||||
{
|
||||
JSONData: plugins.JSONData{ID: "gauge", Info: plugins.Info{Version: ""}},
|
||||
},
|
||||
{
|
||||
JSONData: plugins.JSONData{ID: "stat", Info: plugins.Info{Version: "2.0.0"}},
|
||||
},
|
||||
},
|
||||
buildVersion: "10.0.0",
|
||||
expectedPanels: []schemaversion.PanelPluginInfo{
|
||||
{ID: "gauge", Version: "10.0.0"},
|
||||
{ID: "stat", Version: "2.0.0"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should return empty slice when no plugins",
|
||||
plugins: []pluginstore.Plugin{},
|
||||
buildVersion: "10.0.0",
|
||||
expectedPanels: []schemaversion.PanelPluginInfo{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Create mock plugin store
|
||||
mockStore := &mockPluginStore{
|
||||
plugins: tt.plugins,
|
||||
}
|
||||
|
||||
// Create mock setting
|
||||
mockSetting := &setting.Cfg{
|
||||
BuildVersion: tt.buildVersion,
|
||||
}
|
||||
|
||||
// Create provider
|
||||
provider := &PluginStorePanelProvider{
|
||||
pluginStore: mockStore,
|
||||
buildVersion: mockSetting.BuildVersion,
|
||||
}
|
||||
|
||||
// Call the function
|
||||
result := provider.GetPanels()
|
||||
|
||||
// Assert results
|
||||
assert.Len(t, result, len(tt.expectedPanels))
|
||||
for i, expected := range tt.expectedPanels {
|
||||
assert.Equal(t, expected.ID, result[i].ID)
|
||||
assert.Equal(t, expected.Version, result[i].Version)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginStorePanelProvider_GetPanelPlugin(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
plugins []pluginstore.Plugin
|
||||
buildVersion string
|
||||
searchID string
|
||||
expectedPanel schemaversion.PanelPluginInfo
|
||||
}{
|
||||
{
|
||||
name: "should return panel plugin when found",
|
||||
plugins: []pluginstore.Plugin{
|
||||
{
|
||||
JSONData: plugins.JSONData{ID: "gauge", Info: plugins.Info{Version: "1.0.0"}},
|
||||
},
|
||||
{
|
||||
JSONData: plugins.JSONData{ID: "stat", Info: plugins.Info{Version: "2.0.0"}},
|
||||
},
|
||||
},
|
||||
buildVersion: "10.0.0",
|
||||
searchID: "stat",
|
||||
expectedPanel: schemaversion.PanelPluginInfo{
|
||||
ID: "stat",
|
||||
Version: "2.0.0",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should return panel plugin with build version when plugin version is empty",
|
||||
plugins: []pluginstore.Plugin{
|
||||
{
|
||||
JSONData: plugins.JSONData{ID: "gauge", Info: plugins.Info{Version: ""}},
|
||||
},
|
||||
},
|
||||
buildVersion: "10.0.0",
|
||||
searchID: "gauge",
|
||||
expectedPanel: schemaversion.PanelPluginInfo{
|
||||
ID: "gauge",
|
||||
Version: "10.0.0",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should return empty panel plugin when not found",
|
||||
plugins: []pluginstore.Plugin{
|
||||
{
|
||||
JSONData: plugins.JSONData{ID: "gauge", Info: plugins.Info{Version: "1.0.0"}},
|
||||
},
|
||||
},
|
||||
buildVersion: "10.0.0",
|
||||
searchID: "nonexistent",
|
||||
expectedPanel: schemaversion.PanelPluginInfo{},
|
||||
},
|
||||
{
|
||||
name: "should return empty panel plugin when no plugins exist",
|
||||
plugins: []pluginstore.Plugin{},
|
||||
buildVersion: "10.0.0",
|
||||
searchID: "gauge",
|
||||
expectedPanel: schemaversion.PanelPluginInfo{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mockStore := &mockPluginStore{
|
||||
plugins: tt.plugins,
|
||||
}
|
||||
|
||||
mockSetting := &setting.Cfg{
|
||||
BuildVersion: tt.buildVersion,
|
||||
}
|
||||
|
||||
provider := &PluginStorePanelProvider{
|
||||
pluginStore: mockStore,
|
||||
buildVersion: mockSetting.BuildVersion,
|
||||
}
|
||||
|
||||
result := provider.GetPanelPlugin(tt.searchID)
|
||||
|
||||
assert.Equal(t, tt.expectedPanel.ID, result.ID)
|
||||
assert.Equal(t, tt.expectedPanel.Version, result.Version)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type mockPluginStore struct {
|
||||
plugins []pluginstore.Plugin
|
||||
}
|
||||
|
||||
func (m *mockPluginStore) Plugin(ctx context.Context, pluginID string) (pluginstore.Plugin, bool) {
|
||||
for _, p := range m.plugins {
|
||||
if p.ID == pluginID {
|
||||
return p, true
|
||||
}
|
||||
}
|
||||
return pluginstore.Plugin{}, false
|
||||
}
|
||||
|
||||
func (m *mockPluginStore) Plugins(ctx context.Context, pluginTypes ...plugins.Type) []pluginstore.Plugin {
|
||||
return m.plugins
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"maps"
|
||||
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -28,6 +27,8 @@ import (
|
||||
dashv2beta1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2beta1"
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/migration"
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/migration/conversion"
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
|
||||
folders "github.com/grafana/grafana/apps/folder/pkg/apis/folder/v1beta1"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
|
||||
@@ -37,8 +38,10 @@ import (
|
||||
"github.com/grafana/grafana/pkg/registry/apis/dashboard/legacy"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/dashboard/legacysearcher"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver"
|
||||
authsvc "github.com/grafana/grafana/pkg/services/apiserver/auth/authorizer"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/builder"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/client"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
dashsvc "github.com/grafana/grafana/pkg/services/dashboards/service"
|
||||
@@ -56,10 +59,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/storage/legacysql/dualwrite"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/apistore"
|
||||
"github.com/grafana/grafana/pkg/storage/unified/resource"
|
||||
|
||||
folders "github.com/grafana/grafana/apps/folder/pkg/apis/folder/v1beta1"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/client"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -167,9 +166,6 @@ func RegisterAPIService(
|
||||
migration.RegisterMetrics(reg)
|
||||
migration.Initialize(&datasourceInfoProvider{
|
||||
datasourceService: datasourceService,
|
||||
}, &PluginStorePanelProvider{
|
||||
pluginStore: pluginStore,
|
||||
buildVersion: cfg.BuildVersion,
|
||||
})
|
||||
apiregistration.RegisterAPI(builder)
|
||||
return builder
|
||||
@@ -184,10 +180,7 @@ func NewAPIService(ac claims.AccessClient, features featuremgmt.FeatureToggles,
|
||||
|
||||
logger := log.New("grafana-apiserver.dashboards")
|
||||
|
||||
migration.Initialize(datasourceProvider, &PluginStorePanelProvider{
|
||||
pluginStore: pluginStore,
|
||||
buildVersion: "unknown",
|
||||
})
|
||||
migration.Initialize(datasourceProvider)
|
||||
|
||||
return &DashboardsAPIBuilder{
|
||||
log: logger,
|
||||
|
||||
Reference in New Issue
Block a user