yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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 hcs 16 17 import ( 18 "fmt" 19 "net/url" 20 "strings" 21 22 "yunion.io/x/pkg/errors" 23 24 "yunion.io/x/cloudmux/pkg/apis" 25 api "yunion.io/x/cloudmux/pkg/apis/compute" 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 "yunion.io/x/cloudmux/pkg/multicloud" 28 ) 29 30 // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212656.html 31 type SInstanceType struct { 32 multicloud.SResourceBase 33 HcsTags 34 35 Id string `json:"id"` 36 Name string `json:"name"` 37 Vcpus int `json:"vcpus"` 38 RamMB int `json:"ram"` // 内存大小 39 OSExtraSpecs OSExtraSpecs `json:"os_extra_specs"` // 扩展规格 40 } 41 42 type OSExtraSpecs struct { 43 EcsPerformancetype string `json:"ecs:performancetype"` 44 EcsGeneration string `json:"ecs:generation"` 45 EcsInstanceArchitecture string `json:"ecs:instance_architecture"` 46 } 47 48 func (self *SInstanceType) GetId() string { 49 return self.Id 50 } 51 52 func (self *SInstanceType) GetName() string { 53 return self.Id 54 } 55 56 func (self *SInstanceType) GetGlobalId() string { 57 return self.Id 58 } 59 60 func (self *SInstanceType) GetInstanceTypeFamily() string { 61 if len(self.OSExtraSpecs.EcsGeneration) > 0 { 62 return self.OSExtraSpecs.EcsGeneration 63 } 64 return strings.Split(self.Name, ".")[0] 65 } 66 67 func (self *SInstanceType) GetStatus() string { 68 return "" 69 } 70 71 func (self *SInstanceType) GetInstanceTypeCategory() string { 72 return self.OSExtraSpecs.EcsPerformancetype 73 } 74 75 func (self *SInstanceType) GetPrepaidStatus() string { 76 return api.SkuStatusSoldout 77 } 78 79 func (self *SInstanceType) GetPostpaidStatus() string { 80 return api.SkuStatusAvailable 81 } 82 83 func (self *SInstanceType) GetCpuArch() string { 84 if len(self.OSExtraSpecs.EcsInstanceArchitecture) > 0 { 85 if strings.HasPrefix(self.OSExtraSpecs.EcsInstanceArchitecture, "arm") { 86 return apis.OS_ARCH_AARCH64 87 } 88 return apis.OS_ARCH_X86 89 } 90 return "" 91 } 92 93 func (self *SInstanceType) GetCpuCoreCount() int { 94 return self.Vcpus 95 } 96 97 func (self *SInstanceType) GetMemorySizeMB() int { 98 return self.RamMB 99 } 100 101 func (self *SInstanceType) GetOsName() string { 102 return "" 103 } 104 105 func (self *SInstanceType) GetSysDiskResizable() bool { 106 return false 107 } 108 109 func (self *SInstanceType) GetSysDiskType() string { 110 return "" 111 } 112 113 func (self *SInstanceType) GetSysDiskMinSizeGB() int { 114 return 0 115 } 116 117 func (self *SInstanceType) GetSysDiskMaxSizeGB() int { 118 return 0 119 } 120 121 func (self *SInstanceType) GetAttachedDiskType() string { 122 return "" 123 } 124 125 func (self *SInstanceType) GetAttachedDiskSizeGB() int { 126 return 0 127 } 128 129 func (self *SInstanceType) GetAttachedDiskCount() int { 130 return 0 131 } 132 133 func (self *SInstanceType) GetDataDiskTypes() string { 134 return "" 135 } 136 137 func (self *SInstanceType) GetDataDiskMaxCount() int { 138 return 0 139 } 140 141 func (self *SInstanceType) GetNicType() string { 142 return "" 143 } 144 145 func (self *SInstanceType) GetNicMaxCount() int { 146 return 0 147 } 148 149 func (self *SInstanceType) GetGpuAttachable() bool { 150 return self.OSExtraSpecs.EcsPerformancetype == "gpu" 151 } 152 153 func (self *SInstanceType) GetGpuSpec() string { 154 if self.OSExtraSpecs.EcsPerformancetype == "gpu" { 155 return self.OSExtraSpecs.EcsGeneration 156 } 157 158 return "" 159 } 160 161 func (self *SInstanceType) GetGpuCount() int { 162 if self.OSExtraSpecs.EcsPerformancetype == "gpu" { 163 return 1 164 } 165 166 return 0 167 } 168 169 func (self *SInstanceType) GetGpuMaxCount() int { 170 if self.OSExtraSpecs.EcsPerformancetype == "gpu" { 171 return 1 172 } 173 174 return 0 175 } 176 177 func (self *SInstanceType) Delete() error { 178 return nil 179 } 180 181 func (self *SRegion) GetchInstanceTypes(zoneId string) ([]SInstanceType, error) { 182 query := url.Values{} 183 if len(zoneId) > 0 { 184 query.Set("availability_zone", zoneId) 185 } 186 ret := []SInstanceType{} 187 return ret, self.list("ecs", "v1", "cloudservers/flavors", query, &ret) 188 } 189 190 func (self *SRegion) GetchInstanceType(id string) (*SInstanceType, error) { 191 ret := &SInstanceType{} 192 res := fmt.Sprintf("flavors/%s", id) 193 return ret, self.get("ecs", "v2", res, &ret) 194 } 195 196 func (self *SRegion) GetMatchInstanceTypes(cpu int, memMB int, zoneId string) ([]SInstanceType, error) { 197 instanceTypes, err := self.GetchInstanceTypes(zoneId) 198 if err != nil { 199 return nil, err 200 } 201 202 ret := make([]SInstanceType, 0) 203 for _, t := range instanceTypes { 204 // cpu & mem & disk都匹配才行 205 if t.Vcpus == cpu && t.RamMB == memMB { 206 ret = append(ret, t) 207 } 208 } 209 210 return ret, nil 211 } 212 213 func (self *SRegion) GetISkus() ([]cloudprovider.ICloudSku, error) { 214 flavors, err := self.GetchInstanceTypes("") 215 if err != nil { 216 return nil, errors.Wrap(err, "fetchInstanceTypes") 217 } 218 ret := []cloudprovider.ICloudSku{} 219 for i := range flavors { 220 ret = append(ret, &flavors[i]) 221 } 222 return ret, nil 223 }