2017-11-21 20:46:30 +00:00
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/rancher/norman/httperror"
|
|
|
|
"github.com/rancher/norman/types"
|
2018-01-17 04:46:20 +00:00
|
|
|
"github.com/rancher/norman/types/convert"
|
2017-11-21 20:46:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Wrap(store types.Store) types.Store {
|
2019-04-07 00:41:18 +00:00
|
|
|
if _, ok := store.(*StoreWrapper); ok {
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:46:30 +00:00
|
|
|
return &StoreWrapper{
|
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type StoreWrapper struct {
|
|
|
|
store types.Store
|
|
|
|
}
|
|
|
|
|
2018-02-09 20:31:12 +00:00
|
|
|
func (s *StoreWrapper) Context() types.StorageContext {
|
|
|
|
return s.store.Context()
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:46:30 +00:00
|
|
|
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),
|
2018-05-15 22:24:07 +00:00
|
|
|
}, schema, data), nil
|
2017-11-21 20:46:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:24:07 +00:00
|
|
|
return apiContext.FilterList(opts, schema, data), nil
|
2017-11-21 20:46:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-01-17 04:46:20 +00:00
|
|
|
return convert.Chan(c, func(data map[string]interface{}) map[string]interface{} {
|
|
|
|
return apiContext.FilterObject(&types.QueryOptions{
|
|
|
|
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
|
2018-05-15 22:24:07 +00:00
|
|
|
}, schema, data)
|
2018-01-17 04:46:20 +00:00
|
|
|
}), nil
|
2017-11-28 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
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),
|
2018-05-15 22:24:07 +00:00
|
|
|
}, schema, data), nil
|
2017-11-21 20:46:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 08:25:01 +00:00
|
|
|
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 {
|
2017-12-16 08:25:01 +00:00
|
|
|
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),
|
2018-05-15 22:24:07 +00:00
|
|
|
}, schema, existing) == nil {
|
2017-11-21 20:46:30 +00:00
|
|
|
return httperror.NewAPIError(httperror.NotFound, "failed to find "+id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|