1
0
mirror of https://github.com/rancher/norman.git synced 2025-06-26 07:23:44 +00:00
norman/api/access/list.go
2017-12-12 08:48:17 -07:00

58 lines
1.3 KiB
Go

package access
import (
"fmt"
"github.com/rancher/norman/parse/builder"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
)
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 " + typeName)
}
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
}
return convert.ToObj(item, into)
}
func List(context *types.APIContext, version *types.APIVersion, typeName string, opts types.QueryOptions, into interface{}) error {
schema := context.Schemas.Schema(version, typeName)
if schema == nil {
return fmt.Errorf("failed to find schema " + typeName)
}
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)
}