mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #1569 from ioito/hotfix/qx-sku-fix
fix sku PropertyInstanceSpecs
This commit is contained in:
+8
-15
@@ -23,12 +23,13 @@ import (
|
||||
func init() {
|
||||
type ServerSkusListOptions struct {
|
||||
options.BaseListOptions
|
||||
Region string `help:"region Id or name"`
|
||||
Zone string `help:"zone Id or name"`
|
||||
City *string `help:"city name,eg. BeiJing"`
|
||||
Cpu *int `help:"Cpu core count" json:"cpu_core_count"`
|
||||
Mem *int `help:"Memory size in MB" json:"memory_size_mb"`
|
||||
Name string `help:"Name of Sku"`
|
||||
Cloudregion string `help:"region Id or name"`
|
||||
Usable bool `help:"Filter usable sku"`
|
||||
Zone string `help:"zone Id or name"`
|
||||
City *string `help:"city name,eg. BeiJing"`
|
||||
Cpu *int `help:"Cpu core count" json:"cpu_core_count"`
|
||||
Mem *int `help:"Memory size in MB" json:"memory_size_mb"`
|
||||
Name string `help:"Name of Sku"`
|
||||
}
|
||||
R(&ServerSkusListOptions{}, "server-sku-list", "List all avaiable Server SKU", func(s *mcclient.ClientSession, args *ServerSkusListOptions) error {
|
||||
params, err := options.ListStructToParams(args)
|
||||
@@ -168,15 +169,7 @@ func init() {
|
||||
return nil
|
||||
})
|
||||
|
||||
type ServerSkuSpecsListOptions struct {
|
||||
Provider string `help:"List objects from the provider" choices:"OneCloud|VMware|Aliyun|Qcloud|Azure|Aws|Huawei|Openstack|Ucloud|ZStack" json:"provider"`
|
||||
PublicCloud *bool `help:"List objects belonging to public cloud" json:"public_cloud"`
|
||||
Zone string `help:"zone Id or name"`
|
||||
PostpaidStatus *string `help:"skus available status for postpaid instance" choices:"available|soldout"`
|
||||
PrepaidStatus *string `help:"skus available status for prepaid instance" choices:"available|soldout"`
|
||||
IngoreCache bool `help:"query without cache"`
|
||||
}
|
||||
R(&ServerSkuSpecsListOptions{}, "server-sku-specs-list", "List all avaiable Server SKU specifications", func(s *mcclient.ClientSession, args *ServerSkuSpecsListOptions) error {
|
||||
R(&ServerSkusListOptions{}, "server-sku-specs-list", "List all avaiable Server SKU specifications", func(s *mcclient.ClientSession, args *ServerSkusListOptions) error {
|
||||
params, err := options.ListStructToParams(args)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+20
-37
@@ -558,47 +558,16 @@ func usableFilter(q *sqlchemy.SQuery, public_cloud bool) *sqlchemy.SQuery {
|
||||
}
|
||||
|
||||
func (manager *SServerSkuManager) GetPropertyInstanceSpecs(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
params := NewInstanceSpecQueryParams(query)
|
||||
if !params.IngoreCache {
|
||||
v := Cache.Get(params.GetCacheKey())
|
||||
if v != nil {
|
||||
if cacheRet, ok := v.(*jsonutils.JSONDict); ok {
|
||||
return cacheRet, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
q := manager.Query()
|
||||
// 仅过滤有ip子网的sku,必选显式指定provider进行过滤
|
||||
q = usableFilter(q, params.PublicCloud)
|
||||
q = excludeSkus(q)
|
||||
|
||||
// 如果是查询私有云需要忽略zone参数
|
||||
if params.PublicCloud && len(params.ZoneId) > 0 {
|
||||
zoneObj, err := ZoneManager.FetchByIdOrName(userCred, params.ZoneId)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, httperrors.NewResourceNotFoundError2(ZoneManager.Keyword(), params.ZoneId)
|
||||
}
|
||||
return nil, httperrors.NewGeneralError(err)
|
||||
}
|
||||
|
||||
q = q.Equals("zone_id", zoneObj.GetId())
|
||||
q, err := manager.ListItemFilter(ctx, manager.Query(), userCred, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
skus := make([]SServerSku, 0)
|
||||
if len(params.PostpaidStatus) > 0 {
|
||||
q.Equals("postpaid_status", params.PostpaidStatus)
|
||||
}
|
||||
|
||||
if len(params.PrepaidStatus) > 0 {
|
||||
q.Equals("prepaid_status", params.PrepaidStatus)
|
||||
}
|
||||
q = q.GroupBy(q.Field("cpu_core_count"), q.Field("memory_size_mb"))
|
||||
q = q.Asc(q.Field("cpu_core_count"), q.Field("memory_size_mb"))
|
||||
err := db.FetchModelObjects(manager, q, &skus)
|
||||
err = db.FetchModelObjects(manager, q, &skus)
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
return nil, httperrors.NewBadRequestError("instance specs list query error")
|
||||
}
|
||||
|
||||
@@ -639,8 +608,6 @@ func (manager *SServerSkuManager) GetPropertyInstanceSpecs(ctx context.Context,
|
||||
|
||||
r_obj := jsonutils.Marshal(&cpu_mems_mb)
|
||||
ret.Add(r_obj, "cpu_mems_mb")
|
||||
// cache 1min
|
||||
Cache.Set(params.GetCacheKey(), ret, time.Now().Add(60*time.Second))
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
@@ -742,9 +709,25 @@ func (manager *SServerSkuManager) ListItemFilter(ctx context.Context, q *sqlchem
|
||||
|
||||
if usable, _ := query.Bool("usable"); usable {
|
||||
q = usableFilter(q, publicCloud)
|
||||
q = q.IsTrue("enabled")
|
||||
}
|
||||
|
||||
data := query.(*jsonutils.JSONDict)
|
||||
//OneCloud忽略zone参数
|
||||
if provider == api.CLOUD_PROVIDER_ONECLOUD && data.Contains("zone") {
|
||||
zoneStr, _ := data.GetString("zone")
|
||||
_zone, err := ZoneManager.FetchByIdOrName(userCred, zoneStr)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, httperrors.NewResourceNotFoundError2("zone", zoneStr)
|
||||
}
|
||||
return nil, httperrors.NewGeneralError(err)
|
||||
}
|
||||
zone := _zone.(*SZone)
|
||||
data.Remove("zone")
|
||||
data.Set("cloudregion", jsonutils.NewString(zone.CloudregionId))
|
||||
}
|
||||
|
||||
q, err = validators.ApplyModelFilters(q, data, []*validators.ModelFilterOptions{
|
||||
{Key: "zone", ModelKeyword: "zone", OwnerId: userCred},
|
||||
{Key: "cloudregion", ModelKeyword: "cloudregion", OwnerId: userCred},
|
||||
|
||||
Reference in New Issue
Block a user