yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/offering.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 zstack 16 17 import ( 18 "fmt" 19 "net/url" 20 21 "github.com/pkg/errors" 22 23 "yunion.io/x/jsonutils" 24 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 type SInstanceOffering struct { 31 multicloud.SServerSku 32 ZStackTags 33 region *SRegion 34 35 ZStackBasic 36 MemorySize int `json:"memorySize"` 37 CPUNum int `json:"cpuNum"` 38 CPUSpeed int `json:"cpuSpeed"` 39 Type string `json:"type"` 40 AllocatorStrategy string `json:"allocatorStrategy"` 41 State string `json:"state"` 42 43 ZStackTime 44 } 45 46 func (region *SRegion) GetInstanceOffering(offerId string) (*SInstanceOffering, error) { 47 offer := &SInstanceOffering{region: region} 48 return offer, region.client.getResource("instance-offerings", offerId, offer) 49 } 50 51 func (region *SRegion) GetInstanceOfferingByType(instanceType string) (*SInstanceOffering, error) { 52 offerings, err := region.GetInstanceOfferings("", instanceType, 0, 0) 53 if err != nil { 54 return nil, err 55 } 56 if len(offerings) >= 1 { 57 return &offerings[0], nil 58 } 59 return nil, cloudprovider.ErrNotFound 60 } 61 62 func (region *SRegion) CreateISku(opts *cloudprovider.SServerSkuCreateOption) (cloudprovider.ICloudSku, error) { 63 sku, err := region.CreateInstanceOffering(opts.Name, opts.CpuCount, opts.VmemSizeMb, "UserVm") 64 if err != nil { 65 return nil, errors.Wrapf(err, "CreateISku") 66 } 67 return sku, nil 68 } 69 70 func (region *SRegion) CreateInstanceOffering(name string, cpu int, memoryMb int, offeringType string) (*SInstanceOffering, error) { 71 parmas := map[string]interface{}{ 72 "params": map[string]interface{}{ 73 "name": name, 74 "cpuNum": cpu, 75 "memorySize": memoryMb * 1024 * 1024, 76 "type": offeringType, 77 }, 78 } 79 resp, err := region.client.post("instance-offerings", jsonutils.Marshal(parmas)) 80 if err != nil { 81 return nil, errors.Wrapf(err, "CreateInstanceOffering") 82 } 83 offering := &SInstanceOffering{region: region} 84 err = resp.Unmarshal(offering, "inventory") 85 if err != nil { 86 return nil, errors.Wrapf(err, "resp.Unmarshal") 87 } 88 return offering, nil 89 } 90 91 func (region *SRegion) GetInstanceOfferings(offerId string, name string, cpu int, memorySizeMb int) ([]SInstanceOffering, error) { 92 offerings := []SInstanceOffering{} 93 params := url.Values{} 94 params.Add("q", "type=UserVM") 95 params.Add("q", "state=Enabled") 96 if len(offerId) > 0 { 97 params.Add("q", "uid="+offerId) 98 } 99 if len(name) > 0 { 100 params.Add("q", "name="+name) 101 } 102 if cpu != 0 { 103 params.Add("q", fmt.Sprintf("cpuNum=%d", cpu)) 104 } 105 if memorySizeMb != 0 { 106 params.Add("q", fmt.Sprintf("memorySize=%d", memorySizeMb*1024*1024)) 107 } 108 if err := region.client.listAll("instance-offerings", params, &offerings); err != nil { 109 return nil, err 110 } 111 for i := 0; i < len(offerings); i++ { 112 offerings[i].region = region 113 } 114 return offerings, nil 115 } 116 117 func (offering *SInstanceOffering) IsEmulated() bool { 118 return false 119 } 120 121 func (offering *SInstanceOffering) Refresh() error { 122 new, err := offering.region.GetInstanceOffering(offering.UUID) 123 if err != nil { 124 return err 125 } 126 return jsonutils.Update(offering, new) 127 } 128 129 func (offering *SInstanceOffering) GetName() string { 130 return offering.Name 131 } 132 133 func (offering *SInstanceOffering) GetStatus() string { 134 switch offering.State { 135 case "Enabled": 136 return api.SkuStatusReady 137 } 138 return api.SkuStatusSoldout 139 } 140 141 func (offering *SInstanceOffering) GetId() string { 142 return offering.UUID 143 } 144 145 func (offering *SInstanceOffering) GetGlobalId() string { 146 return offering.UUID 147 } 148 149 func (offering *SInstanceOffering) Delete() error { 150 return offering.region.DeleteOffering(offering.UUID) 151 } 152 153 func (region *SRegion) DeleteOffering(offeringId string) error { 154 return region.client.delete("instance-offerings", offeringId, "") 155 } 156 157 func (offering *SInstanceOffering) GetInstanceTypeFamily() string { 158 return api.InstanceFamilies[api.SkuCategoryGeneralPurpose] 159 } 160 161 func (offering *SInstanceOffering) GetInstanceTypeCategory() string { 162 return api.SkuCategoryGeneralPurpose 163 } 164 165 func (offering *SInstanceOffering) GetPrepaidStatus() string { 166 return api.SkuStatusSoldout 167 } 168 169 func (offering *SInstanceOffering) GetCpuArch() string { 170 return "" 171 } 172 173 func (offering *SInstanceOffering) GetPostpaidStatus() string { 174 return api.SkuStatusAvailable 175 } 176 177 func (offering *SInstanceOffering) GetCpuCoreCount() int { 178 return offering.CPUNum 179 } 180 181 func (offering *SInstanceOffering) GetMemorySizeMB() int { 182 return offering.MemorySize / 1024 / 1024 183 } 184 185 func (offering *SInstanceOffering) GetOsName() string { 186 return "Any" 187 } 188 189 func (offering *SInstanceOffering) GetSysDiskResizable() bool { 190 return true 191 } 192 193 func (offering *SInstanceOffering) GetSysDiskType() string { 194 return "" 195 } 196 197 func (offering *SInstanceOffering) GetSysDiskMinSizeGB() int { 198 return 0 199 } 200 201 func (offering *SInstanceOffering) GetSysDiskMaxSizeGB() int { 202 return 0 203 } 204 205 func (offering *SInstanceOffering) GetAttachedDiskType() string { 206 return "" 207 } 208 209 func (offering *SInstanceOffering) GetAttachedDiskSizeGB() int { 210 return 0 211 } 212 213 func (offering *SInstanceOffering) GetAttachedDiskCount() int { 214 return 0 215 } 216 217 func (offering *SInstanceOffering) GetDataDiskTypes() string { 218 return "" 219 } 220 221 func (offering *SInstanceOffering) GetDataDiskMaxCount() int { 222 return 0 223 } 224 225 func (offering *SInstanceOffering) GetNicType() string { 226 return "vpc" 227 } 228 229 func (offering *SInstanceOffering) GetNicMaxCount() int { 230 return 1 231 } 232 233 func (offering *SInstanceOffering) GetGpuAttachable() bool { 234 return false 235 } 236 237 func (offering *SInstanceOffering) GetGpuSpec() string { 238 return "" 239 } 240 241 func (offering *SInstanceOffering) GetGpuCount() int { 242 return 0 243 } 244 245 func (offering *SInstanceOffering) GetGpuMaxCount() int { 246 return 0 247 }