yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/redis_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 "strings" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/log" 23 24 "yunion.io/x/cloudmux/pkg/apis" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 ) 28 29 type SRedisSkuDetails struct { 30 Capacity int 31 MaxMemory float32 32 MaxConnections int 33 MaxClients int 34 MaxBandwidth int 35 MaxInBandwidth int 36 TenantIpCount int 37 ShardingNum int 38 ProxyNum int 39 DBNumber int 40 } 41 42 type SRedisFlavor struct { 43 Capacity int 44 Unit string 45 AvailableZones []string 46 AzCodes []string 47 } 48 49 type SRedisInstanceType struct { 50 multicloud.SResourceBase 51 ProductId string 52 SpecCode string 53 CacheMode string 54 ProductType string 55 CpuType string 56 StorageType string 57 Details SRedisSkuDetails 58 Engine string 59 EngineVersions string 60 SpecDetails string 61 SpecDetails2 string 62 CharingType string 63 Price float32 64 ProdType string 65 CloudServiceTypeCode string 66 CloudResourceTypeCode string 67 Flavors []SRedisFlavor 68 } 69 70 type SRedisSku struct { 71 SRedisSkuDetails 72 SRedisFlavor 73 ProductId string 74 SpecCode string 75 CacheMode string 76 ProductType string 77 CpuType string 78 StorageType string 79 Engine string 80 EngineVersion string 81 ProdType string 82 } 83 84 func (self *SRedisSku) GetName() string { 85 return self.SpecCode 86 } 87 88 func (self *SRedisSku) GetGlobalId() string { 89 return fmt.Sprintf("%s-%s-%s-%s-%d-%.2f", self.SpecCode, self.Engine, self.EngineVersion, self.CacheMode, self.SRedisFlavor.Capacity, self.MaxMemory) 90 } 91 92 func (self *SRedisSku) GetZoneId() string { 93 if len(self.AzCodes) > 0 { 94 return self.AzCodes[0] 95 } 96 return "" 97 } 98 99 func (self *SRedisSku) GetSlaveZoneId() string { 100 return "" 101 } 102 103 func (self *SRedisSku) GetEngineArch() string { 104 return self.CpuType 105 } 106 107 func (self *SRedisSku) GetLocalCategory() string { 108 return self.CacheMode 109 } 110 111 func (self *SRedisSku) GetPrepaidStatus() string { 112 return apis.SKU_STATUS_SOLDOUT 113 } 114 115 func (self *SRedisSku) GetPostpaidStatus() string { 116 return apis.SKU_STATUS_AVAILABLE 117 } 118 119 func (self *SRedisSku) GetEngine() string { 120 return self.Engine 121 } 122 123 func (self *SRedisSku) GetEngineVersion() string { 124 return self.EngineVersion 125 } 126 127 func (self *SRedisSku) GetCpuArch() string { 128 return self.CpuType 129 } 130 131 func (self *SRedisSku) GetStorageType() string { 132 return self.StorageType 133 } 134 135 func (self *SRedisSku) GetPerformanceType() string { 136 return "standard" 137 } 138 139 func (self *SRedisSku) GetNodeType() string { 140 return self.CacheMode 141 } 142 143 func (self *SRedisSku) GetDiskSizeGb() int { 144 return 0 145 } 146 147 func (self *SRedisSku) GetShardNum() int { 148 return self.ShardingNum 149 } 150 151 func (self *SRedisSku) GetMaxShardNum() int { 152 return self.ShardingNum 153 } 154 155 func (self *SRedisSku) GetReplicasNum() int { 156 return 0 157 } 158 159 func (self *SRedisSku) GetMaxReplicasNum() int { 160 return 0 161 } 162 163 func (self *SRedisSku) GetMaxClients() int { 164 return self.MaxClients 165 } 166 167 func (self *SRedisSku) GetMaxConnections() int { 168 return self.MaxConnections 169 } 170 171 func (self *SRedisSku) GetMaxInBandwidthMb() int { 172 return self.MaxInBandwidth 173 } 174 175 func (self *SRedisSku) GetMemorySizeMb() int { 176 return self.SRedisSkuDetails.Capacity * 1024 177 } 178 179 func (self *SRedisSku) GetMaxMemoryMb() int { 180 return int(self.MaxMemory) * 1024 181 } 182 183 func (self *SRedisSku) GetQps() int { 184 return 0 185 } 186 187 func (self *SRegion) getRedisInstnaceTypes() ([]SRedisInstanceType, error) { 188 ret := []SRedisInstanceType{} 189 return ret, self.redisList("products", nil, &ret) 190 } 191 192 func (self *SRegion) GetRedisInstnaceTypes() ([]SRedisSku, error) { 193 skus, err := self.getRedisInstnaceTypes() 194 if err != nil { 195 return nil, err 196 } 197 ret := []SRedisSku{} 198 for i := range skus { 199 details := []SRedisSkuDetails{} 200 detailsStr := strings.TrimPrefix(skus[i].SpecDetails2, "\n") 201 detailsStr = strings.TrimSuffix(detailsStr, "\n") 202 obj, err := jsonutils.ParseString(detailsStr) 203 if err != nil { 204 log.Errorf("parase details [%s], error: %v", detailsStr, err) 205 continue 206 } 207 err = obj.Unmarshal(&details) 208 if err != nil { 209 log.Errorf("unmarshal error: %v", err) 210 continue 211 } 212 for j := range details { 213 sku := SRedisSku{ 214 SRedisSkuDetails: details[j], 215 ProductId: skus[i].ProductId, 216 SpecCode: skus[i].SpecCode, 217 CacheMode: skus[i].CacheMode, 218 ProductType: skus[i].ProdType, 219 CpuType: skus[i].CpuType, 220 StorageType: skus[i].StorageType, 221 Engine: skus[i].Engine, 222 EngineVersion: skus[i].EngineVersions, 223 ProdType: skus[i].ProdType, 224 } 225 for k := range skus[i].Flavors { 226 if skus[i].Flavors[k].Capacity == details[j].Capacity { 227 sku.SRedisFlavor = skus[i].Flavors[k] 228 if len(sku.GetZoneId()) > 0 { 229 ret = append(ret, sku) 230 } 231 break 232 } 233 } 234 } 235 } 236 237 return ret, nil 238 } 239 240 func (self *SRegion) GetIElasticcacheSkus() ([]cloudprovider.ICloudElasticcacheSku, error) { 241 skus, err := self.GetRedisInstnaceTypes() 242 if err != nil { 243 return nil, err 244 } 245 ret := []cloudprovider.ICloudElasticcacheSku{} 246 for i := range skus { 247 ret = append(ret, &skus[i]) 248 } 249 return ret, nil 250 }