fix: support create 512mb server sku

This commit is contained in:
Qu Xuan
2020-11-12 14:02:10 +08:00
parent 4fe47fe405
commit c7a6ec8e9d
2 changed files with 42 additions and 38 deletions
+2 -6
View File
@@ -23,15 +23,11 @@ type ServerSkuCreateInput struct {
// 区域名称或Id,建议使用Id
// default: default
Cloudregion string `json:"cloudregion"`
// swagger:ignore
CloudregionId string
CloudregionId string `json:"cloudregion_id"`
// 可用区名称或Id, 建议使用Id
// required: false
Zone string `json:"zone"`
// swagger:ignore
ZoneId string
ZoneId string `json:"zone_id"`
// 是否启用
// default: true
+40 -32
View File
@@ -179,16 +179,25 @@ func inWhiteList(provider string) bool {
return provider == api.CLOUD_PROVIDER_ONECLOUD || utils.IsInStringArray(provider, api.PRIVATE_CLOUD_PROVIDERS)
}
func genInstanceType(family string, cpu, mem_mb int64) (string, error) {
func genInstanceType(family string, cpu, memMb int64) (string, error) {
if cpu <= 0 {
return "", fmt.Errorf("cpu_core_count should great than zero")
}
if mem_mb <= 0 || mem_mb%1024 != 0 {
return "", fmt.Errorf("memory_size_mb should great than zero. and should be integral multiple of 1024")
if memMb <= 0 {
return "", fmt.Errorf("memory_size_mb should great than zero")
}
return fmt.Sprintf("ecs.%s.c%dm%d", family, cpu, mem_mb/1024), nil
if memMb%1024 != 0 && memMb != 512 {
return "", fmt.Errorf("memory_size_mb should be 512 or integral multiple of 1024")
}
switch memMb {
case 512:
return fmt.Sprintf("ecs.%s.c%dm1.nano", family, cpu), nil
default:
return fmt.Sprintf("ecs.%s.c%dm%d", family, cpu, memMb/1024), nil
}
}
func skuRelatedGuestCount(self *SServerSku) (int, error) {
@@ -308,36 +317,35 @@ func (manager *SServerSkuManager) AllowCreateItem(ctx context.Context, userCred
return db.IsAdminAllowCreate(userCred, manager)
}
func (self *SServerSkuManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input api.ServerSkuCreateInput) (*jsonutils.JSONDict, error) {
if len(input.Cloudregion) == 0 {
input.Cloudregion = api.DEFAULT_REGION_ID
}
region, err := CloudregionManager.FetchByIdOrName(nil, input.Cloudregion)
if err != nil {
if err == sql.ErrNoRows {
return nil, httperrors.NewResourceNotFoundError("failed to found cloudregion %s", input.Cloudregion)
}
return nil, httperrors.NewGeneralError(err)
}
input.CloudregionId = region.GetId()
if len(input.Zone) > 0 {
zone, err := ZoneManager.FetchByIdOrName(nil, input.Zone)
func (self *SServerSkuManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input api.ServerSkuCreateInput) (api.ServerSkuCreateInput, error) {
var err error
if len(input.CloudregionId) > 0 {
_, err = validators.ValidateModel(userCred, CloudregionManager, &input.CloudregionId)
if err != nil {
if err == sql.ErrNoRows {
return nil, httperrors.NewResourceNotFoundError("failed to found zone %s", input.Zone)
}
return nil, httperrors.NewGeneralError(err)
return input, err
}
}
if len(input.ZoneId) > 0 {
_zone, err := validators.ValidateModel(userCred, ZoneManager, &input.ZoneId)
if err != nil {
return input, err
}
zone := _zone.(*SZone)
if len(input.CloudregionId) == 0 {
input.CloudregionId = zone.CloudregionId
}
if input.CloudregionId != zone.CloudregionId {
return input, httperrors.NewConflictError("zone %s not in cloudregion %s", zone.Name, input.CloudregionId)
}
input.ZoneId = zone.GetId()
}
if input.CpuCoreCount < 1 || input.CpuCoreCount > 256 {
return nil, httperrors.NewOutOfRangeError("cpu_core_count should be range of 1~256")
return input, httperrors.NewOutOfRangeError("cpu_core_count should be range of 1~256")
}
if input.MemorySizeMB < 512 || input.MemorySizeMB > 1024*512 {
return nil, httperrors.NewOutOfRangeError("memory_size_mb, shoud be range of 512~%d", 1024*512)
return input, httperrors.NewOutOfRangeError("memory_size_mb, shoud be range of 512~%d", 1024*512)
}
if len(input.InstanceTypeCategory) == 0 {
@@ -345,7 +353,7 @@ func (self *SServerSkuManager) ValidateCreateData(ctx context.Context, userCred
}
if !utils.IsInStringArray(input.InstanceTypeCategory, api.SKU_FAMILIES) {
return nil, httperrors.NewInputParameterError("instance_type_category shoud be one of %s", api.SKU_FAMILIES)
return input, httperrors.NewInputParameterError("instance_type_category shoud be one of %s", api.SKU_FAMILIES)
}
if input.Enabled == nil {
@@ -363,23 +371,23 @@ func (self *SServerSkuManager) ValidateCreateData(ctx context.Context, userCred
// 格式 ecs.g1.c1m1
input.Name, err = genInstanceType(input.InstanceTypeFamily, input.CpuCoreCount, input.MemorySizeMB)
if err != nil {
return nil, httperrors.NewInputParameterError("%v", err)
return input, httperrors.NewInputParameterError("%v", err)
}
q := self.Query().Equals("name", input.Name)
count, err := q.CountWithError()
if err != nil {
return nil, httperrors.NewInternalServerError("checkout server sku name duplicate error: %v", err)
return input, httperrors.NewInternalServerError("checkout server sku name duplicate error: %v", err)
}
if count > 0 {
return nil, httperrors.NewDuplicateResourceError("Duplicate sku %s", input.Name)
return input, httperrors.NewDuplicateResourceError("Duplicate sku %s", input.Name)
}
}
input.StatusStandaloneResourceCreateInput, err = self.SStatusStandaloneResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.StatusStandaloneResourceCreateInput)
if err != nil {
return nil, err
return input, err
}
return input.JSON(input), nil
return input, nil
}
func (self *SServerSku) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {