yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud 16 17 import ( 18 "fmt" 19 20 "github.com/jdcloud-api/jdcloud-sdk-go/core" 21 22 api "yunion.io/x/cloudmux/pkg/apis/compute" 23 "yunion.io/x/cloudmux/pkg/cloudprovider" 24 "yunion.io/x/cloudmux/pkg/multicloud" 25 ) 26 27 const ( 28 CLOUD_PROVIDER_JDCLOUD = api.CLOUD_PROVIDER_JDCLOUD 29 CLOUD_PROVIDER_JDCLOUD_CN = "京东云" 30 CLOUD_PROVIDER_JDCLOUD_EN = "JDcloud" 31 32 JDCLOUD_DEFAULT_REGION = "cn-north-1" 33 ) 34 35 var regionList = map[string]string{ 36 "cn-north-1": "华北-北京", 37 "cn-east-1": "华东-宿迁", 38 "cn-east-2": "华东-上海", 39 "cn-south-1": "华南-广州", 40 } 41 42 type SRegion struct { 43 cloudprovider.SFakeOnPremiseRegion 44 multicloud.SRegion 45 multicloud.SNoObjectStorageRegion 46 47 client *SJDCloudClient 48 49 cpcfg cloudprovider.ProviderConfig 50 storageCache *SStoragecache 51 52 ID string `json:"id"` 53 Name string `json:"Name"` 54 55 izones []cloudprovider.ICloudZone 56 ivpcs []cloudprovider.ICloudVpc 57 } 58 59 func (self *SRegion) GetClient() *SJDCloudClient { 60 return self.client 61 } 62 63 func (self *SRegion) getCredential() *core.Credential { 64 return self.client.getCredential() 65 } 66 67 func (r *SRegion) GetId() string { 68 return r.ID 69 } 70 71 func (r *SRegion) GetName() string { 72 return r.Name 73 } 74 75 func (r *SRegion) GetGlobalId() string { 76 return fmt.Sprintf("%s/%s", api.CLOUD_ACCESS_ENV_JDCLOUD_CHINA, r.GetId()) 77 } 78 79 func (r *SRegion) GetStatus() string { 80 return api.CLOUD_REGION_STATUS_INSERVER 81 } 82 83 func (r *SRegion) Refresh() error { 84 return nil 85 } 86 87 func (r *SRegion) IsEmulated() bool { 88 return false 89 } 90 91 func (r *SRegion) GetI18n() cloudprovider.SModelI18nTable { 92 en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_JDCLOUD_EN, r.Name) 93 table := cloudprovider.SModelI18nTable{} 94 table["name"] = cloudprovider.NewSModelI18nEntry(r.GetName()).CN(r.GetName()).EN(en) 95 return table 96 } 97 98 func (r *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo { 99 if info, ok := LatitudeAndLongitude[r.ID]; ok { 100 return info 101 } 102 return cloudprovider.SGeographicInfo{} 103 } 104 105 func (r *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) { 106 if r.izones == nil { 107 err := r.fetchZones() 108 if err != nil { 109 return nil, err 110 } 111 } 112 return r.izones, nil 113 } 114 115 func (r *SRegion) fetchZones() error { 116 zoneIdens := ZonesInRegion[r.ID] 117 r.izones = make([]cloudprovider.ICloudZone, 0, len(zoneIdens)) 118 for _, iden := range zoneIdens { 119 zone := SZone{ 120 region: r, 121 ID: iden.Id, 122 Name: iden.Name, 123 } 124 r.izones = append(r.izones, &zone) 125 } 126 return nil 127 } 128 129 func (r *SRegion) fetchVpcs() error { 130 vpcs := make([]SVpc, 0) 131 n := 1 132 for { 133 part, total, err := r.GetVpcs(n, 100) 134 if err != nil { 135 return err 136 } 137 vpcs = append(vpcs, part...) 138 if len(vpcs) >= total { 139 break 140 } 141 n++ 142 } 143 r.ivpcs = make([]cloudprovider.ICloudVpc, len(vpcs)) 144 for i := 0; i < len(vpcs); i++ { 145 r.ivpcs[i] = &vpcs[i] 146 } 147 return nil 148 } 149 150 func (r *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) { 151 if r.ivpcs == nil { 152 err := r.fetchVpcs() 153 if err != nil { 154 return nil, err 155 } 156 } 157 return r.ivpcs, nil 158 } 159 160 func (r *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) { 161 return nil, nil 162 } 163 164 func (r *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) { 165 vpc, err := r.GetVpcById(id) 166 if err != nil { 167 return nil, err 168 } 169 return vpc, nil 170 } 171 172 func (r *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) { 173 izones, err := r.GetIZones() 174 if err != nil { 175 return nil, err 176 } 177 for i := 0; i < len(izones); i += 1 { 178 if izones[i].GetGlobalId() == id { 179 return izones[i], nil 180 } 181 } 182 return nil, cloudprovider.ErrNotFound 183 } 184 185 func (r *SRegion) getIZoneByRealId(id string) (cloudprovider.ICloudZone, error) { 186 izones, err := r.GetIZones() 187 if err != nil { 188 return nil, err 189 } 190 for i := 0; i < len(izones); i += 1 { 191 if izones[i].GetId() == id { 192 return izones[i], nil 193 } 194 } 195 return nil, cloudprovider.ErrNotFound 196 } 197 198 func (r *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) { 199 return nil, cloudprovider.ErrNotImplemented 200 } 201 202 func (r *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) { 203 vm, err := r.GetInstanceById(id) 204 if err != nil { 205 return nil, err 206 } 207 izone, err := r.getIZoneByRealId(vm.Az) 208 if err != nil { 209 return nil, err 210 } 211 zone := izone.(*SZone) 212 vm.host = &SHost{ 213 zone: zone, 214 } 215 return vm, nil 216 } 217 218 func (r *SRegion) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) { 219 return r.GetDiskById(id) 220 } 221 222 func (r *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) { 223 izones, err := r.GetIZones() 224 if err != nil { 225 return nil, err 226 } 227 iHosts := make([]cloudprovider.ICloudHost, 0, len(izones)) 228 for i := range izones { 229 hosts, err := izones[i].GetIHosts() 230 if err != nil { 231 return nil, err 232 } 233 iHosts = append(iHosts, hosts...) 234 } 235 return iHosts, nil 236 } 237 238 func (r *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) { 239 hosts, err := r.GetIHosts() 240 if err != nil { 241 return nil, err 242 } 243 for i := range hosts { 244 if hosts[i].GetGlobalId() == id { 245 return hosts[i], nil 246 } 247 } 248 return nil, cloudprovider.ErrNotFound 249 } 250 251 func (r *SRegion) GetIStorages() ([]cloudprovider.ICloudStorage, error) { 252 iStores := make([]cloudprovider.ICloudStorage, 0) 253 254 izones, err := r.GetIZones() 255 if err != nil { 256 return nil, err 257 } 258 for i := 0; i < len(izones); i += 1 { 259 iZoneStores, err := izones[i].GetIStorages() 260 if err != nil { 261 return nil, err 262 } 263 iStores = append(iStores, iZoneStores...) 264 } 265 return iStores, nil 266 } 267 268 func (r *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) { 269 istores, err := r.GetIStorages() 270 if err != nil { 271 return nil, err 272 } 273 for i := range istores { 274 if istores[i].GetGlobalId() == id { 275 return istores[i], nil 276 } 277 } 278 return nil, cloudprovider.ErrNotFound 279 } 280 281 func (r *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) { 282 sc := r.getStoragecache() 283 return []cloudprovider.ICloudStoragecache{sc}, nil 284 } 285 286 func (s *SRegion) getStoragecache() *SStoragecache { 287 if s.storageCache == nil { 288 s.storageCache = &SStoragecache{region: s} 289 } 290 return s.storageCache 291 } 292 293 func (r *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) { 294 storageCache := r.getStoragecache() 295 if storageCache.GetGlobalId() == id { 296 return storageCache, nil 297 } 298 return nil, cloudprovider.ErrNotFound 299 } 300 301 func (r *SRegion) GetProvider() string { 302 return api.CLOUD_PROVIDER_JDCLOUD 303 } 304 305 func (r *SRegion) GetCapabilities() []string { 306 return []string{ 307 cloudprovider.CLOUD_CAPABILITY_COMPUTE + cloudprovider.READ_ONLY_SUFFIX, 308 cloudprovider.CLOUD_CAPABILITY_NETWORK + cloudprovider.READ_ONLY_SUFFIX, 309 cloudprovider.CLOUD_CAPABILITY_EIP + cloudprovider.READ_ONLY_SUFFIX, 310 cloudprovider.CLOUD_CAPABILITY_RDS + cloudprovider.READ_ONLY_SUFFIX, 311 } 312 }