From 940f6c84dee569505133610fc12ce4437b5428b1 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Fri, 3 Jan 2020 15:18:19 +0800 Subject: [PATCH] automatically save pendingUsage in context --- pkg/cloudcommon/db/db_dispatcher.go | 12 +++--------- pkg/cloudcommon/db/quotas/context.go | 2 +- pkg/cloudcommon/db/quotas/register.go | 8 +++++++- pkg/compute/models/guests.go | 1 - 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pkg/cloudcommon/db/db_dispatcher.go b/pkg/cloudcommon/db/db_dispatcher.go index c6fe724091..c7b5660912 100644 --- a/pkg/cloudcommon/db/db_dispatcher.go +++ b/pkg/cloudcommon/db/db_dispatcher.go @@ -39,7 +39,6 @@ import ( "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/mcclient/modulebase" - "yunion.io/x/onecloud/pkg/util/httputils" "yunion.io/x/onecloud/pkg/util/rbacutils" "yunion.io/x/onecloud/pkg/util/stringutils2" ) @@ -1242,14 +1241,9 @@ func (dispatcher *DBModelDispatcher) BatchCreate(ctx context.Context, query json for i, res := range createResults { result := modulebase.SubmitResult{} if res.err != nil { - jsonErr, ok := res.err.(*httputils.JSONClientError) - if ok { - result.Status = jsonErr.Code - result.Data = jsonutils.Marshal(jsonErr) - } else { - result.Status = 500 - result.Data = jsonutils.NewString(res.err.Error()) - } + jsonErr := httperrors.NewGeneralError(res.err) + result.Status = jsonErr.Code + result.Data = jsonutils.Marshal(jsonErr) } else { lockman.LockObject(ctx, res.model) defer lockman.ReleaseObject(ctx, res.model) diff --git a/pkg/cloudcommon/db/quotas/context.go b/pkg/cloudcommon/db/quotas/context.go index 53e67479b8..2da99b30fc 100644 --- a/pkg/cloudcommon/db/quotas/context.go +++ b/pkg/cloudcommon/db/quotas/context.go @@ -57,7 +57,7 @@ func clearPendingUsagesInContext(ctx context.Context) { } } -func SavePendingUsagesInContext(ctx context.Context, quotas ...IQuota) { +func savePendingUsagesInContext(ctx context.Context, quotas ...IQuota) { val := ctx.Value(APP_CONTEXT_KEY_PENDINGUSAGES) if val != nil { quotaList := val.(*list.List) diff --git a/pkg/cloudcommon/db/quotas/register.go b/pkg/cloudcommon/db/quotas/register.go index 5ac0944c3c..80ef522e81 100644 --- a/pkg/cloudcommon/db/quotas/register.go +++ b/pkg/cloudcommon/db/quotas/register.go @@ -20,6 +20,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/mcclient" @@ -61,7 +62,12 @@ func CancelPendingUsage(ctx context.Context, userCred mcclient.TokenCredential, func CheckSetPendingQuota(ctx context.Context, userCred mcclient.TokenCredential, quota IQuota) error { manager := getQuotaManager(quota) - return manager.checkSetPendingQuota(ctx, userCred, quota) + err := manager.checkSetPendingQuota(ctx, userCred, quota) + if err != nil { + return errors.Wrap(err, "manager.checkSetPendingQuota") + } + savePendingUsagesInContext(ctx, quota) + return nil } func CancelUsages(ctx context.Context, userCred mcclient.TokenCredential, usages []db.IUsage) { diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index ad6a844ef8..b707b383fd 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -1393,7 +1393,6 @@ func (manager *SGuestManager) checkCreateQuota( if err != nil { return errors.Wrap(err, "quotas.CheckSetPendingQuota") } - quotas.SavePendingUsagesInContext(ctx, &req, ®ionReq) return nil }