From ff86ae0705c5757003779e310fca653321b1eb83 Mon Sep 17 00:00:00 2001 From: markturansky Date: Wed, 1 Apr 2015 14:42:16 -0400 Subject: [PATCH] Fixed RESTCreate/Update interface methods --- pkg/registry/persistentvolume/etcd/etcd.go | 2 +- pkg/registry/persistentvolume/rest.go | 17 ++++++-- .../persistentvolumeclaim/etcd/etcd.go | 2 +- pkg/registry/persistentvolumeclaim/rest.go | 41 ++++++------------- 4 files changed, 28 insertions(+), 34 deletions(-) diff --git a/pkg/registry/persistentvolume/etcd/etcd.go b/pkg/registry/persistentvolume/etcd/etcd.go index fa9ce0e0c12..69baf3b34db 100644 --- a/pkg/registry/persistentvolume/etcd/etcd.go +++ b/pkg/registry/persistentvolume/etcd/etcd.go @@ -56,7 +56,7 @@ func NewStorage(h tools.EtcdHelper) (*REST, *StatusREST) { } store.CreateStrategy = persistentvolume.Strategy - store.UpdateStrategy = persistentvolume.StatusStrategy + store.UpdateStrategy = persistentvolume.Strategy store.ReturnDeletedObject = true statusStore := *store diff --git a/pkg/registry/persistentvolume/rest.go b/pkg/registry/persistentvolume/rest.go index b15e58f07d4..fd17bae5ba5 100644 --- a/pkg/registry/persistentvolume/rest.go +++ b/pkg/registry/persistentvolume/rest.go @@ -38,34 +38,43 @@ type persistentvolumeStrategy struct { // objects via the REST API. var Strategy = persistentvolumeStrategy{api.Scheme, api.SimpleNameGenerator} -// NamespaceScoped is false for persistentvolumes. func (persistentvolumeStrategy) NamespaceScoped() bool { return false } -// ResetBeforeCreate clears fields that are not allowed to be set by end users on creation. +// ResetBeforeCreate clears the Status field which is not allowed to be set by end users on creation. func (persistentvolumeStrategy) PrepareForCreate(obj runtime.Object) { pv := obj.(*api.PersistentVolume) pv.Status = api.PersistentVolumeStatus{} } -// Validate validates a new persistentvolume. func (persistentvolumeStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { persistentvolume := obj.(*api.PersistentVolume) return validation.ValidatePersistentVolume(persistentvolume) } -// AllowCreateOnUpdate is false for persistentvolumes. func (persistentvolumeStrategy) AllowCreateOnUpdate() bool { return false } +// PrepareForUpdate sets the Status fields which is not allowed to be set by an end user updating a PV +func (persistentvolumeStrategy) PrepareForUpdate(obj, old runtime.Object) { + newPv := obj.(*api.PersistentVolume) + oldPv := obj.(*api.PersistentVolume) + newPv.Status = oldPv.Status +} + +func (persistentvolumeStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { + return validation.ValidatePersistentVolumeUpdate(obj.(*api.PersistentVolume), old.(*api.PersistentVolume)) +} + type persistentvolumeStatusStrategy struct { persistentvolumeStrategy } var StatusStrategy = persistentvolumeStatusStrategy{Strategy} +// PrepareForUpdate sets the Spec field which is not allowed to be changed when updating a PV's Status func (persistentvolumeStatusStrategy) PrepareForUpdate(obj, old runtime.Object) { newPv := obj.(*api.PersistentVolume) oldPv := obj.(*api.PersistentVolume) diff --git a/pkg/registry/persistentvolumeclaim/etcd/etcd.go b/pkg/registry/persistentvolumeclaim/etcd/etcd.go index 9b57cabf176..ada9ce88d89 100644 --- a/pkg/registry/persistentvolumeclaim/etcd/etcd.go +++ b/pkg/registry/persistentvolumeclaim/etcd/etcd.go @@ -56,7 +56,7 @@ func NewStorage(h tools.EtcdHelper) *REST { } store.CreateStrategy = persistentvolumeclaim.Strategy - store.UpdateStrategy = persistentvolumeclaim.StatusStrategy + store.UpdateStrategy = persistentvolumeclaim.Strategy store.ReturnDeletedObject = true return &REST{store} diff --git a/pkg/registry/persistentvolumeclaim/rest.go b/pkg/registry/persistentvolumeclaim/rest.go index fafe02b365f..f7f685e75c0 100644 --- a/pkg/registry/persistentvolumeclaim/rest.go +++ b/pkg/registry/persistentvolumeclaim/rest.go @@ -38,49 +38,34 @@ type persistentvolumeclaimStrategy struct { // objects via the REST API. var Strategy = persistentvolumeclaimStrategy{api.Scheme, api.SimpleNameGenerator} -// NamespaceScoped is true for persistentvolumeclaims. func (persistentvolumeclaimStrategy) NamespaceScoped() bool { return true } -// ResetBeforeCreate clears fields that are not allowed to be set by end users on creation. +// PrepareForCreate clears the Status field which is not allowed to be set by end users on creation. func (persistentvolumeclaimStrategy) PrepareForCreate(obj runtime.Object) { pv := obj.(*api.PersistentVolumeClaim) pv.Status = api.PersistentVolumeClaimStatus{} } -// PrepareForUpdate clears fields that are not allowed to be set by end users on update. +func (persistentvolumeclaimStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { + pvc := obj.(*api.PersistentVolumeClaim) + return validation.ValidatePersistentVolumeClaim(pvc) +} + +func (persistentvolumeclaimStrategy) AllowCreateOnUpdate() bool { + return false +} + +// PrepareForUpdate sets the Status field which is not allowed to be set by end users on update func (persistentvolumeclaimStrategy) PrepareForUpdate(obj, old runtime.Object) { newPvc := obj.(*api.PersistentVolumeClaim) oldPvc := obj.(*api.PersistentVolumeClaim) newPvc.Status = oldPvc.Status } -// Validate validates a new persistentvolumeclaim. -func (persistentvolumeclaimStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList { - pvc := obj.(*api.PersistentVolumeClaim) - return validation.ValidatePersistentVolumeClaim(pvc) -} - -// AllowCreateOnUpdate is false for persistentvolumeclaims. -func (persistentvolumeclaimStrategy) AllowCreateOnUpdate() bool { - return false -} - -type persistentvolumeclaimStatusStrategy struct { - persistentvolumeclaimStrategy -} - -var StatusStrategy = persistentvolumeclaimStatusStrategy{Strategy} - -func (persistentvolumeclaimStatusStrategy) PrepareForUpdate(obj, old runtime.Object) { - newPvc := obj.(*api.PersistentVolumeClaim) - oldPvc := obj.(*api.PersistentVolumeClaim) - newPvc.Spec = oldPvc.Spec -} - -func (persistentvolumeclaimStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { - return validation.ValidatePersistentVolumeClaimStatusUpdate(obj.(*api.PersistentVolumeClaim), old.(*api.PersistentVolumeClaim)) +func (persistentvolumeclaimStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList { + return validation.ValidatePersistentVolumeClaimUpdate(obj.(*api.PersistentVolumeClaim), old.(*api.PersistentVolumeClaim)) } // MatchPersistentVolumeClaim returns a generic matcher for a given label and field selector.