1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-14 14:22:04 +00:00

Adding ability to replace schemas

This change makes it so that schemas can be replaced when they already exist.
This is to support the ability for kontainer drivers to update their dynamic
schemas.

Issue:
https://github.com/rancher/rancher/issues/17712
This commit is contained in:
Nathan Jenan
2019-02-05 13:20:38 -07:00
committed by Craig Jellick
parent 2d37b1235b
commit 362802224f
2 changed files with 43 additions and 25 deletions

View File

@@ -108,10 +108,16 @@ func (s *Schemas) removeReferences(schema *Schema) {
func (s *Schemas) AddSchema(schema Schema) *Schemas { func (s *Schemas) AddSchema(schema Schema) *Schemas {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
return s.doAddSchema(schema) return s.doAddSchema(schema, false)
} }
func (s *Schemas) doAddSchema(schema Schema) *Schemas { func (s *Schemas) ForceAddSchema(schema Schema) *Schemas {
s.Lock()
defer s.Unlock()
return s.doAddSchema(schema, true)
}
func (s *Schemas) doAddSchema(schema Schema, replace bool) *Schemas {
s.setupDefaults(&schema) s.setupDefaults(&schema)
if s.AddHook != nil { if s.AddHook != nil {
@@ -125,9 +131,20 @@ func (s *Schemas) doAddSchema(schema Schema) *Schemas {
s.versions = append(s.versions, schema.Version) s.versions = append(s.versions, schema.Version)
} }
if _, ok := schemas[schema.ID]; !ok { if _, ok := schemas[schema.ID]; !ok ||
(replace && schema.DynamicSchemaVersion != schemas[schema.ID].DynamicSchemaVersion) {
schemas[schema.ID] = &schema schemas[schema.ID] = &schema
s.schemas = append(s.schemas, &schema)
if replace {
for i, candidate := range s.schemas {
if candidate.ID == schema.ID {
s.schemas[i] = &schema
break
}
}
} else {
s.schemas = append(s.schemas, &schema)
}
if !schema.Embed { if !schema.Embed {
s.addReferences(&schema) s.addReferences(&schema)
@@ -159,7 +176,7 @@ func (s *Schemas) removeEmbed(schema *Schema) {
} }
s.doRemoveSchema(*target) s.doRemoveSchema(*target)
s.doAddSchema(newSchema) s.doAddSchema(newSchema, false)
} }
func (s *Schemas) embed(schema *Schema) { func (s *Schemas) embed(schema *Schema) {
@@ -184,7 +201,7 @@ func (s *Schemas) embed(schema *Schema) {
} }
s.doRemoveSchema(*target) s.doRemoveSchema(*target)
s.doAddSchema(newSchema) s.doAddSchema(newSchema, false)
} }
func (s *Schemas) addReferences(schema *Schema) { func (s *Schemas) addReferences(schema *Schema) {

View File

@@ -94,25 +94,26 @@ var NamespaceScope TypeScope = "namespace"
type TypeScope string type TypeScope string
type Schema struct { type Schema struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
Embed bool `json:"embed,omitempty"` Embed bool `json:"embed,omitempty"`
EmbedType string `json:"embedType,omitempty"` EmbedType string `json:"embedType,omitempty"`
CodeName string `json:"-"` CodeName string `json:"-"`
CodeNamePlural string `json:"-"` CodeNamePlural string `json:"-"`
PkgName string `json:"-"` PkgName string `json:"-"`
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
BaseType string `json:"baseType,omitempty"` BaseType string `json:"baseType,omitempty"`
Links map[string]string `json:"links"` Links map[string]string `json:"links"`
Version APIVersion `json:"version"` Version APIVersion `json:"version"`
PluralName string `json:"pluralName,omitempty"` PluralName string `json:"pluralName,omitempty"`
ResourceMethods []string `json:"resourceMethods,omitempty"` ResourceMethods []string `json:"resourceMethods,omitempty"`
ResourceFields map[string]Field `json:"resourceFields"` ResourceFields map[string]Field `json:"resourceFields"`
ResourceActions map[string]Action `json:"resourceActions,omitempty"` ResourceActions map[string]Action `json:"resourceActions,omitempty"`
CollectionMethods []string `json:"collectionMethods,omitempty"` CollectionMethods []string `json:"collectionMethods,omitempty"`
CollectionFields map[string]Field `json:"collectionFields,omitempty"` CollectionFields map[string]Field `json:"collectionFields,omitempty"`
CollectionActions map[string]Action `json:"collectionActions,omitempty"` CollectionActions map[string]Action `json:"collectionActions,omitempty"`
CollectionFilters map[string]Filter `json:"collectionFilters,omitempty"` CollectionFilters map[string]Filter `json:"collectionFilters,omitempty"`
Scope TypeScope `json:"-"` DynamicSchemaVersion string `json:"dynamicSchemaVersion,omitempty"`
Scope TypeScope `json:"-"`
InternalSchema *Schema `json:"-"` InternalSchema *Schema `json:"-"`
Mapper Mapper `json:"-"` Mapper Mapper `json:"-"`