Files
grafana/pkg/registry/apis/dashboard/libary_panel.go
Marco de Abreu d57fa1291f Dashboards: Disallow List with RV filter on legacy & align RV format (#104003)
Throw errors when trying to call List in legacy with an RV specified
Change ResourceVersion to microseconds

Co-authored-by: Marco de Abreu <18629099+marcoabreu@users.noreply.github.com>
2025-04-15 22:08:54 +01:00

98 lines
2.6 KiB
Go

package dashboard
import (
"context"
"fmt"
"strconv"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
"github.com/grafana/grafana/pkg/apimachinery/utils"
"github.com/grafana/grafana/pkg/registry/apis/dashboard/legacy"
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)
var (
_ rest.Scoper = (*LibraryPanelStore)(nil)
_ rest.SingularNameProvider = (*LibraryPanelStore)(nil)
_ rest.Getter = (*LibraryPanelStore)(nil)
_ rest.Lister = (*LibraryPanelStore)(nil)
_ rest.Storage = (*LibraryPanelStore)(nil)
)
type LibraryPanelStore struct {
Access legacy.DashboardAccess
ResourceInfo utils.ResourceInfo
}
func (s *LibraryPanelStore) New() runtime.Object {
return s.ResourceInfo.NewFunc()
}
func (s *LibraryPanelStore) Destroy() {}
func (s *LibraryPanelStore) NamespaceScoped() bool {
return true // namespace == org
}
func (s *LibraryPanelStore) GetSingularName() string {
return s.ResourceInfo.GetSingularName()
}
func (s *LibraryPanelStore) NewList() runtime.Object {
return s.ResourceInfo.NewListFunc()
}
func (s *LibraryPanelStore) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
return s.ResourceInfo.TableConverter().ConvertToTable(ctx, object, tableOptions)
}
func (s *LibraryPanelStore) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
if options.ResourceVersion != "" {
return nil, apierrors.NewBadRequest("List with explicit resourceVersion is not supported with this storage backend")
}
ns, err := request.NamespaceInfoFrom(ctx, true)
if err != nil {
return nil, err
}
query := legacy.LibraryPanelQuery{
OrgID: ns.OrgID,
Limit: options.Limit,
}
if options.Continue != "" {
query.LastID, err = strconv.ParseInt(options.Continue, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid continue token")
}
}
if query.Limit < 1 {
query.Limit = 25
}
return s.Access.GetLibraryPanels(ctx, query)
}
func (s *LibraryPanelStore) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
ns, err := request.NamespaceInfoFrom(ctx, true)
if err != nil {
return nil, err
}
query := legacy.LibraryPanelQuery{
OrgID: ns.OrgID,
UID: name,
Limit: 1,
}
found, err := s.Access.GetLibraryPanels(ctx, query)
if err != nil {
return nil, err
}
if len(found.Items) == 1 {
return &found.Items[0], nil
}
return nil, s.ResourceInfo.NewNotFound(name)
}