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

This commit is contained in:
Yousong Zhou
2019-06-24 10:44:55 +00:00
parent 1d7bcc43a5
commit a09f7e5536
2 changed files with 24 additions and 11 deletions

View File

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

View File

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