From 53caf371f5b15441c47501e8b5ae308a92e07fc3 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Sat, 11 Aug 2018 15:19:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=EF=BC=9A=E5=90=8C=E6=AD=A5py?= =?UTF-8?q?thon=E5=92=8Cgolang=E7=9A=84validateupdatedata=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=EF=BC=8C=E5=85=81=E8=AE=B8=E4=BF=AE=E6=94=B9=E4=B8=BB?= =?UTF-8?q?=E6=9C=BA=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/models/guests.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 94364c56aa..a0f6a14fe1 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -453,9 +453,25 @@ func validateMemCpuData(data jsonutils.JSONObject) (int, int, error) { } func (self *SGuest) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { - if data.Contains("name") { - return nil, httperrors.NewInputParameterError("cannot update server name") + vmemSize, vcpuCount, err := validateMemCpuData(data) + if err != nil { + return nil, err } + if vmemSize > 0 { + data.Add(jsonutils.NewInt(int64(vmemSize)), "vmem_size") + } + if vcpuCount > 0 { + data.Add(jsonutils.NewInt(int64(vcpuCount)), "vcpu_count") + } + + err = self.checkUpdateQuota(ctx, userCred, vcpuCount, vmemSize) + if err != nil { + return nil, err + } + + // if data.Contains("name") { + // return nil, httperrors.NewInputParameterError("cannot update server name") + // } /* if self.GetHypervisor() == HYPERVISOR_BAREMETAL { return nil, httperrors.NewInputParameterError("Cannot modify memory for baremetal") } @@ -737,6 +753,22 @@ func (manager *SGuestManager) checkCreateQuota(ctx context.Context, userCred mcc } } +func (self *SGuest) checkUpdateQuota(ctx context.Context, userCred mcclient.TokenCredential, vcpuCount int, vmemSize int) error { + req := SQuota{} + + if vcpuCount > 0 && vcpuCount > int(self.VcpuCount) { + req.Cpu = vcpuCount - int(self.VcpuCount) + } + + if vmemSize > 0 && vmemSize > self.VmemSize { + req.Memory = vmemSize - self.VmemSize + } + + _, err := QuotaManager.CheckQuota(ctx, userCred, self.ProjectId, &req) + + return err +} + func getGuestResourceRequirements(ctx context.Context, userCred mcclient.TokenCredential, data jsonutils.JSONObject, count int) SQuota { vcpuCount, _ := data.Int("vcpu_count") if vcpuCount == 0 {