From a09f7e553694e12ef5dc3b3f367fd84c0cd07d2b Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Mon, 24 Jun 2019 10:44:55 +0000 Subject: [PATCH] =?UTF-8?q?validators:=20=E9=BB=98=E8=AE=A4=E5=B0=86pendin?= =?UTF-8?q?g=20deleted=E7=9A=84=E6=A8=A1=E5=9E=8B=E6=8E=92=E9=99=A4?= =?UTF-8?q?=E5=9C=A8=E5=A4=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cloudcommon/validators/errors.go | 2 +- pkg/cloudcommon/validators/validators.go | 33 +++++++++++++++++------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pkg/cloudcommon/validators/errors.go b/pkg/cloudcommon/validators/errors.go index e5d519c9d9..6abf9c142d 100644 --- a/pkg/cloudcommon/validators/errors.go +++ b/pkg/cloudcommon/validators/errors.go @@ -113,7 +113,7 @@ func newModelManagerError(modelKeyword string) error { func newModelNotFoundError(modelKeyword, idOrName string, err error) error { errFmt := "cannot find %q with id/name %q" params := []interface{}{modelKeyword, idOrName} - if err != sql.ErrNoRows { + if err != nil && err != sql.ErrNoRows { errFmt += ": %s" params = append(params, err.Error()) } diff --git a/pkg/cloudcommon/validators/validators.go b/pkg/cloudcommon/validators/validators.go index a731989f53..204a20c7d7 100644 --- a/pkg/cloudcommon/validators/validators.go +++ b/pkg/cloudcommon/validators/validators.go @@ -375,12 +375,13 @@ func NewNonNegativeValidator(key string) *ValidatorRange { type ValidatorModelIdOrName struct { Validator - ModelKeyword string - ProjectId string - UserId string - ModelManager db.IModelManager - Model db.IModel - modelIdKey string + ModelKeyword string + ProjectId string + UserId string + ModelManager db.IModelManager + Model db.IModel + modelIdKey string + noPendingDeleted bool } func (v *ValidatorModelIdOrName) GetProjectId() string { @@ -401,10 +402,11 @@ func (v *ValidatorModelIdOrName) getValue() interface{} { func NewModelIdOrNameValidator(key string, modelKeyword string, projectId string) *ValidatorModelIdOrName { v := &ValidatorModelIdOrName{ - Validator: Validator{Key: key}, - ProjectId: projectId, - ModelKeyword: modelKeyword, - modelIdKey: key + "_id", + Validator: Validator{Key: key}, + ProjectId: projectId, + ModelKeyword: modelKeyword, + modelIdKey: key + "_id", + noPendingDeleted: true, } v.parent = v return v @@ -415,6 +417,12 @@ func (v *ValidatorModelIdOrName) ModelIdKey(modelIdKey string) *ValidatorModelId return v } +// AllowPendingDeleted allows the to-be-validated id or name to be of a pending deleted model +func (v *ValidatorModelIdOrName) AllowPendingDeleted(b bool) *ValidatorModelIdOrName { + v.noPendingDeleted = !b + return v +} + func (v *ValidatorModelIdOrName) validate(data *jsonutils.JSONDict) error { if err, isSet := v.Validator.validateEx(data); err != nil || !isSet { return err @@ -433,6 +441,11 @@ func (v *ValidatorModelIdOrName) validate(data *jsonutils.JSONDict) error { if err != nil { return newModelNotFoundError(v.ModelKeyword, modelIdOrName, err) } + if v.noPendingDeleted { + if pd, ok := model.(db.IPendingDeletable); ok && pd.GetPendingDeleted() { + return newModelNotFoundError(v.ModelKeyword, modelIdOrName, nil) + } + } v.Model = model return nil }