yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"strconv"
    19  	"strings"
    20  
    21  	"yunion.io/x/cloudmux/pkg/apis"
    22  	"yunion.io/x/cloudmux/pkg/multicloud"
    23  )
    24  
    25  // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212656.html
    26  type SInstanceType struct {
    27  	multicloud.SResourceBase
    28  	ID                     string       `json:"id"`
    29  	Name                   string       `json:"name"`
    30  	Vcpus                  string       `json:"vcpus"`
    31  	RamMB                  int          `json:"ram"` // 内存大小
    32  	Disk                   string       `json:"disk"`
    33  	Swap                   string       `json:"swap"`
    34  	OSFLVEXTDATAEphemeral  int64        `json:"OS-FLV-EXT-DATA:ephemeral"`
    35  	RxtxFactor             int64        `json:"rxtx_factor"`
    36  	OSFLVDISABLEDDisabled  bool         `json:"OS-FLV-DISABLED:disabled"`
    37  	OSFlavorAccessIsPublic bool         `json:"os-flavor-access:is_public"`
    38  	OSExtraSpecs           OSExtraSpecs `json:"os_extra_specs"` // 扩展规格
    39  }
    40  
    41  type OSExtraSpecs struct {
    42  	EcsPerformancetype      string `json:"ecs:performancetype"`
    43  	EcsGeneration           string `json:"ecs:generation"`
    44  	EcsInstanceArchitecture string `json:"ecs:instance_architecture"`
    45  }
    46  
    47  var FLAVOR_FAMILY_CATEGORY_MAP = map[string]string{
    48  	"s1":    "通用型I代",
    49  	"s2":    "通用型II代",
    50  	"s3":    "通用型S3",
    51  	"sn3":   "通用型",
    52  	"s6":    "通用型S6",
    53  	"p1":    "GPU P1型",
    54  	"pi1":   "GPU Pi1型",
    55  	"p2v":   "GPU P2v型",
    56  	"t6":    "通用型T6",
    57  	"m1":    "内存优化型I代",
    58  	"m2":    "内存优化型II代",
    59  	"m3":    "内存优化型",
    60  	"m3ne":  "内存优化M3ne型",
    61  	"h1":    "高性能计算型I代",
    62  	"h2":    "高性能计算型II代",
    63  	"h3":    "高性能计算型",
    64  	"hc2":   "高性能计算HC2型",
    65  	"hi3":   "超高性能计算型",
    66  	"d1":    "密集存储型I代",
    67  	"d2":    "密集存储型II代",
    68  	"d3":    "磁盘增强型",
    69  	"g1":    "GPU加速型I代",
    70  	"g2":    "GPU加速型II代",
    71  	"g3":    "GPU加速型III代",
    72  	"f1":    "FPGA高性能型",
    73  	"f2":    "FPGA通用型",
    74  	"fp1":   "FPGA FP1型",
    75  	"fp1c":  "FPGA FP1C型",
    76  	"ai1":   "人工智能Ai1型",
    77  	"c1":    "通用计算增强C1型",
    78  	"c2":    "通用计算增强C2型",
    79  	"c3":    "通用计算增强C3型",
    80  	"c3ne":  "通用计算增强C3ne型",
    81  	"c6":    "通用计算增强C6型",
    82  	"e1":    "大内存E1型",
    83  	"e2":    "大内存E2型",
    84  	"et2":   "大内存ET2型",
    85  	"e3":    "大内存E3型",
    86  	"i3":    "超高I/O型",
    87  	"kc1":   "鲲鹏通用计算增强型",
    88  	"km1":   "鲲鹏内存优化型",
    89  	"ki1":   "鲲鹏超高I/O型",
    90  	"kai1s": "鲲鹏AI推理加速型",
    91  }
    92  
    93  func getFlavorCategory(family string) string {
    94  	ret, ok := FLAVOR_FAMILY_CATEGORY_MAP[family]
    95  	if ok {
    96  		return ret
    97  	}
    98  
    99  	return family
   100  }
   101  
   102  func getFlavorLocalCategory(family string) string {
   103  	switch family {
   104  	case "s1", "s2", "s3", "sn3", "s6", "t6":
   105  		return "general-purpose"
   106  	case "c1", "c2", "c3", "c3ne", "c6", "h1", "h2", "h3", "hc2", "hi3", "kc1":
   107  		return "compute-optimized"
   108  	case "m1", "m2", "m3", "m3ne", "e1", "e2", "et2", "e3", "km1":
   109  		return "memory-optimized"
   110  	case "d1", "d2", "d3":
   111  		return "storage-optimized"
   112  	case "p1", "pi1", "p2v", "g1", "g2", "g3":
   113  		return "gpu-compute"
   114  	default:
   115  		return "others"
   116  	}
   117  }
   118  
   119  func (self *SInstanceType) GetId() string {
   120  	return self.ID
   121  }
   122  
   123  func (self *SInstanceType) GetName() string {
   124  	return self.ID
   125  }
   126  
   127  func (self *SInstanceType) GetGlobalId() string {
   128  	return self.ID
   129  }
   130  
   131  func (self *SInstanceType) GetStatus() string {
   132  	return ""
   133  }
   134  
   135  func (self *SInstanceType) Refresh() error {
   136  	return nil
   137  }
   138  
   139  func (self *SInstanceType) IsEmulated() bool {
   140  	return false
   141  }
   142  
   143  func (self *SInstanceType) GetSysTags() map[string]string {
   144  	return nil
   145  }
   146  
   147  func (self *SInstanceType) GetTags() (map[string]string, error) {
   148  	return nil, nil
   149  }
   150  
   151  func (self *SInstanceType) SetTags(tags map[string]string, replace bool) error {
   152  	return nil
   153  }
   154  
   155  func (self *SInstanceType) GetInstanceTypeFamily() string {
   156  	if len(self.OSExtraSpecs.EcsGeneration) > 0 {
   157  		return self.OSExtraSpecs.EcsGeneration
   158  	} else {
   159  		return strings.Split(self.ID, ".")[0]
   160  	}
   161  }
   162  
   163  func (self *SInstanceType) GetInstanceTypeCategory() string {
   164  	return getFlavorCategory(self.GetInstanceTypeFamily())
   165  }
   166  
   167  func (self *SInstanceType) GetPrepaidStatus() string {
   168  	return "available"
   169  }
   170  
   171  func (self *SInstanceType) GetPostpaidStatus() string {
   172  	return "available"
   173  }
   174  
   175  // https://support.huaweicloud.com/productdesc-ecs/ecs_01_0066.html
   176  // https://support.huaweicloud.com/ecs_faq/ecs_faq_0105.html
   177  func (self *SInstanceType) GetCpuArch() string {
   178  	if len(self.OSExtraSpecs.EcsInstanceArchitecture) > 0 {
   179  		if strings.ToLower(self.OSExtraSpecs.EcsInstanceArchitecture) == "arm64" {
   180  			return apis.OS_ARCH_AARCH64
   181  		}
   182  
   183  		if strings.HasPrefix(self.OSExtraSpecs.EcsInstanceArchitecture, "arm") {
   184  			return apis.OS_ARCH_AARCH64
   185  		}
   186  	}
   187  
   188  	if strings.HasPrefix(self.ID, "k") {
   189  		return apis.OS_ARCH_AARCH64
   190  	}
   191  
   192  	return apis.OS_ARCH_X86
   193  }
   194  
   195  func (self *SInstanceType) GetCpuCoreCount() int {
   196  	count, err := strconv.Atoi(self.Vcpus)
   197  	if err == nil {
   198  		return count
   199  	}
   200  	return 0
   201  }
   202  
   203  func (self *SInstanceType) GetMemorySizeMB() int {
   204  	return self.RamMB
   205  }
   206  
   207  func (self *SInstanceType) GetOsName() string {
   208  	return ""
   209  }
   210  
   211  func (self *SInstanceType) GetSysDiskResizable() bool {
   212  	return false
   213  }
   214  
   215  func (self *SInstanceType) GetSysDiskType() string {
   216  	return ""
   217  }
   218  
   219  func (self *SInstanceType) GetSysDiskMinSizeGB() int {
   220  	return 0
   221  }
   222  
   223  func (self *SInstanceType) GetSysDiskMaxSizeGB() int {
   224  	return 0
   225  }
   226  
   227  func (self *SInstanceType) GetAttachedDiskType() string {
   228  	return ""
   229  }
   230  
   231  func (self *SInstanceType) GetAttachedDiskSizeGB() int {
   232  	return 0
   233  }
   234  
   235  func (self *SInstanceType) GetAttachedDiskCount() int {
   236  	return 0
   237  }
   238  
   239  func (self *SInstanceType) GetDataDiskTypes() string {
   240  	return ""
   241  }
   242  
   243  func (self *SInstanceType) GetDataDiskMaxCount() int {
   244  	return 0
   245  }
   246  
   247  func (self *SInstanceType) GetNicType() string {
   248  	return ""
   249  }
   250  
   251  func (self *SInstanceType) GetNicMaxCount() int {
   252  	return 0
   253  }
   254  
   255  func (self *SInstanceType) GetGpuAttachable() bool {
   256  	return self.OSExtraSpecs.EcsPerformancetype == "gpu"
   257  }
   258  
   259  func (self *SInstanceType) GetGpuSpec() string {
   260  	if self.OSExtraSpecs.EcsPerformancetype == "gpu" {
   261  		return self.OSExtraSpecs.EcsGeneration
   262  	}
   263  
   264  	return ""
   265  }
   266  
   267  func (self *SInstanceType) GetGpuCount() int {
   268  	if self.OSExtraSpecs.EcsPerformancetype == "gpu" {
   269  		return 1
   270  	}
   271  
   272  	return 0
   273  }
   274  
   275  func (self *SInstanceType) GetGpuMaxCount() int {
   276  	if self.OSExtraSpecs.EcsPerformancetype == "gpu" {
   277  		return 1
   278  	}
   279  
   280  	return 0
   281  }
   282  
   283  func (self *SInstanceType) Delete() error {
   284  	return nil
   285  }
   286  
   287  // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212656.html
   288  func (self *SRegion) fetchInstanceTypes(zoneId string) ([]SInstanceType, error) {
   289  	querys := map[string]string{}
   290  	if len(zoneId) > 0 {
   291  		querys["availability_zone"] = zoneId
   292  	}
   293  
   294  	instanceTypes := make([]SInstanceType, 0)
   295  	err := doListAll(self.ecsClient.Flavors.List, querys, &instanceTypes)
   296  	return instanceTypes, err
   297  }
   298  
   299  func (self *SRegion) GetMatchInstanceTypes(cpu int, memMB int, zoneId string) ([]SInstanceType, error) {
   300  	instanceTypes, err := self.fetchInstanceTypes(zoneId)
   301  	if err != nil {
   302  		return nil, err
   303  	}
   304  
   305  	ret := make([]SInstanceType, 0)
   306  	for _, t := range instanceTypes {
   307  		// cpu & mem & disk都匹配才行
   308  		if t.Vcpus == strconv.Itoa(cpu) && t.RamMB == memMB {
   309  			ret = append(ret, t)
   310  		}
   311  	}
   312  
   313  	return ret, nil
   314  }