Merge pull request #12537 from ioito/automated-cherry-pick-of-#12535-upstream-release-3.7

Automated cherry pick of #12535: fix(region): validate update
This commit is contained in:
Zexi Li
2021-10-28 16:36:48 +08:00
committed by GitHub
4 changed files with 20 additions and 23 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ type ModelBaseDetails struct {
CanUpdate bool `json:"can_update"`
// 资源不能更新的原因
UpdateFailReason string `json:"update_fail_reason"`
UpdateFailReason httperrors.Error `json:"update_fail_reason"`
}
type ModelBaseShortDescDetail struct {
+19
View File
@@ -312,6 +312,7 @@ func FetchCustomizeColumns(
jsonDict.Update(jsonutils.Marshal(objs[i]).(*jsonutils.JSONDict))
out := apis.ModelBaseDetails{
CanDelete: true,
CanUpdate: true,
}
err = ValidateDeleteCondition(objs[i].(IModel), ctx, jsonDict)
@@ -321,6 +322,13 @@ func FetchCustomizeColumns(
out.DeleteFailReason = httperrors.NewErrorFromGeneralError(ctx, err)
}
}
err = ValidateUpdateCondition(objs[i].(IModel), ctx)
if err != nil {
out.CanUpdate = false
if showReason {
out.UpdateFailReason = httperrors.NewErrorFromGeneralError(ctx, err)
}
}
jsonDict.Update(jsonutils.Marshal(out))
retVal[i] = jsonDict
@@ -365,3 +373,14 @@ func ValidateDeleteCondition(model IModel, ctx context.Context, data jsonutils.J
}
return ValueToError(ret[0])
}
func ValidateUpdateCondition(model IModel, ctx context.Context) error {
ret, err := call(model, "ValidateUpdateCondition", ctx)
if err != nil {
return httperrors.NewGeneralError(err)
}
if len(ret) != 1 {
return httperrors.NewInternalServerError("Invald ValidateUpdateCondition return value")
}
return ValueToError(ret[0])
}
-15
View File
@@ -30,7 +30,6 @@ import (
"yunion.io/x/pkg/utils"
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/appsrv"
"yunion.io/x/onecloud/pkg/appsrv/dispatcher"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
@@ -854,20 +853,6 @@ func (dispatcher *DBModelDispatcher) List(ctx context.Context, query jsonutils.J
return items, nil
}
func getModelExtraDetails(item IModel, ctx context.Context, showReason bool) apis.ModelBaseDetails {
out := apis.ModelBaseDetails{
CanUpdate: true,
}
err := item.ValidateUpdateCondition(ctx)
if err != nil {
out.CanUpdate = false
if showReason {
out.UpdateFailReason = err.Error()
}
}
return out
}
func getModelItemDetails(manager IModelManager, item IModel, ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, isHead bool) (jsonutils.JSONObject, error) {
appParams := appsrv.AppContextGetParams(ctx)
if appParams == nil && isHead {
-7
View File
@@ -310,14 +310,7 @@ func (manager *SModelBaseManager) FetchCustomizeColumns(
fields stringutils2.SSortedStrings,
isList bool,
) []apis.ModelBaseDetails {
showReason := false
if query.Contains("show_fail_reason") {
showReason = true
}
ret := make([]apis.ModelBaseDetails, len(objs))
for i := range objs {
ret[i] = getModelExtraDetails(objs[i].(IModel), ctx, showReason)
}
return ret
}