mirror of
https://github.com/rancher/norman.git
synced 2025-08-18 23:36:53 +00:00
Make queryopts be a pointer
This commit is contained in:
parent
02738370b1
commit
eec4103473
@ -30,7 +30,7 @@ func ByID(context *types.APIContext, version *types.APIVersion, typeName string,
|
|||||||
return convert.ToObj(item, into)
|
return convert.ToObj(item, into)
|
||||||
}
|
}
|
||||||
|
|
||||||
func List(context *types.APIContext, version *types.APIVersion, typeName string, opts types.QueryOptions, into interface{}) error {
|
func List(context *types.APIContext, version *types.APIVersion, typeName string, opts *types.QueryOptions, into interface{}) error {
|
||||||
schema := context.Schemas.Schema(version, typeName)
|
schema := context.Schemas.Schema(version, typeName)
|
||||||
if schema == nil {
|
if schema == nil {
|
||||||
return fmt.Errorf("failed to find schema " + typeName)
|
return fmt.Errorf("failed to find schema " + typeName)
|
||||||
|
@ -52,7 +52,7 @@ func (a *APIRootStore) ByID(apiContext *types.APIContext, schema *types.Schema,
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *APIRootStore) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
|
func (a *APIRootStore) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
|
||||||
var roots []map[string]interface{}
|
var roots []map[string]interface{}
|
||||||
|
|
||||||
for _, version := range apiContext.Schemas.Versions() {
|
for _, version := range apiContext.Schemas.Versions() {
|
||||||
|
@ -20,7 +20,7 @@ func ListHandler(request *types.APIContext) error {
|
|||||||
|
|
||||||
if request.ID == "" {
|
if request.ID == "" {
|
||||||
opts := parse.QueryOptions(request, request.Schema)
|
opts := parse.QueryOptions(request, request.Schema)
|
||||||
data, err = store.List(request, request.Schema, opts)
|
data, err = store.List(request, request.Schema, &opts)
|
||||||
} else if request.Link == "" {
|
} else if request.Link == "" {
|
||||||
data, err = store.ByID(request, request.Schema, request.ID)
|
data, err = store.ByID(request, request.Schema, request.ID)
|
||||||
} else {
|
} else {
|
||||||
|
@ -7,11 +7,11 @@ import (
|
|||||||
"github.com/rancher/norman/types/convert"
|
"github.com/rancher/norman/types/convert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func QueryFilter(opts types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
|
func QueryFilter(opts *types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
|
||||||
return ApplyQueryOptions(opts, data)
|
return ApplyQueryOptions(opts, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApplyQueryOptions(options types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
|
func ApplyQueryOptions(options *types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
|
||||||
data = ApplyQueryConditions(options.Conditions, data)
|
data = ApplyQueryConditions(options.Conditions, data)
|
||||||
data = ApplySort(options.Sort, data)
|
data = ApplySort(options.Sort, data)
|
||||||
return ApplyPagination(options.Pagination, data)
|
return ApplyPagination(options.Pagination, data)
|
||||||
|
@ -115,7 +115,7 @@ func handler(apiContext *types.APIContext) error {
|
|||||||
func streamStore(ctx context.Context, eg *errgroup.Group, apiContext *types.APIContext, schema *types.Schema, result chan map[string]interface{}) {
|
func streamStore(ctx context.Context, eg *errgroup.Group, apiContext *types.APIContext, schema *types.Schema, result chan map[string]interface{}) {
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
opts := parse.QueryOptions(apiContext, schema)
|
opts := parse.QueryOptions(apiContext, schema)
|
||||||
events, err := schema.Store.Watch(apiContext, schema, opts)
|
events, err := schema.Store.Watch(apiContext, schema, &opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ func (c *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id st
|
|||||||
return store.Delete(apiContext, schema, id)
|
return store.Delete(apiContext, schema, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
|
func (c *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
|
||||||
store, ok := c.schemaStores[key(schema)]
|
store, ok := c.schemaStores[key(schema)]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -76,7 +76,7 @@ func (c *Store) List(apiContext *types.APIContext, schema *types.Schema, opt typ
|
|||||||
return store.List(apiContext, schema, opt)
|
return store.List(apiContext, schema, opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
|
func (c *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
|
||||||
store, ok := c.schemaStores[key(schema)]
|
store, ok := c.schemaStores[key(schema)]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -27,6 +27,6 @@ func (e *Store) Update(apiContext *types.APIContext, schema *types.Schema, data
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
|
func (e *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ func (p *Store) byID(apiContext *types.APIContext, schema *types.Schema, id stri
|
|||||||
return p.singleResult(apiContext, schema, req)
|
return p.singleResult(apiContext, schema, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
|
func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
|
||||||
namespace := getNamespace(apiContext, opt)
|
namespace := getNamespace(apiContext, opt)
|
||||||
|
|
||||||
req := p.common(namespace, p.k8sClient.Get())
|
req := p.common(namespace, p.k8sClient.Get())
|
||||||
@ -99,7 +99,7 @@ func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt typ
|
|||||||
return apiContext.AccessControl.FilterList(apiContext, result, p.authContext), nil
|
return apiContext.AccessControl.FilterList(apiContext, result, p.authContext), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
|
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
|
||||||
namespace := getNamespace(apiContext, opt)
|
namespace := getNamespace(apiContext, opt)
|
||||||
|
|
||||||
req := p.common(namespace, p.k8sClient.Get())
|
req := p.common(namespace, p.k8sClient.Get())
|
||||||
@ -126,6 +126,9 @@ func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt ty
|
|||||||
for event := range watcher.ResultChan() {
|
for event := range watcher.ResultChan() {
|
||||||
data := event.Object.(*unstructured.Unstructured)
|
data := event.Object.(*unstructured.Unstructured)
|
||||||
p.fromInternal(schema, data.Object)
|
p.fromInternal(schema, data.Object)
|
||||||
|
if event.Type == watch.Deleted && data.Object != nil {
|
||||||
|
data.Object[".removed"] = true
|
||||||
|
}
|
||||||
result <- apiContext.AccessControl.Filter(apiContext, data.Object, p.authContext)
|
result <- apiContext.AccessControl.Filter(apiContext, data.Object, p.authContext)
|
||||||
}
|
}
|
||||||
close(result)
|
close(result)
|
||||||
@ -144,7 +147,7 @@ func (d *unstructuredDecoder) Decode(data []byte, defaults *schema.GroupVersionK
|
|||||||
return into, defaults, ejson.Unmarshal(data, &into)
|
return into, defaults, ejson.Unmarshal(data, &into)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNamespace(apiContext *types.APIContext, opt types.QueryOptions) string {
|
func getNamespace(apiContext *types.APIContext, opt *types.QueryOptions) string {
|
||||||
if val, ok := apiContext.SubContext["namespaces"]; ok {
|
if val, ok := apiContext.SubContext["namespaces"]; ok {
|
||||||
return convert.ToString(val)
|
return convert.ToString(val)
|
||||||
}
|
}
|
||||||
|
@ -34,11 +34,11 @@ func (s *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id stri
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
|
func (s *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
|
func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
|
||||||
schemaMap := apiContext.Schemas.SchemasForVersion(*apiContext.Version)
|
schemaMap := apiContext.Schemas.SchemasForVersion(*apiContext.Version)
|
||||||
schemas := make([]*types.Schema, 0, len(schemaMap))
|
schemas := make([]*types.Schema, 0, len(schemaMap))
|
||||||
schemaData := make([]map[string]interface{}, 0, len(schemaMap))
|
schemaData := make([]map[string]interface{}, 0, len(schemaMap))
|
||||||
|
@ -16,12 +16,19 @@ func NewSubTypeStore(subType string, store types.Store) *Store {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
|
func (p *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
|
||||||
|
if data != nil {
|
||||||
|
data["kind"] = p.subType
|
||||||
|
}
|
||||||
|
return p.Store.Create(apiContext, schema, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
|
||||||
opt.Conditions = append(opt.Conditions, types.NewConditionFromString("type", types.ModifierEQ, p.subType))
|
opt.Conditions = append(opt.Conditions, types.NewConditionFromString("type", types.ModifierEQ, p.subType))
|
||||||
return p.Store.List(apiContext, schema, opt)
|
return p.Store.List(apiContext, schema, opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
|
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
|
||||||
opt.Conditions = append(opt.Conditions, types.NewConditionFromString("type", types.ModifierEQ, p.subType))
|
opt.Conditions = append(opt.Conditions, types.NewConditionFromString("type", types.ModifierEQ, p.subType))
|
||||||
return p.Store.Watch(apiContext, schema, opt)
|
return p.Store.Watch(apiContext, schema, opt)
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ func (t *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id stri
|
|||||||
return t.Transformer(apiContext, data)
|
return t.Transformer(apiContext, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
|
func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
|
||||||
c, err := t.Store.Watch(apiContext, schema, opt)
|
c, err := t.Store.Watch(apiContext, schema, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -50,7 +50,7 @@ func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt ty
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
|
func (t *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
|
||||||
data, err := t.Store.List(apiContext, schema, opt)
|
data, err := t.Store.List(apiContext, schema, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -21,12 +21,12 @@ func (s *StoreWrapper) ByID(apiContext *types.APIContext, schema *types.Schema,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return apiContext.FilterObject(types.QueryOptions{
|
return apiContext.FilterObject(&types.QueryOptions{
|
||||||
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
|
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
|
||||||
}, data), nil
|
}, data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema, opts types.QueryOptions) ([]map[string]interface{}, error) {
|
func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema, opts *types.QueryOptions) ([]map[string]interface{}, error) {
|
||||||
opts.Conditions = append(opts.Conditions, apiContext.SubContextAttributeProvider.Query(apiContext, schema)...)
|
opts.Conditions = append(opts.Conditions, apiContext.SubContextAttributeProvider.Query(apiContext, schema)...)
|
||||||
data, err := s.store.List(apiContext, schema, opts)
|
data, err := s.store.List(apiContext, schema, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -36,7 +36,7 @@ func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema,
|
|||||||
return apiContext.FilterList(opts, data), nil
|
return apiContext.FilterList(opts, data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
|
func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
|
||||||
c, err := s.store.Watch(apiContext, schema, opt)
|
c, err := s.store.Watch(apiContext, schema, opt)
|
||||||
if err != nil || c == nil {
|
if err != nil || c == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -45,7 +45,7 @@ func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema,
|
|||||||
result := make(chan map[string]interface{})
|
result := make(chan map[string]interface{})
|
||||||
go func() {
|
go func() {
|
||||||
for item := range c {
|
for item := range c {
|
||||||
item = apiContext.FilterObject(types.QueryOptions{
|
item = apiContext.FilterObject(&types.QueryOptions{
|
||||||
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
|
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
|
||||||
}, item)
|
}, item)
|
||||||
if item != nil {
|
if item != nil {
|
||||||
@ -85,7 +85,7 @@ func (s *StoreWrapper) Update(apiContext *types.APIContext, schema *types.Schema
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return apiContext.FilterObject(types.QueryOptions{
|
return apiContext.FilterObject(&types.QueryOptions{
|
||||||
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
|
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
|
||||||
}, data), nil
|
}, data), nil
|
||||||
}
|
}
|
||||||
@ -109,7 +109,7 @@ func validateGet(apiContext *types.APIContext, schema *types.Schema, id string)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if apiContext.Filter(types.QueryOptions{
|
if apiContext.Filter(&types.QueryOptions{
|
||||||
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
|
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
|
||||||
}, existing) == nil {
|
}, existing) == nil {
|
||||||
return httperror.NewAPIError(httperror.NotFound, "failed to find "+id)
|
return httperror.NewAPIError(httperror.NotFound, "failed to find "+id)
|
||||||
|
@ -44,7 +44,7 @@ type ActionHandler func(actionName string, action *Action, request *APIContext)
|
|||||||
|
|
||||||
type RequestHandler func(request *APIContext) error
|
type RequestHandler func(request *APIContext) error
|
||||||
|
|
||||||
type QueryFilter func(opts QueryOptions, data []map[string]interface{}) []map[string]interface{}
|
type QueryFilter func(opts *QueryOptions, data []map[string]interface{}) []map[string]interface{}
|
||||||
|
|
||||||
type Validator func(request *APIContext, data map[string]interface{}) error
|
type Validator func(request *APIContext, data map[string]interface{}) error
|
||||||
|
|
||||||
@ -100,11 +100,11 @@ func (r *APIContext) WriteResponse(code int, obj interface{}) {
|
|||||||
r.ResponseWriter.Write(r, code, obj)
|
r.ResponseWriter.Write(r, code, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *APIContext) FilterList(opts QueryOptions, obj []map[string]interface{}) []map[string]interface{} {
|
func (r *APIContext) FilterList(opts *QueryOptions, obj []map[string]interface{}) []map[string]interface{} {
|
||||||
return r.QueryFilter(opts, obj)
|
return r.QueryFilter(opts, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *APIContext) FilterObject(opts QueryOptions, obj map[string]interface{}) map[string]interface{} {
|
func (r *APIContext) FilterObject(opts *QueryOptions, obj map[string]interface{}) map[string]interface{} {
|
||||||
opts.Pagination = nil
|
opts.Pagination = nil
|
||||||
result := r.QueryFilter(opts, []map[string]interface{}{obj})
|
result := r.QueryFilter(opts, []map[string]interface{}{obj})
|
||||||
if len(result) == 0 {
|
if len(result) == 0 {
|
||||||
@ -113,7 +113,7 @@ func (r *APIContext) FilterObject(opts QueryOptions, obj map[string]interface{})
|
|||||||
return result[0]
|
return result[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *APIContext) Filter(opts QueryOptions, obj interface{}) interface{} {
|
func (r *APIContext) Filter(opts *QueryOptions, obj interface{}) interface{} {
|
||||||
switch v := obj.(type) {
|
switch v := obj.(type) {
|
||||||
case []map[string]interface{}:
|
case []map[string]interface{}:
|
||||||
return r.FilterList(opts, v)
|
return r.FilterList(opts, v)
|
||||||
@ -161,9 +161,9 @@ type URLBuilder interface {
|
|||||||
|
|
||||||
type Store interface {
|
type Store interface {
|
||||||
ByID(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
|
ByID(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
|
||||||
List(apiContext *APIContext, schema *Schema, opt QueryOptions) ([]map[string]interface{}, error)
|
List(apiContext *APIContext, schema *Schema, opt *QueryOptions) ([]map[string]interface{}, error)
|
||||||
Create(apiContext *APIContext, schema *Schema, data map[string]interface{}) (map[string]interface{}, error)
|
Create(apiContext *APIContext, schema *Schema, data map[string]interface{}) (map[string]interface{}, error)
|
||||||
Update(apiContext *APIContext, schema *Schema, data map[string]interface{}, id string) (map[string]interface{}, error)
|
Update(apiContext *APIContext, schema *Schema, data map[string]interface{}, id string) (map[string]interface{}, error)
|
||||||
Delete(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
|
Delete(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
|
||||||
Watch(apiContext *APIContext, schema *Schema, opt QueryOptions) (chan map[string]interface{}, error)
|
Watch(apiContext *APIContext, schema *Schema, opt *QueryOptions) (chan map[string]interface{}, error)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user