1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-28 11:50:34 +00:00
norman/store/wrapper/wrapper.go

120 lines
3.1 KiB
Go
Raw Normal View History

2017-11-21 20:46:30 +00:00
package wrapper
import (
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
)
func Wrap(store types.Store) types.Store {
return &StoreWrapper{
store: store,
}
}
type StoreWrapper struct {
store types.Store
}
func (s *StoreWrapper) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
data, err := s.store.ByID(apiContext, schema, id)
if err != nil {
return nil, err
}
2017-12-28 15:47:10 +00:00
return apiContext.FilterObject(&types.QueryOptions{
2017-11-21 20:46:30 +00:00
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, data), nil
}
2017-12-28 15:47:10 +00:00
func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema, opts *types.QueryOptions) ([]map[string]interface{}, error) {
2017-11-21 20:46:30 +00:00
opts.Conditions = append(opts.Conditions, apiContext.SubContextAttributeProvider.Query(apiContext, schema)...)
data, err := s.store.List(apiContext, schema, opts)
if err != nil {
return nil, err
}
return apiContext.FilterList(opts, data), nil
}
2017-12-28 15:47:10 +00:00
func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
2017-11-28 21:28:25 +00:00
c, err := s.store.Watch(apiContext, schema, opt)
2017-12-12 03:58:43 +00:00
if err != nil || c == nil {
2017-11-28 21:28:25 +00:00
return nil, err
}
result := make(chan map[string]interface{})
go func() {
for item := range c {
2017-12-28 15:47:10 +00:00
item = apiContext.FilterObject(&types.QueryOptions{
2017-11-28 21:28:25 +00:00
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, item)
if item != nil {
result <- item
}
}
close(result)
}()
return result, nil
}
2017-11-21 20:46:30 +00:00
func (s *StoreWrapper) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
for key, value := range apiContext.SubContextAttributeProvider.Create(apiContext, schema) {
if data == nil {
data = map[string]interface{}{}
}
data[key] = value
}
data, err := s.store.Create(apiContext, schema, data)
if err != nil {
return nil, err
}
return data, nil
}
func (s *StoreWrapper) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
err := validateGet(apiContext, schema, id)
if err != nil {
return nil, err
}
data, err = s.store.Update(apiContext, schema, data, id)
if err != nil {
return nil, err
}
2017-12-28 15:47:10 +00:00
return apiContext.FilterObject(&types.QueryOptions{
2017-11-21 20:46:30 +00:00
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, data), nil
}
func (s *StoreWrapper) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
2017-11-21 20:46:30 +00:00
if err := validateGet(apiContext, schema, id); err != nil {
return nil, err
2017-11-21 20:46:30 +00:00
}
return s.store.Delete(apiContext, schema, id)
}
func validateGet(apiContext *types.APIContext, schema *types.Schema, id string) error {
store := schema.Store
if store == nil {
return nil
}
existing, err := store.ByID(apiContext, schema, id)
if err != nil {
return err
}
2017-12-28 15:47:10 +00:00
if apiContext.Filter(&types.QueryOptions{
2017-11-21 20:46:30 +00:00
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, existing) == nil {
return httperror.NewAPIError(httperror.NotFound, "failed to find "+id)
}
return nil
}