1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-09 11:17:48 +00:00
norman/api/access/list.go
Vatsal Parekh 70595af083
[release/v0.4] Upgrade go version to 1.23 (#587)
* Upgrade go version to 1.23

Signed-off-by: Vatsal Parekh <vatsalparekh@outlook.com>

* Upgrade golangci-lint to 1.63.4

Signed-off-by: Vatsal Parekh <vatsalparekh@outlook.com>

* correct linter issues

Signed-off-by: Vatsal Parekh <vatsalparekh@outlook.com>

---------

Signed-off-by: Vatsal Parekh <vatsalparekh@outlook.com>
2025-01-29 08:53:47 -05:00

88 lines
1.9 KiB
Go

package access
import (
"fmt"
"github.com/rancher/norman/parse/builder"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
)
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)
}
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)
}
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)
}
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
}
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 %s", 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)
}