diff --git a/pkg/apis/output.go b/pkg/apis/output.go index f628293c3a..7c5c721df6 100644 --- a/pkg/apis/output.go +++ b/pkg/apis/output.go @@ -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 { diff --git a/pkg/cloudcommon/db/caller.go b/pkg/cloudcommon/db/caller.go index 822d5cb232..27e316eaea 100644 --- a/pkg/cloudcommon/db/caller.go +++ b/pkg/cloudcommon/db/caller.go @@ -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]) +} diff --git a/pkg/cloudcommon/db/db_dispatcher.go b/pkg/cloudcommon/db/db_dispatcher.go index 95a6f026b4..aba084b64c 100644 --- a/pkg/cloudcommon/db/db_dispatcher.go +++ b/pkg/cloudcommon/db/db_dispatcher.go @@ -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 { diff --git a/pkg/cloudcommon/db/modelbase.go b/pkg/cloudcommon/db/modelbase.go index 9c261e9217..aba78b77d8 100644 --- a/pkg/cloudcommon/db/modelbase.go +++ b/pkg/cloudcommon/db/modelbase.go @@ -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 }