1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-17 14:56:42 +00:00
norman/store/schema/schema_store.go

153 lines
4.5 KiB
Go
Raw Normal View History

2017-11-11 04:44:02 +00:00
package schema
import (
"encoding/json"
"net/http"
2017-11-11 04:44:02 +00:00
"strings"
2018-02-14 22:48:47 +00:00
"github.com/rancher/norman/httperror"
2017-11-11 04:44:02 +00:00
"github.com/rancher/norman/store/empty"
"github.com/rancher/norman/types"
2017-11-28 21:28:25 +00:00
"github.com/rancher/norman/types/definition"
"github.com/rancher/norman/types/slice"
2017-11-11 04:44:02 +00:00
)
type Store struct {
empty.Store
}
func NewSchemaStore() types.Store {
return &Store{}
}
func (s *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
2017-11-21 20:46:30 +00:00
for _, schema := range apiContext.Schemas.SchemasForVersion(*apiContext.Version) {
2017-11-11 04:44:02 +00:00
if strings.EqualFold(schema.ID, id) {
if schema.Enabled != nil {
if !schema.Enabled() {
return nil, httperror.NewAPIError(httperror.NotFound, "schema disabled")
}
}
2017-11-11 04:44:02 +00:00
schemaData := map[string]interface{}{}
data, err := json.Marshal(s.modifyForAccessControl(apiContext, *schema))
2017-11-11 04:44:02 +00:00
if err != nil {
return nil, err
}
return schemaData, json.Unmarshal(data, &schemaData)
}
}
2018-02-14 22:48:47 +00:00
return nil, httperror.NewAPIError(httperror.NotFound, "no such schema")
2017-11-11 04:44:02 +00:00
}
func (s *Store) modifyForAccessControl(context *types.APIContext, schema types.Schema) *types.Schema {
var resourceMethods []string
if slice.ContainsString(schema.ResourceMethods, http.MethodPut) && schema.CanUpdate(context) == nil {
resourceMethods = append(resourceMethods, http.MethodPut)
}
if slice.ContainsString(schema.ResourceMethods, http.MethodDelete) && schema.CanDelete(context) == nil {
resourceMethods = append(resourceMethods, http.MethodDelete)
}
if slice.ContainsString(schema.ResourceMethods, http.MethodGet) && schema.CanGet(context) == nil {
2018-02-14 22:49:24 +00:00
resourceMethods = append(resourceMethods, http.MethodGet)
}
var collectionMethods []string
if slice.ContainsString(schema.CollectionMethods, http.MethodPost) && schema.CanCreate(context) == nil {
collectionMethods = append(collectionMethods, http.MethodPost)
}
if slice.ContainsString(schema.CollectionMethods, http.MethodGet) && schema.CanList(context) == nil {
collectionMethods = append(collectionMethods, http.MethodGet)
}
schema.ResourceMethods = resourceMethods
schema.CollectionMethods = collectionMethods
return &schema
}
2017-12-28 15:47:10 +00:00
func (s *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
2017-11-28 21:28:25 +00:00
return nil, nil
}
2017-12-28 15:47:10 +00:00
func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
2017-11-21 20:46:30 +00:00
schemaMap := apiContext.Schemas.SchemasForVersion(*apiContext.Version)
schemas := make([]*types.Schema, 0, len(schemaMap))
schemaData := make([]map[string]interface{}, 0, len(schemaMap))
2017-11-11 04:44:02 +00:00
2017-11-28 21:28:25 +00:00
included := map[string]bool{}
2017-11-21 20:46:30 +00:00
for _, schema := range schemaMap {
2017-11-28 21:28:25 +00:00
if included[schema.ID] {
continue
}
if schema.Enabled != nil {
if !schema.Enabled() {
continue
}
}
if schema.CanList(apiContext) == nil || schema.CanGet(apiContext) == nil {
schemas = s.addSchema(apiContext, schema, schemaMap, schemas, included)
2017-11-28 21:28:25 +00:00
}
2017-11-11 04:44:02 +00:00
}
2017-11-21 20:46:30 +00:00
data, err := json.Marshal(schemas)
if err != nil {
2017-11-11 04:44:02 +00:00
return nil, err
}
2017-11-21 20:46:30 +00:00
return schemaData, json.Unmarshal(data, &schemaData)
2017-11-11 04:44:02 +00:00
}
2017-11-28 21:28:25 +00:00
func (s *Store) addSchema(apiContext *types.APIContext, schema *types.Schema, schemaMap map[string]*types.Schema, schemas []*types.Schema, included map[string]bool) []*types.Schema {
2017-11-28 21:28:25 +00:00
included[schema.ID] = true
schemas = s.traverseAndAdd(apiContext, schema, schemaMap, schemas, included)
schemas = append(schemas, s.modifyForAccessControl(apiContext, *schema))
2017-11-28 21:28:25 +00:00
return schemas
}
func (s *Store) traverseAndAdd(apiContext *types.APIContext, schema *types.Schema, schemaMap map[string]*types.Schema, schemas []*types.Schema, included map[string]bool) []*types.Schema {
2017-11-28 21:28:25 +00:00
for _, field := range schema.ResourceFields {
2017-12-16 08:25:37 +00:00
t := ""
subType := field.Type
for subType != t {
t = subType
subType = definition.SubType(t)
2017-11-28 21:28:25 +00:00
}
if refSchema, ok := schemaMap[t]; ok && !included[t] {
schemas = s.addSchema(apiContext, refSchema, schemaMap, schemas, included)
2017-11-28 21:28:25 +00:00
}
}
for _, action := range schema.ResourceActions {
for _, t := range []string{action.Output, action.Input} {
if t == "" {
continue
}
if refSchema, ok := schemaMap[t]; ok && !included[t] {
schemas = s.addSchema(apiContext, refSchema, schemaMap, schemas, included)
2017-11-28 21:28:25 +00:00
}
}
}
2018-01-20 08:08:31 +00:00
for _, action := range schema.CollectionActions {
for _, t := range []string{action.Output, action.Input} {
if t == "" {
continue
}
if refSchema, ok := schemaMap[t]; ok && !included[t] {
schemas = s.addSchema(apiContext, refSchema, schemaMap, schemas, included)
}
}
}
2017-11-28 21:28:25 +00:00
return schemas
}