1
0
mirror of https://github.com/rancher/norman.git synced 2025-06-25 15:02:07 +00:00
norman/api/access/list.go

88 lines
1.9 KiB
Go
Raw Permalink Normal View History

2017-12-12 15:48:17 +00:00
package access
import (
"fmt"
"github.com/rancher/norman/parse/builder"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
)
2018-04-20 04:55:54 +00:00
func Create(context *types.APIContext, version *types.APIVersion, typeName string, data map[string]interface{}, into interface{}) error {
schema := context.Schemas.Schema(version, typeName)
if schema == nil {
return fmt.Errorf("failed to find schema %s", typeName)
2018-04-20 04:55:54 +00:00
}
item, err := schema.Store.Create(context, schema, data)
if err != nil {
return err
}
b := builder.NewBuilder(context)
b.Version = version
item, err = b.Construct(schema, item, builder.List)
if err != nil {
return err
}
if into == nil {
return nil
}
return convert.ToObj(item, into)
}
2017-12-12 15:48:17 +00:00
func ByID(context *types.APIContext, version *types.APIVersion, typeName string, id string, into interface{}) error {
schema := context.Schemas.Schema(version, typeName)
if schema == nil {
return fmt.Errorf("failed to find schema %s", typeName)
2017-12-12 15:48:17 +00:00
}
item, err := schema.Store.ByID(context, schema, id)
if err != nil {
return err
}
b := builder.NewBuilder(context)
b.Version = version
item, err = b.Construct(schema, item, builder.List)
if err != nil {
return err
}
if into == nil {
return nil
}
2017-12-12 15:48:17 +00:00
return convert.ToObj(item, into)
}
2017-12-28 15:47:10 +00:00
func List(context *types.APIContext, version *types.APIVersion, typeName string, opts *types.QueryOptions, into interface{}) error {
2017-12-12 15:48:17 +00:00
schema := context.Schemas.Schema(version, typeName)
if schema == nil {
return fmt.Errorf("failed to find schema %s", typeName)
2017-12-12 15:48:17 +00:00
}
data, err := schema.Store.List(context, schema, opts)
if err != nil {
return err
}
b := builder.NewBuilder(context)
b.Version = version
var newData []map[string]interface{}
for _, item := range data {
item, err = b.Construct(schema, item, builder.List)
if err != nil {
return err
}
newData = append(newData, item)
}
return convert.ToObj(newData, into)
}