From 21ffa9047075c5a87bf82249541ec0f0c38ec07e Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Tue, 27 Feb 2018 15:24:24 -0700 Subject: [PATCH] Update vendor --- vendor.conf | 2 +- .../rancher/norman/httperror/error.go | 19 ++++++-- .../rancher/norman/httperror/handler.go | 4 ++ .../rancher/norman/store/proxy/proxy_store.go | 17 ------- .../rancher/norman/types/convert/convert.go | 4 ++ .../rancher/norman/types/convert/transform.go | 2 +- .../norman/types/mapper/annotation_field.go | 2 +- .../rancher/norman/types/mapper/batchmove.go | 46 +++++++++++++++++++ .../rancher/norman/types/mapper/embed.go | 20 ++++++-- .../rancher/norman/types/mapper/enum.go | 42 +++++------------ .../rancher/norman/types/reflection.go | 13 ++++++ 11 files changed, 111 insertions(+), 60 deletions(-) create mode 100644 vendor/github.com/rancher/norman/types/mapper/batchmove.go diff --git a/vendor.conf b/vendor.conf index 35cd2802..14a49710 100644 --- a/vendor.conf +++ b/vendor.conf @@ -5,4 +5,4 @@ k8s.io/kubernetes v1.8.3 bitbucket.org/ww/goautoneg a547fc61f48d567d5b4ec6f8aee5573d8efce11d https://github.com/rancher/goautoneg.git golang.org/x/sync fd80eb99c8f653c847d294a001bdf2a3a6f768f5 -github.com/rancher/norman 94b274ab753743ef3e8a7cca7bbf157d877d2789 +github.com/rancher/norman ee25c89cfd450e9256b251d43ecbb7d27670adfa diff --git a/vendor/github.com/rancher/norman/httperror/error.go b/vendor/github.com/rancher/norman/httperror/error.go index 5b0fec94..9edc7b75 100644 --- a/vendor/github.com/rancher/norman/httperror/error.go +++ b/vendor/github.com/rancher/norman/httperror/error.go @@ -5,6 +5,11 @@ import ( ) var ( + Unauthorized = ErrorCode{"Unauthorized", 401} + PermissionDenied = ErrorCode{"PermissionDenied", 403} + NotFound = ErrorCode{"NotFound", 404} + MethodNotAllowed = ErrorCode{"MethodNotAllow", 405} + InvalidDateFormat = ErrorCode{"InvalidDateFormat", 422} InvalidFormat = ErrorCode{"InvalidFormat", 422} InvalidReference = ErrorCode{"InvalidReference", 422} @@ -23,12 +28,9 @@ var ( InvalidType = ErrorCode{"InvalidType", 422} ActionNotAvailable = ErrorCode{"ActionNotAvailable", 404} InvalidState = ErrorCode{"InvalidState", 422} + ServerError = ErrorCode{"ServerError", 500} ClusterUnavailable = ErrorCode{"ClusterUnavailable", 503} - PermissionDenied = ErrorCode{"PermissionDenied", 403} - - MethodNotAllowed = ErrorCode{"MethodNotAllow", 405} - NotFound = ErrorCode{"NotFound", 404} ) type ErrorCode struct { @@ -69,6 +71,8 @@ func NewFieldAPIError(code ErrorCode, fieldName, message string) error { } } +// WrapFieldAPIError will cause the API framework to log the underlying err before returning the APIError as a response. +// err WILL NOT be in the API response func WrapFieldAPIError(err error, code ErrorCode, fieldName, message string) error { return &APIError{ Cause: err, @@ -78,6 +82,8 @@ func WrapFieldAPIError(err error, code ErrorCode, fieldName, message string) err } } +// WrapAPIError will cause the API framework to log the underlying err before returning the APIError as a response. +// err WILL NOT be in the API response func WrapAPIError(err error, code ErrorCode, message string) error { return &APIError{ code: code, @@ -92,3 +98,8 @@ func (a *APIError) Error() string { } return fmt.Sprintf("%s: %s", a.code, a.message) } + +func IsAPIError(err error) bool { + _, ok := err.(*APIError) + return ok +} diff --git a/vendor/github.com/rancher/norman/httperror/handler.go b/vendor/github.com/rancher/norman/httperror/handler.go index 62fbed3a..3db377dc 100644 --- a/vendor/github.com/rancher/norman/httperror/handler.go +++ b/vendor/github.com/rancher/norman/httperror/handler.go @@ -8,6 +8,10 @@ import ( func ErrorHandler(request *types.APIContext, err error) { var error *APIError if apiError, ok := err.(*APIError); ok { + if apiError.Cause != nil { + logrus.Errorf("API error response %v for %v %v. Cause: %v", apiError.code.Status, request.Request.Method, + request.Request.RequestURI, apiError.Cause) + } error = apiError } else { logrus.Errorf("Unknown error: %v", err) diff --git a/vendor/github.com/rancher/norman/store/proxy/proxy_store.go b/vendor/github.com/rancher/norman/store/proxy/proxy_store.go index f28749f9..5dd44579 100644 --- a/vendor/github.com/rancher/norman/store/proxy/proxy_store.go +++ b/vendor/github.com/rancher/norman/store/proxy/proxy_store.go @@ -108,23 +108,6 @@ func NewProxyStore(clientGetter ClientGetter, storageContext types.StorageContex } } -func NewRawProxyStore(clientGetter ClientGetter, storageContext types.StorageContext, - prefix []string, group, version, kind, resourcePlural string) *Store { - return &Store{ - clientGetter: clientGetter, - storageContext: storageContext, - prefix: prefix, - group: group, - version: version, - kind: kind, - resourcePlural: resourcePlural, - authContext: map[string]string{ - "apiGroup": group, - "resource": resourcePlural, - }, - } -} - func (p *Store) getUser(apiContext *types.APIContext) string { return apiContext.Request.Header.Get(userAuthHeader) } diff --git a/vendor/github.com/rancher/norman/types/convert/convert.go b/vendor/github.com/rancher/norman/types/convert/convert.go index ba382ce9..902e341d 100644 --- a/vendor/github.com/rancher/norman/types/convert/convert.go +++ b/vendor/github.com/rancher/norman/types/convert/convert.go @@ -198,6 +198,10 @@ func ToObj(data interface{}, into interface{}) error { } func EncodeToMap(obj interface{}) (map[string]interface{}, error) { + if m, ok := obj.(map[string]interface{}); ok { + return m, nil + } + b, err := json.Marshal(obj) if err != nil { return nil, err diff --git a/vendor/github.com/rancher/norman/types/convert/transform.go b/vendor/github.com/rancher/norman/types/convert/transform.go index 9bb8aa6f..3fd9b924 100644 --- a/vendor/github.com/rancher/norman/types/convert/transform.go +++ b/vendor/github.com/rancher/norman/types/convert/transform.go @@ -27,7 +27,7 @@ func Transform(data map[string]interface{}, path []string, transformer Transform // You can't end a path with ARRAY/MAP. Not supported right now if len(path) > 1 { - switch path[1] { + switch path[0] { case ArrayKey: for _, valueMap := range ToMapSlice(value) { Transform(valueMap, path[1:], transformer) diff --git a/vendor/github.com/rancher/norman/types/mapper/annotation_field.go b/vendor/github.com/rancher/norman/types/mapper/annotation_field.go index 821621a3..583a3e73 100644 --- a/vendor/github.com/rancher/norman/types/mapper/annotation_field.go +++ b/vendor/github.com/rancher/norman/types/mapper/annotation_field.go @@ -38,7 +38,7 @@ func (e AnnotationField) FromInternal(data map[string]interface{}) { func (e AnnotationField) ToInternal(data map[string]interface{}) { v, ok := data[e.Field] - if ok { + if ok && v != nil { if e.Object || e.List { if bytes, err := json.Marshal(v); err == nil { v = string(bytes) diff --git a/vendor/github.com/rancher/norman/types/mapper/batchmove.go b/vendor/github.com/rancher/norman/types/mapper/batchmove.go new file mode 100644 index 00000000..c0b4b7bb --- /dev/null +++ b/vendor/github.com/rancher/norman/types/mapper/batchmove.go @@ -0,0 +1,46 @@ +package mapper + +import ( + "path" + + "github.com/rancher/norman/types" +) + +type BatchMove struct { + From []string + To string + DestDefined bool + NoDeleteFromField bool + moves []Move +} + +func (b *BatchMove) FromInternal(data map[string]interface{}) { + for _, m := range b.moves { + m.FromInternal(data) + } +} + +func (b *BatchMove) ToInternal(data map[string]interface{}) { + for i := len(b.moves) - 1; i >= 0; i++ { + b.moves[i].ToInternal(data) + } +} + +func (b *BatchMove) ModifySchema(s *types.Schema, schemas *types.Schemas) error { + for _, from := range b.From { + b.moves = append(b.moves, Move{ + From: from, + To: path.Join(b.To, from), + DestDefined: b.DestDefined, + NoDeleteFromField: b.NoDeleteFromField, + }) + } + + for _, m := range b.moves { + if err := m.ModifySchema(s, schemas); err != nil { + return err + } + } + + return nil +} diff --git a/vendor/github.com/rancher/norman/types/mapper/embed.go b/vendor/github.com/rancher/norman/types/mapper/embed.go index 86415f1a..fd6ea08d 100644 --- a/vendor/github.com/rancher/norman/types/mapper/embed.go +++ b/vendor/github.com/rancher/norman/types/mapper/embed.go @@ -59,6 +59,7 @@ func (e *Embed) ModifySchema(schema *types.Schema, schemas *types.Schemas) error return fmt.Errorf("failed to find schema %s for embedding", embeddedSchemaID) } + deleteField := true for name, field := range embeddedSchema.ResourceFields { for _, ignore := range e.Ignore { if ignore == name { @@ -66,20 +67,29 @@ func (e *Embed) ModifySchema(schema *types.Schema, schemas *types.Schemas) error } } - if !e.ignoreOverride { - if _, ok := schema.ResourceFields[name]; ok { - return fmt.Errorf("embedding field %s on %s will overwrite the field %s", - e.Field, schema.ID, name) + if name == e.Field { + deleteField = false + } else { + if !e.ignoreOverride { + if _, ok := schema.ResourceFields[name]; ok { + return fmt.Errorf("embedding field %s on %s will overwrite the field %s", + e.Field, schema.ID, name) + } } } + if e.ReadOnly { field.Create = false field.Update = false } + schema.ResourceFields[name] = field e.embeddedFields = append(e.embeddedFields, name) } - delete(schema.ResourceFields, e.Field) + if deleteField { + delete(schema.ResourceFields, e.Field) + } + return nil } diff --git a/vendor/github.com/rancher/norman/types/mapper/enum.go b/vendor/github.com/rancher/norman/types/mapper/enum.go index 29b1cdb8..77c7c2da 100644 --- a/vendor/github.com/rancher/norman/types/mapper/enum.go +++ b/vendor/github.com/rancher/norman/types/mapper/enum.go @@ -2,47 +2,27 @@ package mapper import ( "github.com/rancher/norman/types" - "github.com/rancher/norman/types/convert" ) type Enum struct { - Field string - Values map[string][]string + Field string + Options []string } func (e Enum) FromInternal(data map[string]interface{}) { - v, ok := data[e.Field] - if !ok { - return - } - - str := convert.ToString(v) - - mapping, ok := e.Values[str] - if ok { - data[e.Field] = mapping[0] - } else { - data[e.Field] = v - } } func (e Enum) ToInternal(data map[string]interface{}) { - v, ok := data[e.Field] - if !ok { - return - } - - str := convert.ToString(v) - for newValue, values := range e.Values { - for _, value := range values { - if str == value { - data[e.Field] = newValue - return - } - } - } } func (e Enum) ModifySchema(schema *types.Schema, schemas *types.Schemas) error { - return ValidateField(e.Field, schema) + if err := ValidateField(e.Field, schema); err != nil { + return err + } + + f := schema.ResourceFields[e.Field] + f.Type = "enum" + f.Options = e.Options + schema.ResourceFields[e.Field] = f + return nil } diff --git a/vendor/github.com/rancher/norman/types/reflection.go b/vendor/github.com/rancher/norman/types/reflection.go index d8661d01..ef3f123e 100644 --- a/vendor/github.com/rancher/norman/types/reflection.go +++ b/vendor/github.com/rancher/norman/types/reflection.go @@ -284,6 +284,19 @@ func (s *Schemas) readFields(schema *Schema, t reflect.Type) error { schemaField.Type = inferedType } + if schemaField.Default != nil { + switch schemaField.Type { + case "int": + n, err := convert.ToNumber(schemaField.Default) + if err != nil { + return err + } + schemaField.Default = n + case "boolean": + schemaField.Default = convert.ToBool(schemaField.Default) + } + } + logrus.Debugf("Setting field %s.%s: %#v", schema.ID, fieldName, schemaField) schema.ResourceFields[fieldName] = schemaField }