mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-06-10 00:27:11 +08:00
validators: 默认将pending deleted的模型排除在外
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user