Skip to content

Commit

Permalink
Make all status fields readonly by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Dec 28, 2017
1 parent 4944bcd commit 0d05740
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion types/mapper/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewObject(mappers ...types.Mapper) Object {
Mappers: append([]types.Mapper{
&Embed{Field: "metadata"},
&Embed{Field: "spec", Optional: true},
&ReadOnly{Field: "status", Optional: true},
&ReadOnly{Field: "status", Optional: true, SubFields: true},
Drop{"kind"},
Drop{"apiVersion"},
&Scope{
Expand Down
31 changes: 23 additions & 8 deletions types/mapper/read_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

type ReadOnly struct {
Field string
Optional bool
Field string
Optional bool
SubFields bool
}

func (r ReadOnly) FromInternal(data map[string]interface{}) {
Expand All @@ -15,12 +16,28 @@ func (r ReadOnly) FromInternal(data map[string]interface{}) {
func (r ReadOnly) ToInternal(data map[string]interface{}) {
}

func (r ReadOnly) readOnly(field types.Field, schema *types.Schema, schemas *types.Schemas) types.Field {
field.Create = false
field.Update = false

if r.SubFields {
subSchema := schemas.Schema(&schema.Version, field.Type)
if subSchema != nil {
for name, field := range subSchema.ResourceFields {
field.Create = false
field.Update = false
subSchema.ResourceFields[name] = field
}
}
}

return field
}

func (r ReadOnly) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
if r.Field == "*" {
for name, field := range schema.ResourceFields {
field.Create = false
field.Update = false
schema.ResourceFields[name] = field
schema.ResourceFields[name] = r.readOnly(field, schema, schemas)
}
return nil
}
Expand All @@ -33,9 +50,7 @@ func (r ReadOnly) ModifySchema(schema *types.Schema, schemas *types.Schemas) err
}

field := schema.ResourceFields[r.Field]
field.Create = false
field.Update = false
schema.ResourceFields[r.Field] = field
schema.ResourceFields[r.Field] = r.readOnly(field, schema, schemas)

return nil
}

0 comments on commit 0d05740

Please sign in to comment.