Merge pull request #12259 from ioito/hotfix/qx-optimized-list-query

fix(region): optimized validate delete condition query
This commit is contained in:
Zexi Li
2021-09-26 10:03:18 +08:00
committed by GitHub
112 changed files with 291 additions and 259 deletions
+32
View File
@@ -24,6 +24,7 @@ import (
"yunion.io/x/pkg/gotypes"
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/stringutils2"
@@ -284,12 +285,32 @@ func FetchCustomizeColumns(
if ret[0].Len() != len(objs) {
return nil, httperrors.NewInternalServerError("Invalid FetchCustomizeColumns return value, inconsistent obj count: input %d != output %d", len(objs), ret[0].Len())
}
showReason := false
if query.Contains("show_fail_reason") {
showReason = true
}
retVal := make([]*jsonutils.JSONDict, ret[0].Len())
for i := 0; i < ret[0].Len(); i += 1 {
jsonDict := ValueToJSONDict(ret[0].Index(i))
// NOTE: don't use obj update jsonDict as retval
jsonDict.Update(jsonutils.Marshal(objs[i]).(*jsonutils.JSONDict))
out := apis.ModelBaseDetails{
CanDelete: true,
}
err = ValidateDeleteCondition(objs[i].(IModel), ctx, jsonDict)
if err != nil {
out.CanDelete = false
if showReason {
out.DeleteFailReason = httperrors.NewErrorFromGeneralError(ctx, err)
}
}
jsonDict.Update(jsonutils.Marshal(out))
retVal[i] = jsonDict
}
return retVal, nil
}
@@ -319,3 +340,14 @@ func CustomizeDelete(model IModel, ctx context.Context, userCred mcclient.TokenC
}
return ValueToError(ret[0])
}
func ValidateDeleteCondition(model IModel, ctx context.Context, data jsonutils.JSONObject) error {
ret, err := call(model, "ValidateDeleteCondition", ctx, data)
if err != nil {
return httperrors.NewGeneralError(err)
}
if len(ret) != 1 {
return httperrors.NewInternalServerError("Invald ValidateDeleteCondition return value")
}
return ValueToError(ret[0])
}
+3 -11
View File
@@ -854,17 +854,9 @@ func (dispatcher *DBModelDispatcher) List(ctx context.Context, query jsonutils.J
func getModelExtraDetails(item IModel, ctx context.Context, showReason bool) apis.ModelBaseDetails {
out := apis.ModelBaseDetails{
CanDelete: true,
CanUpdate: true,
}
err := item.ValidateDeleteCondition(ctx)
if err != nil {
out.CanDelete = false
if showReason {
out.DeleteFailReason = httperrors.NewErrorFromGeneralError(ctx, err)
}
}
err = item.ValidateUpdateCondition(ctx)
err := item.ValidateUpdateCondition(ctx)
if err != nil {
out.CanUpdate = false
if showReason {
@@ -1834,9 +1826,9 @@ func DeleteModel(ctx context.Context, userCred mcclient.TokenCredential, item IM
func deleteItem(manager IModelManager, model IModel, ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
// log.Debugf("deleteItem %s", jsonutils.Marshal(model))
err := model.ValidateDeleteCondition(ctx)
err := ValidateDeleteCondition(model, ctx, nil)
if err != nil {
log.Errorf("validate delete condition error: %s", err)
return nil, err
}
+1 -1
View File
@@ -286,7 +286,7 @@ func (dispatcher *DBJointModelDispatcher) Detach(ctx context.Context, id1 string
}
func DetachJoint(ctx context.Context, userCred mcclient.TokenCredential, item IJointModel) error {
err := item.ValidateDeleteCondition(ctx)
err := ValidateDeleteCondition(item, ctx, nil)
if err != nil {
return err
}
+1 -1
View File
@@ -181,7 +181,7 @@ type IModel interface {
// delete hooks
AllowDeleteItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool
ValidateDeleteCondition(ctx context.Context) error
// ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error
// CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error
PreDelete(ctx context.Context, userCred mcclient.TokenCredential)
MarkDelete() error
+1 -1
View File
@@ -580,7 +580,7 @@ func (model *SModelBase) ValidateUpdateCondition(ctx context.Context) error {
return nil
}
func (model *SModelBase) ValidateDeleteCondition(ctx context.Context) error {
func (model *SModelBase) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
return nil
}
+1 -1
View File
@@ -363,7 +363,7 @@ func (self *SOpsLog) AllowDeleteItem(ctx context.Context, userCred mcclient.Toke
return false
}
func (self *SOpsLog) ValidateDeleteCondition(ctx context.Context) error {
func (self *SOpsLog) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
return httperrors.NewForbiddenError("not allow to delete log")
}
+2 -2
View File
@@ -106,7 +106,7 @@ func (ps *SProxySetting) HttpTransportProxyFunc() httputils.TransportProxyFunc {
}
}
func (ps *SProxySetting) ValidateDeleteCondition(ctx context.Context) error {
func (ps *SProxySetting) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
if ps.Id == proxyapi.ProxySettingId_DIRECT {
return httperrors.NewConflictError("DIRECT setting cannot be deleted")
}
@@ -122,7 +122,7 @@ func (ps *SProxySetting) ValidateDeleteCondition(ctx context.Context) error {
ps.Id, n, man.KeywordPlural())
}
}
return ps.SInfrasResourceBase.ValidateDeleteCondition(ctx)
return ps.SInfrasResourceBase.ValidateDeleteCondition(ctx, nil)
}
func (ps *SProxySetting) AllowPerformTest(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
+1 -1
View File
@@ -173,7 +173,7 @@ func (self *STask) AllowDeleteItem(ctx context.Context, userCred mcclient.TokenC
return false
}
func (self *STask) ValidateDeleteCondition(ctx context.Context) error {
func (self *STask) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
return httperrors.NewForbiddenError("forbidden")
}