validators: 默认将pending deleted的模型排除在外

This commit is contained in:
Yousong Zhou
2019-06-24 10:44:55 +00:00
parent 7bab7724a9
commit c0b080ab66
2 changed files with 23 additions and 10 deletions

View File

@@ -381,11 +381,12 @@ func NewNonNegativeValidator(key string) *ValidatorRange {
type ValidatorModelIdOrName struct {
Validator
ModelKeyword string
OwnerId mcclient.IIdentityProvider
ModelManager db.IModelManager
Model db.IModel
modelIdKey string
ModelKeyword string
OwnerId mcclient.IIdentityProvider
ModelManager db.IModelManager
Model db.IModel
modelIdKey string
noPendingDeleted bool
}
func (v *ValidatorModelIdOrName) GetProjectId() string {
@@ -434,10 +435,11 @@ func (v *ValidatorModelIdOrName) getValue() interface{} {
func NewModelIdOrNameValidator(key string, modelKeyword string, ownerId mcclient.IIdentityProvider) *ValidatorModelIdOrName {
v := &ValidatorModelIdOrName{
Validator: Validator{Key: key},
OwnerId: ownerId,
ModelKeyword: modelKeyword,
modelIdKey: key + "_id",
Validator: Validator{Key: key},
OwnerId: ownerId,
ModelKeyword: modelKeyword,
modelIdKey: key + "_id",
noPendingDeleted: true,
}
v.SetParent(v)
return v
@@ -448,6 +450,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
@@ -466,6 +474,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
}