yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/region.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 "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" 23 24 "yunion.io/x/pkg/errors" 25 26 api "yunion.io/x/cloudmux/pkg/apis/compute" 27 "yunion.io/x/cloudmux/pkg/cloudprovider" 28 "yunion.io/x/cloudmux/pkg/multicloud" 29 ) 30 31 type SRegion struct { 32 HcsTags 33 multicloud.SRegion 34 multicloud.SRegionEipBase 35 multicloud.SRegionOssBase 36 multicloud.SRegionLbBase 37 multicloud.SRegionVpcBase 38 multicloud.SRegionZoneBase 39 multicloud.SRegionSecurityGroupBase 40 41 obsClient *obs.ObsClient // 对象存储client.请勿直接引用。 42 43 client *SHcsClient 44 45 Id string 46 CloudInfras []string 47 Description string 48 Locales struct { 49 EnUS string `json:"en-us"` 50 ZhCN string `json:"zh-cn"` 51 } 52 ParentRegionId string 53 } 54 55 func (self *SRegion) GetClient() *SHcsClient { 56 return self.client 57 } 58 59 func (self *SRegion) GetId() string { 60 return self.Id 61 } 62 63 func (self *SRegion) GetGlobalId() string { 64 return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_HCS, self.Id) 65 } 66 67 func (self *SRegion) GetName() string { 68 return fmt.Sprintf("%s %s", CLOUD_PROVIDER_HCS_CN, self.Locales.ZhCN) 69 } 70 71 func (self *SRegion) GetStatus() string { 72 return api.CLOUD_REGION_STATUS_INSERVER 73 } 74 75 func (self *SRegion) GetILoadBalancerBackendGroups() ([]cloudprovider.ICloudLoadbalancerBackendGroup, error) { 76 return nil, cloudprovider.ErrNotImplemented 77 } 78 79 func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable { 80 en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_HCS_EN, self.Locales.EnUS) 81 table := cloudprovider.SModelI18nTable{} 82 table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en) 83 return table 84 } 85 86 func (self *SRegion) GetCapabilities() []string { 87 return self.client.GetCapabilities() 88 } 89 90 func (self *SRegion) GetCloudEnv() string { 91 return "" 92 } 93 94 func (self *SRegion) GetProvider() string { 95 return CLOUD_PROVIDER_HCS 96 } 97 98 func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo { 99 return cloudprovider.SGeographicInfo{} 100 } 101 102 func (self *SRegion) ecsList(resource string, query url.Values, retVal interface{}) error { 103 return self.client.ecsList(self.Id, resource, query, retVal) 104 } 105 106 func (self *SRegion) ecsGet(resource string, retVal interface{}) error { 107 return self.client.ecsGet(self.Id, resource, retVal) 108 } 109 110 func (self *SRegion) ecsDelete(resource string) error { 111 return self.client.ecsDelete(self.Id, resource) 112 } 113 114 func (self *SRegion) ecsCreate(resource string, body map[string]interface{}, retVal interface{}) error { 115 return self.client.ecsCreate(self.Id, resource, body, retVal) 116 } 117 118 func (self *SRegion) ecsPerform(resource, action string, params map[string]interface{}, retVal interface{}) error { 119 return self.client.ecsPerform(self.Id, resource, action, params, retVal) 120 } 121 122 func (region *SRegion) rdsList(resource string, query url.Values, retVal interface{}) error { 123 return region.client.rdsList(region.Id, resource, query, retVal) 124 } 125 126 func (region *SRegion) rdsGet(resource string, retVal interface{}) error { 127 return region.client.rdsGet(region.Id, resource, retVal) 128 } 129 130 func (region *SRegion) rdsDelete(resource string) error { 131 return region.client.rdsDelete(region.Id, resource) 132 } 133 134 func (region *SRegion) rdsCreate(resource string, body map[string]interface{}, retVal interface{}) error { 135 return region.client.rdsCreate(region.Id, resource, body, retVal) 136 } 137 138 func (self *SRegion) rdsPerform(resource, action string, params map[string]interface{}, retVal interface{}) error { 139 return self.client.rdsPerform(self.Id, resource, action, params, retVal) 140 } 141 142 func (region *SRegion) rdsJobGet(resource string, query url.Values, retVal interface{}) error { 143 return region.client.rdsJobGet(region.Id, resource, query, retVal) 144 } 145 146 func (region *SRegion) rdsDBPrivilegesDelete(resource string, params map[string]interface{}) error { 147 return region.client.rdsDBPrivvilegesDelete(region.Id, resource, params) 148 } 149 150 func (region *SRegion) rdsDBPrivilegesGrant(resource string, params map[string]interface{}, retVal interface{}) error { 151 return region.client.rdsDBPrivilegesGrant(region.Id, resource, params, retVal) 152 } 153 154 func (self *SRegion) perform(product, version, resource, action string, params map[string]interface{}, retVal interface{}) error { 155 return self.client.perform(product, version, self.Id, resource, action, params, retVal) 156 } 157 158 func (self *SRegion) create(product, version, resource string, body map[string]interface{}, retVal interface{}) error { 159 return self.client.create(product, version, self.Id, resource, body, retVal) 160 } 161 162 func (self *SRegion) delete(product, version, resource string) error { 163 return self.client.delete(product, version, self.Id, resource) 164 } 165 166 func (self *SRegion) list(product, version, resource string, query url.Values, retVal interface{}) error { 167 return self.client.list(product, version, self.Id, resource, query, retVal) 168 } 169 170 func (self *SRegion) get(product, version, resource string, retVal interface{}) error { 171 return self.client.get(product, version, self.Id, resource, retVal) 172 } 173 func (self *SRegion) update(product, version, resource string, params map[string]interface{}) error { 174 return self.client.update(product, version, self.Id, resource, params) 175 } 176 177 func (self *SRegion) evsList(resource string, query url.Values, retVal interface{}) error { 178 return self.client.evsList(self.Id, resource, query, retVal) 179 } 180 181 func (self *SRegion) evsGet(resource string, retVal interface{}) error { 182 return self.client.evsGet(self.Id, resource, retVal) 183 } 184 185 func (self *SRegion) evsDelete(resource string) error { 186 return self.client.evsDelete(self.Id, resource) 187 } 188 189 func (self *SRegion) evsPerform(resource, action string, params map[string]interface{}) error { 190 return self.client.evsPerform(self.Id, resource, action, params) 191 } 192 193 func (self *SRegion) evsCreate(resource string, body map[string]interface{}, retVal interface{}) error { 194 return self.client.evsCreate(self.Id, resource, body, retVal) 195 } 196 197 func (self *SRegion) vpcList(resource string, query url.Values, retVal interface{}) error { 198 return self.client.vpcList(self.Id, resource, query, retVal) 199 } 200 201 func (self *SRegion) vpcCreate(resource string, body map[string]interface{}, retVal interface{}) error { 202 return self.client.vpcCreate(self.Id, resource, body, retVal) 203 } 204 205 func (self *SRegion) vpcGet(resource string, retVal interface{}) error { 206 return self.client.vpcGet(self.Id, resource, retVal) 207 } 208 209 func (self *SRegion) vpcUpdate(resource string, params map[string]interface{}) error { 210 return self.client.vpcUpdate(self.Id, resource, params) 211 } 212 213 func (self *SRegion) vpcDelete(resource string) error { 214 return self.client.vpcDelete(self.Id, resource) 215 } 216 217 func (self *SRegion) imsList(resource string, query url.Values, retVal interface{}) error { 218 return self.client.imsList(self.Id, resource, query, retVal) 219 } 220 221 func (self *SRegion) imsCreate(resource string, body map[string]interface{}, retVal interface{}) error { 222 return self.client.imsCreate(self.Id, resource, body, retVal) 223 } 224 225 func (self *SRegion) imsGet(resource string, retVal interface{}) error { 226 return self.client.imsGet(self.Id, resource, retVal) 227 } 228 229 func (self *SRegion) imsUpdate(resource string, params map[string]interface{}) error { 230 return self.client.imsUpdate(self.Id, resource, params) 231 } 232 233 func (self *SRegion) imsDelete(resource string) error { 234 return self.client.imsDelete(self.Id, resource) 235 } 236 237 func (self *SRegion) imsPerform(resource, action string, params map[string]interface{}, retVal interface{}) error { 238 return self.client.imsPerform(self.Id, resource, action, params, retVal) 239 } 240 241 func (self *SRegion) redisList(resource string, query url.Values, retVal interface{}) error { 242 return self.client.dcsList(self.Id, resource, query, retVal) 243 } 244 245 func (self *SRegion) redisCreate(resource string, body map[string]interface{}, retVal interface{}) error { 246 return self.client.dcsCreate(self.Id, resource, body, retVal) 247 } 248 249 func (self *SRegion) redisGet(resource string, retVal interface{}) error { 250 return self.client.dcsGet(self.Id, resource, retVal) 251 } 252 253 func (self *SRegion) redisUpdate(resource string, params map[string]interface{}) error { 254 return self.client.dcsUpdate(self.Id, resource, params) 255 } 256 257 func (self *SRegion) redisDelete(resource string) error { 258 return self.client.dcsDelete(self.Id, resource) 259 } 260 261 func (self *SRegion) redisPerform(resource, action string, params map[string]interface{}, retVal interface{}) error { 262 return self.client.dcsPerform(self.Id, resource, action, params, retVal) 263 } 264 265 func (self *SRegion) GetICloudSku(skuId string) (cloudprovider.ICloudSku, error) { 266 sku, err := self.GetchInstanceType(skuId) 267 if err != nil { 268 return nil, err 269 } 270 return sku, nil 271 } 272 273 func (self *SRegion) getOBSClient() (*obs.ObsClient, error) { 274 if self.obsClient == nil { 275 obsClient, err := self.client.getOBSClient(self.GetId()) 276 if err != nil { 277 return nil, err 278 } 279 self.obsClient = obsClient 280 } 281 return self.obsClient, nil 282 } 283 284 func (self *SRegion) HeadBucket(name string) (*obs.BaseModel, error) { 285 obsClient, err := self.getOBSClient() 286 if err != nil { 287 return nil, errors.Wrap(err, "region.getOBSClient") 288 } 289 return obsClient.HeadBucket(name) 290 } 291 292 func (self *SRegion) getOBSEndpoint() string { 293 return self.client.getOBSEndpoint(self.GetId()) 294 } 295 296 func str2StorageClass(storageClassStr string) (obs.StorageClassType, error) { 297 if strings.EqualFold(storageClassStr, string(obs.StorageClassStandard)) { 298 return obs.StorageClassStandard, nil 299 } else if strings.EqualFold(storageClassStr, string(obs.StorageClassWarm)) { 300 return obs.StorageClassWarm, nil 301 } else if strings.EqualFold(storageClassStr, string(obs.StorageClassCold)) { 302 return obs.StorageClassCold, nil 303 } else { 304 return obs.StorageClassStandard, errors.Error("unsupported storageClass") 305 } 306 } 307 308 func (self *SRegion) GetBuckets() ([]SBucket, error) { 309 buckets, err := self.client.GetBuckets() 310 if err != nil { 311 return nil, err 312 } 313 ret := []SBucket{} 314 for i := range buckets { 315 if buckets[i].region.GetGlobalId() == self.GetGlobalId() { 316 ret = append(ret, buckets[i]) 317 } 318 } 319 return ret, nil 320 } 321 322 func (self *SRegion) GetIBuckets() ([]cloudprovider.ICloudBucket, error) { 323 buckets, err := self.GetBuckets() 324 if err != nil { 325 return nil, errors.Wrap(err, "GetBuckets") 326 } 327 ret := make([]cloudprovider.ICloudBucket, 0) 328 for i := range buckets { 329 ret = append(ret, &buckets[i]) 330 } 331 return ret, nil 332 }