Files
2025-05-15 21:36:52 +02:00

76 lines
1.8 KiB
Go

package folders
import (
"context"
"net/http"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
folders "github.com/grafana/grafana/apps/folder/pkg/apis/folder/v1beta1"
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
"github.com/grafana/grafana/pkg/storage/unified/resourcepb"
)
type subCountREST struct {
searcher resourcepb.ResourceIndexClient
}
var (
_ = rest.Connecter(&subCountREST{})
_ = rest.StorageMetadata(&subCountREST{})
)
func (r *subCountREST) New() runtime.Object {
return &folders.DescendantCounts{}
}
func (r *subCountREST) Destroy() {
}
func (r *subCountREST) ConnectMethods() []string {
return []string{"GET"}
}
func (r *subCountREST) ProducesMIMETypes(verb string) []string {
return nil
}
func (r *subCountREST) ProducesObject(verb string) interface{} {
return &folders.DescendantCounts{}
}
func (r *subCountREST) NewConnectOptions() (runtime.Object, bool, string) {
return nil, false, "" // true means you can use the trailing path as a variable
}
func (r *subCountREST) Connect(ctx context.Context, name string, _ runtime.Object, responder rest.Responder) (http.Handler, error) {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ns, err := request.NamespaceInfoFrom(ctx, true)
if err != nil {
responder.Error(err)
return
}
stats, err := r.searcher.GetStats(ctx, &resourcepb.ResourceStatsRequest{
Namespace: ns.Value,
Folder: name,
})
if err != nil {
responder.Error(err)
return
}
rsp := &folders.DescendantCounts{
Counts: make([]folders.ResourceStats, len(stats.Stats)),
}
for i, v := range stats.Stats {
rsp.Counts[i] = folders.ResourceStats{
Group: v.Group,
Resource: v.Resource,
Count: v.Count,
}
}
responder.Object(200, rsp)
}), nil
}