mirror of
https://github.com/niusmallnan/steve.git
synced 2025-09-05 15:11:26 +00:00
Disable counts on schema
This commit is contained in:
@@ -1,15 +1,7 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"golang.org/x/sync/semaphore"
|
|
||||||
|
|
||||||
"github.com/rancher/norman/pkg/types/values"
|
|
||||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/rancher/naok/pkg/accesscontrol"
|
"github.com/rancher/naok/pkg/accesscontrol"
|
||||||
@@ -51,69 +43,11 @@ func (a *apiServer) newSchemas() (*types.Schemas, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sSchema := schemas.Schema("schema")
|
|
||||||
sSchema.CollectionFormatter = a.schemaCollectionFormatter(sSchema.CollectionFormatter)
|
|
||||||
|
|
||||||
schemas.DefaultMapper = newDefaultMapper
|
schemas.DefaultMapper = newDefaultMapper
|
||||||
subscribe.Register(schemas)
|
subscribe.Register(schemas)
|
||||||
return schemas, nil
|
return schemas, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *apiServer) schemaCollectionFormatter(next types.CollectionFormatter) types.CollectionFormatter {
|
|
||||||
return func(request *types.APIRequest, collection *types.GenericCollection) {
|
|
||||||
if next != nil {
|
|
||||||
next(request, collection)
|
|
||||||
}
|
|
||||||
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
sem := semaphore.NewWeighted(100)
|
|
||||||
|
|
||||||
for _, item := range collection.Data {
|
|
||||||
resource, ok := item.(*types.RawResource)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
schema := request.Schemas.Schema(resource.ID)
|
|
||||||
if schema == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
access := accesscontrol.GetAccessListMap(schema)
|
|
||||||
if !access.Grants("list", "*", "*") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Add(1)
|
|
||||||
if err := sem.Acquire(context.TODO(), 1); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
defer func() {
|
|
||||||
sem.Release(1)
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
|
|
||||||
client, err := a.cf.Client(request, schema)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("listing", attributes.GVK(schema))
|
|
||||||
resp, err := client.List(v1.ListOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(resp.Items) > 0 {
|
|
||||||
values.PutValue(resource.Values, len(resp.Items), "attributes", "count")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.APIRequest, bool) {
|
func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.APIRequest, bool) {
|
||||||
user := &user.DefaultInfo{
|
user := &user.DefaultInfo{
|
||||||
Name: "admin",
|
Name: "admin",
|
||||||
|
Reference in New Issue
Block a user