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

101 lines
2.5 KiB
Go
Raw Normal View History

2017-11-11 04:44:02 +00:00
package schema
import (
"encoding/json"
"strings"
"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"
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) {
schemaData := map[string]interface{}{}
data, err := json.Marshal(schema)
if err != nil {
return nil, err
}
return schemaData, json.Unmarshal(data, &schemaData)
}
}
return nil, nil
}
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.CanList() {
schemas = addSchema(schema, schemaMap, schemas, included)
}
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 addSchema(schema *types.Schema, schemaMap map[string]*types.Schema, schemas []*types.Schema, included map[string]bool) []*types.Schema {
included[schema.ID] = true
schemas = traverseAndAdd(schema, schemaMap, schemas, included)
schemas = append(schemas, schema)
return schemas
}
func traverseAndAdd(schema *types.Schema, schemaMap map[string]*types.Schema, schemas []*types.Schema, included map[string]bool) []*types.Schema {
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 = addSchema(refSchema, schemaMap, schemas, included)
}
}
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 = addSchema(refSchema, schemaMap, schemas, included)
}
}
}
return schemas
}