1
0
mirror of https://github.com/rancher/types.git synced 2025-08-31 21:00:16 +00:00

Update vendor

This commit is contained in:
Darren Shepherd
2017-12-28 09:56:37 -07:00
parent 84e83e6ac9
commit e08e1fbd27
7 changed files with 87 additions and 21 deletions

View File

@@ -38,6 +38,27 @@ func (c Cond) IsUnknown(obj runtime.Object) bool {
func (c Cond) Reason(obj runtime.Object, reason string) {
cond := findOrCreateCond(obj, string(c))
getFieldValue(cond, "Reason").SetString(reason)
}
func (c Cond) Message(obj runtime.Object, message string) {
cond := findOrCreateCond(obj, string(c))
v := getFieldValue(cond, "Message")
if v.IsValid() {
v.SetString(message)
}
}
func (c Cond) ReasonError(obj runtime.Object, err error) {
cond := findOrCreateCond(obj, string(c))
if ce, ok := err.(*conditionError); ok {
getFieldValue(cond, "Reason").SetString(ce.reason)
v := getFieldValue(cond, "Message")
if v.IsValid() {
v.SetString(ce.Error())
}
} else {
getFieldValue(cond, "Reason").SetString(err.Error())
}
touchTS(cond)
}
@@ -65,14 +86,14 @@ func (c Cond) Once(obj runtime.Object, f func() (runtime.Object, error)) (runtim
if err != nil {
c.False(obj)
c.Reason(obj, err.Error())
c.ReasonError(obj, err)
return obj, err
}
c.True(obj)
return obj, nil
}
func (c Cond) Do(obj runtime.Object, f func() (runtime.Object, error)) error {
func (c Cond) Do(obj runtime.Object, f func() (runtime.Object, error)) (runtime.Object, error) {
c.Unknown(obj)
newObj, err := f()
if newObj != nil {
@@ -81,11 +102,13 @@ func (c Cond) Do(obj runtime.Object, f func() (runtime.Object, error)) error {
if err != nil {
c.False(obj)
c.Reason(obj, err.Error())
return err
c.ReasonError(obj, err)
return obj, err
}
c.True(obj)
return nil
c.Reason(obj, "")
c.Message(obj, "")
return obj, nil
}
func touchTS(value reflect.Value) {
@@ -152,3 +175,19 @@ func getFieldValue(v reflect.Value, name ...string) reflect.Value {
}
return getFieldValue(field, name[1:]...)
}
func Error(reason string, err error) error {
return &conditionError{
reason: reason,
message: err.Error(),
}
}
type conditionError struct {
reason string
message string
}
func (e *conditionError) Error() string {
return e.message
}

View File

@@ -76,6 +76,14 @@ func Capitalize(s string) string {
return strings.ToUpper(s[:1]) + s[1:]
}
func Uncapitalize(s string) string {
if len(s) <= 1 {
return strings.ToLower(s)
}
return strings.ToLower(s[:1]) + s[1:]
}
func LowerTitle(input string) string {
runes := []rune(input)
for i := 0; i < len(runes); i++ {

View File

@@ -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{

View File

@@ -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{}) {
@@ -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
}
@@ -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
}

View File

@@ -25,6 +25,10 @@ func (s *Schema) CanList() bool {
return slice.ContainsString(s.CollectionMethods, http.MethodGet)
}
func (s *Schema) CanCreate() bool {
return slice.ContainsString(s.CollectionMethods, http.MethodPost)
}
func (s *Schema) CanUpdate() bool {
return slice.ContainsString(s.ResourceMethods, http.MethodPut)
}

View File

@@ -44,7 +44,7 @@ type ActionHandler func(actionName string, action *Action, request *APIContext)
type RequestHandler func(request *APIContext) error
type QueryFilter func(opts QueryOptions, data []map[string]interface{}) []map[string]interface{}
type QueryFilter func(opts *QueryOptions, data []map[string]interface{}) []map[string]interface{}
type Validator func(request *APIContext, data map[string]interface{}) error
@@ -100,11 +100,11 @@ func (r *APIContext) WriteResponse(code int, obj interface{}) {
r.ResponseWriter.Write(r, code, obj)
}
func (r *APIContext) FilterList(opts QueryOptions, obj []map[string]interface{}) []map[string]interface{} {
func (r *APIContext) FilterList(opts *QueryOptions, obj []map[string]interface{}) []map[string]interface{} {
return r.QueryFilter(opts, obj)
}
func (r *APIContext) FilterObject(opts QueryOptions, obj map[string]interface{}) map[string]interface{} {
func (r *APIContext) FilterObject(opts *QueryOptions, obj map[string]interface{}) map[string]interface{} {
opts.Pagination = nil
result := r.QueryFilter(opts, []map[string]interface{}{obj})
if len(result) == 0 {
@@ -113,7 +113,7 @@ func (r *APIContext) FilterObject(opts QueryOptions, obj map[string]interface{})
return result[0]
}
func (r *APIContext) Filter(opts QueryOptions, obj interface{}) interface{} {
func (r *APIContext) Filter(opts *QueryOptions, obj interface{}) interface{} {
switch v := obj.(type) {
case []map[string]interface{}:
return r.FilterList(opts, v)
@@ -161,9 +161,9 @@ type URLBuilder interface {
type Store interface {
ByID(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
List(apiContext *APIContext, schema *Schema, opt QueryOptions) ([]map[string]interface{}, error)
List(apiContext *APIContext, schema *Schema, opt *QueryOptions) ([]map[string]interface{}, error)
Create(apiContext *APIContext, schema *Schema, data map[string]interface{}) (map[string]interface{}, error)
Update(apiContext *APIContext, schema *Schema, data map[string]interface{}, id string) (map[string]interface{}, error)
Delete(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
Watch(apiContext *APIContext, schema *Schema, opt QueryOptions) (chan map[string]interface{}, error)
Watch(apiContext *APIContext, schema *Schema, opt *QueryOptions) (chan map[string]interface{}, error)
}