改进:同步python和golang的validateupdatedata实现,允许修改主机名

This commit is contained in:
Qiu Jian
2018-08-11 15:19:50 +08:00
parent 68d4b2caef
commit 53caf371f5
+34 -2
View File
@@ -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 {