yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/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 huawei
    16  
    17  import (
    18  	"strconv"
    19  )
    20  
    21  // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212656.html
    22  type SInstanceType struct {
    23  	ID           string       `json:"id"`
    24  	Name         string       `json:"name"`
    25  	Vcpus        string       `json:"vcpus"`
    26  	RamMB        int          `json:"ram"`            // 内存大小
    27  	OSExtraSpecs OSExtraSpecs `json:"os_extra_specs"` // 扩展规格
    28  }
    29  
    30  type OSExtraSpecs struct {
    31  	EcsPerformancetype string `json:"ecs:performancetype"`
    32  }
    33  
    34  // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212656.html
    35  func (self *SRegion) fetchInstanceTypes(zoneId string) ([]SInstanceType, error) {
    36  	querys := map[string]string{}
    37  	if len(zoneId) > 0 {
    38  		querys["availability_zone"] = zoneId
    39  	}
    40  
    41  	instanceTypes := make([]SInstanceType, 0)
    42  	err := doListAll(self.ecsClient.Flavors.List, querys, &instanceTypes)
    43  	return instanceTypes, err
    44  }
    45  
    46  func (self *SRegion) GetMatchInstanceTypes(cpu int, memMB int, zoneId string) ([]SInstanceType, error) {
    47  	instanceTypes, err := self.fetchInstanceTypes(zoneId)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	ret := make([]SInstanceType, 0)
    53  	for _, t := range instanceTypes {
    54  		// cpu & mem & disk都匹配才行
    55  		if t.Vcpus == strconv.Itoa(cpu) && t.RamMB == memMB {
    56  			ret = append(ret, t)
    57  		}
    58  	}
    59  
    60  	return ret, nil
    61  }