yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/instancetype.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package qcloud
    16  
    17  import (
    18  	"yunion.io/x/log"
    19  	"yunion.io/x/pkg/errors"
    20  	"yunion.io/x/pkg/utils"
    21  )
    22  
    23  // "time"
    24  
    25  // {"CpuCoreCount":1,"EniQuantity":1,"GPUAmount":0,"GPUSpec":"","InstanceTypeFamily":"ecs.t1","InstanceTypeId":"ecs.t1.xsmall","LocalStorageCategory":"","MemorySize":0.500000}
    26  // InstanceBandwidthRx":26214400,"InstanceBandwidthTx":26214400,"InstancePpsRx":4500000,"InstancePpsTx":4500000
    27  
    28  type SInstanceType struct {
    29  	Zone              string //	可用区。
    30  	InstanceType      string //	实例机型。
    31  	InstanceFamily    string //	实例机型系列。
    32  	GPU               int    //	GPU核数,单位:核。
    33  	CPU               int    //	CPU核数,单位:核。
    34  	Memory            int    //	内存容量,单位:GB。
    35  	CbsSupport        string //	是否支持云硬盘。取值范围:TRUE:表示支持云硬盘;FALSE:表示不支持云硬盘。
    36  	InstanceTypeState string //	机型状态。取值范围:AVAILABLE:表示机型可用;UNAVAILABLE:表示机型不可用。
    37  }
    38  
    39  func (self *SRegion) GetInstanceTypes() ([]SInstanceType, error) {
    40  	params := make(map[string]string)
    41  	params["Region"] = self.Region
    42  
    43  	body, err := self.cvmRequest("DescribeInstanceTypeConfigs", params, true)
    44  	if err != nil {
    45  		log.Errorf("DescribeInstanceTypeConfigs fail %s", err)
    46  		return nil, err
    47  	}
    48  
    49  	instanceTypes := make([]SInstanceType, 0)
    50  	err = body.Unmarshal(&instanceTypes, "InstanceTypeConfigSet")
    51  	if err != nil {
    52  		log.Errorf("Unmarshal instance type details fail %s", err)
    53  		return nil, err
    54  	}
    55  	return instanceTypes, nil
    56  }
    57  
    58  func (self *SInstanceType) memoryMB() int {
    59  	return int(self.Memory * 1024)
    60  }
    61  
    62  type SLocalDiskType struct {
    63  	Type          string
    64  	PartitionType string
    65  	MinSize       int
    66  	MaxSize       int
    67  }
    68  
    69  type SStorageBlockAttr struct {
    70  	Type    string
    71  	MinSize int
    72  	MaxSize int
    73  }
    74  
    75  type SExternal struct {
    76  	ReleaseAddress    string
    77  	UnsupportNetworks []string
    78  	StorageBlockAttr  SStorageBlockAttr
    79  }
    80  
    81  type SZoneInstanceType struct {
    82  	Zone               string
    83  	InstanceType       string
    84  	InstanceChargeType string
    85  	NetworkCard        int
    86  	Externals          SExternal
    87  	Cpu                int
    88  	Memory             int
    89  	InstanceFamily     string
    90  	TypeName           string
    91  	LocalDiskTypeList  []SLocalDiskType
    92  	Status             string
    93  }
    94  
    95  func (self *SRegion) GetZoneInstanceTypes(zoneId string) ([]SZoneInstanceType, error) {
    96  	params := map[string]string{}
    97  	params["Region"] = self.Region
    98  	params["Filters.0.Name"] = "zone"
    99  	params["Filters.0.Values.0"] = zoneId
   100  	body, err := self.cvmRequest("DescribeZoneInstanceConfigInfos", params, true)
   101  	if err != nil {
   102  		return nil, errors.Wrap(err, "DescribeZoneInstanceConfigInfos")
   103  	}
   104  	instanceTypes := []SZoneInstanceType{}
   105  	err = body.Unmarshal(&instanceTypes, "InstanceTypeQuotaSet")
   106  	if err != nil {
   107  		return nil, errors.Wrap(err, "body.Unmarshal")
   108  	}
   109  	return instanceTypes, nil
   110  }
   111  
   112  func (self *SRegion) GetZoneLocalStorages(zoneId string) ([]string, error) {
   113  	instanceTypes, err := self.GetZoneInstanceTypes(zoneId)
   114  	if err != nil {
   115  		return nil, errors.Wrap(err, "GetZoneInstanceTypes")
   116  	}
   117  	storages := []string{}
   118  	for _, instanceType := range instanceTypes {
   119  		storage := instanceType.Externals.StorageBlockAttr.Type
   120  		if len(storage) > 0 && !utils.IsInStringArray(storage, storages) {
   121  			storages = append(storages, storage)
   122  		}
   123  		for _, localstorage := range instanceType.LocalDiskTypeList {
   124  			if len(localstorage.Type) > 0 && !utils.IsInStringArray(localstorage.Type, storages) {
   125  				storages = append(storages, localstorage.Type)
   126  			}
   127  		}
   128  	}
   129  	return storages, nil
   130  }