From 5e4cc33146ac2945fd9e0497b02a66d62dd26f35 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Wed, 21 Oct 2020 21:18:01 +0800 Subject: [PATCH] fix: add lb quota check --- pkg/compute/models/loadbalancers.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index 6e2db9b9f1..ac28945d14 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -310,8 +310,9 @@ func (man *SLoadbalancerManager) ValidateCreateData( input.CloudregionId = region.GetId() + var cloudprovider *SCloudprovider if len(input.CloudproviderId) > 0 { - _, input.CloudproviderResourceInput, err = ValidateCloudproviderResourceInput(userCred, input.CloudproviderResourceInput) + cloudprovider, input.CloudproviderResourceInput, err = ValidateCloudproviderResourceInput(userCred, input.CloudproviderResourceInput) if err != nil { return nil, errors.Wrap(err, "ValidateCloudproviderResourceInput") } @@ -322,6 +323,13 @@ func (man *SLoadbalancerManager) ValidateCreateData( return nil, err } + quotaKeys := fetchRegionalQuotaKeys(rbacutils.ScopeProject, ownerId, region, cloudprovider) + pendingUsage := SRegionQuota{Loadbalancer: 1} + pendingUsage.SetKeys(quotaKeys) + if err := quotas.CheckSetPendingQuota(ctx, userCred, &pendingUsage); err != nil { + return nil, httperrors.NewOutOfQuotaError("%s", err) + } + return region.GetDriver().ValidateCreateLoadbalancerData(ctx, userCred, ownerId, input.JSON(input)) } @@ -371,6 +379,13 @@ func (lb *SLoadbalancer) PostCreate(ctx context.Context, userCred mcclient.Token // NOTE this means lb.UpdateVersion will be 0, then 1 after creation // NOTE need ways to notify error + pendingUsage := SRegionQuota{Loadbalancer: 1} + pendingUsage.SetKeys(lb.GetQuotaKeys()) + err := quotas.CancelPendingUsage(ctx, userCred, &pendingUsage, &pendingUsage, true) + if err != nil { + log.Errorf("CancelPendingUsage error %s", err) + } + lb.SetStatus(userCred, api.LB_CREATING, "") if err := lb.StartLoadBalancerCreateTask(ctx, userCred, data.(*jsonutils.JSONDict), ""); err != nil { log.Errorf("Failed to create loadbalancer error: %v", err)