yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/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 ecloud 16 17 import ( 18 "context" 19 "fmt" 20 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 var regionList = map[string]string{ 29 "guangzhou-2": "华南-广州2", 30 "beijing-1": "华北-北京1", 31 "hunan-1": "华中-长沙1", 32 "wuxi-1": "华东-苏州", 33 "dongguan-1": "华南-广州3", 34 "yaan-1": "西南-成都", 35 "zhengzhou-1": "华中-郑州", 36 "beijing-2": "华北-北京3", 37 "zhuzhou-1": "华中-长沙2", 38 "jinan-1": "华东-济南", 39 "xian-1": "西北-西安", 40 "shanghai-1": "华东-上海1", 41 "chongqing-1": "西南-重庆", 42 "ningbo-1": "华东-杭州", 43 "tianjin-1": "天津-天津", 44 "jilin-1": "吉林-长春", 45 "hubei-1": "湖北-襄阳", 46 "jiangxi-1": "江西-南昌", 47 "gansu-1": "甘肃-兰州", 48 "shanxi-1": "山西-太原", 49 "liaoning-1": "辽宁-沈阳", 50 "yunnan-2": "云南-昆明2", 51 "hebei-1": "河北-石家庄", 52 "fujian-1": "福建-厦门", 53 "guangxi-1": "广西-南宁", 54 "anhui-1": "安徽-淮南", 55 "huhehaote-1": "华北-呼和浩特", 56 "guiyang-1": "西南-贵阳", 57 } 58 59 type SRegion struct { 60 cloudprovider.SFakeOnPremiseRegion 61 multicloud.SRegion 62 multicloud.SNoObjectStorageRegion 63 64 client *SEcloudClient 65 storageCache *SStoragecache 66 67 ID string `json:"id"` 68 Name string `json:"Name"` 69 70 izones []cloudprovider.ICloudZone 71 ivpcs []cloudprovider.ICloudVpc 72 } 73 74 func (r *SRegion) GetId() string { 75 return r.ID 76 } 77 78 func (r *SRegion) GetName() string { 79 return r.Name 80 } 81 82 func (r *SRegion) GetGlobalId() string { 83 return fmt.Sprintf("%s/%s", r.client.GetAccessEnv(), r.ID) 84 } 85 86 func (r *SRegion) GetStatus() string { 87 return api.CLOUD_REGION_STATUS_INSERVER 88 } 89 90 func (r *SRegion) Refresh() error { 91 // err := r.fetchZones() 92 // if err != nil { 93 // return err 94 // } 95 // return r.fetchVpcs() 96 return nil 97 } 98 99 func (r *SRegion) IsEmulated() bool { 100 return false 101 } 102 103 func (r *SRegion) GetI18n() cloudprovider.SModelI18nTable { 104 en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_ECLOUD_EN, r.Name) 105 table := cloudprovider.SModelI18nTable{} 106 table["name"] = cloudprovider.NewSModelI18nEntry(r.GetName()).CN(r.GetName()).EN(en) 107 return table 108 } 109 110 // GetLatitude() float32 111 // GetLongitude() float32 112 func (r *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo { 113 if info, ok := LatitudeAndLongitude[r.ID]; ok { 114 return info 115 } 116 return cloudprovider.SGeographicInfo{} 117 } 118 119 func (r *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) { 120 if r.izones == nil { 121 err := r.fetchZones() 122 if err != nil { 123 return nil, err 124 } 125 } 126 return r.izones, nil 127 } 128 129 func (r *SRegion) fetchZones() error { 130 request := NewNovaRequest(NewApiRequest(r.ID, "/api/v2/region", 131 map[string]string{"component": "NOVA"}, nil)) 132 zones := make([]SZone, 0) 133 err := r.client.doList(context.Background(), request, &zones) 134 if err != nil { 135 return err 136 } 137 izones := make([]cloudprovider.ICloudZone, len(zones)) 138 for i := range zones { 139 zones[i].region = r 140 zones[i].host = &SHost{ 141 zone: &zones[i], 142 } 143 izones[i] = &zones[i] 144 } 145 r.izones = izones 146 return nil 147 } 148 149 func (r *SRegion) fetchVpcs() error { 150 vpcs, err := r.getVpcs() 151 if err != nil { 152 return err 153 } 154 ivpcs := make([]cloudprovider.ICloudVpc, len(vpcs)) 155 for i := range vpcs { 156 ivpcs[i] = &vpcs[i] 157 } 158 r.ivpcs = ivpcs 159 return nil 160 } 161 162 func (r *SRegion) getVpcs() ([]SVpc, error) { 163 request := NewConsoleRequest(r.ID, "/api/v2/netcenter/vpc", nil, nil) 164 vpcs := make([]SVpc, 0) 165 err := r.client.doList(context.Background(), request, &vpcs) 166 if err != nil { 167 return nil, err 168 } 169 for i := range vpcs { 170 vpcs[i].region = r 171 } 172 return vpcs, err 173 } 174 175 func (r *SRegion) getVpcById(id string) (*SVpc, error) { 176 request := NewConsoleRequest(r.ID, fmt.Sprintf("/api/v2/netcenter/vpc/%s", id), nil, nil) 177 vpc := SVpc{} 178 err := r.client.doGet(context.Background(), request, &vpc) 179 if err != nil { 180 return nil, err 181 } 182 vpc.region = r 183 return &vpc, err 184 } 185 186 func (r *SRegion) getVpcByRouterId(id string) (*SVpc, error) { 187 request := NewConsoleRequest(r.ID, fmt.Sprintf("/api/v2/netcenter/vpc/router/%s", id), nil, nil) 188 vpc := SVpc{} 189 err := r.client.doGet(context.Background(), request, &vpc) 190 if err != nil { 191 return nil, err 192 } 193 vpc.region = r 194 return &vpc, err 195 } 196 197 func (r *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) { 198 if r.ivpcs == nil { 199 err := r.fetchVpcs() 200 if err != nil { 201 return nil, err 202 } 203 } 204 return r.ivpcs, nil 205 } 206 207 func (r *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) { 208 return nil, cloudprovider.ErrNotSupported 209 } 210 211 func (r *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) { 212 vpc, err := r.getVpcById(id) 213 if err != nil { 214 return nil, err 215 } 216 vpc.region = r 217 return vpc, nil 218 } 219 220 func (r *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) { 221 izones, err := r.GetIZones() 222 if err != nil { 223 return nil, err 224 } 225 for i := 0; i < len(izones); i += 1 { 226 if izones[i].GetGlobalId() == id { 227 return izones[i], nil 228 } 229 } 230 return nil, cloudprovider.ErrNotFound 231 } 232 233 func (r *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) { 234 return nil, cloudprovider.ErrNotImplemented 235 } 236 237 func (r *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) { 238 vm, err := r.GetInstanceById(id) 239 if err != nil { 240 return nil, err 241 } 242 zone, err := r.FindZone(vm.Region) 243 if err != nil { 244 return nil, err 245 } 246 vm.host = &SHost{ 247 zone: zone, 248 } 249 return vm, nil 250 } 251 252 func (r *SRegion) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) { 253 return r.GetDisk(id) 254 } 255 256 func (r *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) { 257 izones, err := r.GetIZones() 258 if err != nil { 259 return nil, err 260 } 261 iHosts := make([]cloudprovider.ICloudHost, 0, len(izones)) 262 for i := range izones { 263 hosts, err := izones[i].GetIHosts() 264 if err != nil { 265 return nil, err 266 } 267 iHosts = append(iHosts, hosts...) 268 } 269 return iHosts, nil 270 } 271 272 func (r *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) { 273 hosts, err := r.GetIHosts() 274 if err != nil { 275 return nil, err 276 } 277 for i := range hosts { 278 if hosts[i].GetGlobalId() == id { 279 return hosts[i], nil 280 } 281 } 282 return nil, cloudprovider.ErrNotFound 283 } 284 285 func (r *SRegion) GetIStorages() ([]cloudprovider.ICloudStorage, error) { 286 iStores := make([]cloudprovider.ICloudStorage, 0) 287 288 izones, err := r.GetIZones() 289 if err != nil { 290 return nil, err 291 } 292 for i := 0; i < len(izones); i += 1 { 293 iZoneStores, err := izones[i].GetIStorages() 294 if err != nil { 295 return nil, err 296 } 297 iStores = append(iStores, iZoneStores...) 298 } 299 return iStores, nil 300 } 301 302 func (r *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) { 303 istores, err := r.GetIStorages() 304 if err != nil { 305 return nil, err 306 } 307 for i := range istores { 308 if istores[i].GetGlobalId() == id { 309 return istores[i], nil 310 } 311 } 312 return nil, cloudprovider.ErrNotFound 313 } 314 315 func (r *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) { 316 sc := r.getStoragecache() 317 return []cloudprovider.ICloudStoragecache{sc}, nil 318 } 319 320 func (r *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) { 321 storageCache := r.getStoragecache() 322 if storageCache.GetGlobalId() == id { 323 return storageCache, nil 324 } 325 return nil, cloudprovider.ErrNotFound 326 } 327 328 func (r *SRegion) GetProvider() string { 329 return api.CLOUD_PROVIDER_ECLOUD 330 } 331 332 func (r *SRegion) GetCapabilities() []string { 333 return r.client.GetCapabilities() 334 } 335 336 func (r *SRegion) GetClient() *SEcloudClient { 337 return r.client 338 } 339 340 func (r *SRegion) FindZone(zoneRegion string) (*SZone, error) { 341 izones, err := r.GetIZones() 342 if err != nil { 343 return nil, errors.Wrap(err, "unable to GetZones") 344 } 345 findZone := func(zoneRegion string) *SZone { 346 for i := range izones { 347 zone := izones[i].(*SZone) 348 if zone.Region == zoneRegion { 349 return zone 350 } 351 } 352 return nil 353 } 354 return findZone(zoneRegion), nil 355 }