yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/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 bingocloud 16 17 import ( 18 "fmt" 19 20 "yunion.io/x/jsonutils" 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 type SRegion struct { 29 multicloud.SRegion 30 multicloud.SRegionEipBase 31 multicloud.SRegionLbBase 32 multicloud.SRegionOssBase 33 multicloud.SRegionSecurityGroupBase 34 multicloud.SRegionVpcBase 35 multicloud.SRegionZoneBase 36 37 client *SBingoCloudClient 38 39 RegionId string 40 RegionName string 41 Hypervisor string 42 NetworkMode string 43 RegionEndpoint string 44 } 45 46 func (self *SRegion) GetId() string { 47 return self.RegionId 48 } 49 50 func (self *SRegion) GetName() string { 51 return self.RegionName 52 } 53 54 func (self *SRegion) GetGlobalId() string { 55 return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_BINGO_CLOUD, self.RegionId) 56 } 57 58 func (self *SRegion) GetClient() *SBingoCloudClient { 59 return self.client 60 } 61 62 func (self *SRegion) GetCapabilities() []string { 63 return self.client.GetCapabilities() 64 } 65 66 func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable { 67 return cloudprovider.SModelI18nTable{} 68 } 69 70 func (self *SRegion) GetProvider() string { 71 return api.CLOUD_PROVIDER_BINGO_CLOUD 72 } 73 74 func (self *SRegion) GetStatus() string { 75 return api.CLOUD_REGION_STATUS_INSERVER 76 } 77 78 func (self *SRegion) GetCloudEnv() string { 79 return "" 80 } 81 82 func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo { 83 return cloudprovider.SGeographicInfo{} 84 } 85 86 func (self *SRegion) invoke(action string, params map[string]string) (jsonutils.JSONObject, error) { 87 return self.client.invoke(action, params) 88 } 89 90 func (self *SBingoCloudClient) GetRegions() ([]SRegion, error) { 91 resp, err := self.invoke("DescribeRegions", nil) 92 if err != nil { 93 return nil, err 94 } 95 ret := []SRegion{} 96 err = resp.Unmarshal(&ret, "regionInfo") 97 if err != nil { 98 return nil, err 99 } 100 return ret, nil 101 } 102 103 func (self *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) { 104 storages := []SStorage{} 105 part, nextToken, err := self.GetStorages("") 106 if err != nil { 107 return nil, err 108 } 109 storages = append(storages, part...) 110 for len(nextToken) > 0 { 111 part, nextToken, err = self.GetStorages(nextToken) 112 if err != nil { 113 return nil, err 114 } 115 storages = append(storages, part...) 116 } 117 ret := []cloudprovider.ICloudStoragecache{} 118 for i := range storages { 119 cache := SStoragecache{ 120 region: self, 121 storageName: storages[i].StorageName, 122 storageId: storages[i].StorageId, 123 } 124 ret = append(ret, &cache) 125 } 126 return ret, nil 127 } 128 129 func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) { 130 caches, err := self.GetIStoragecaches() 131 if err != nil { 132 return nil, err 133 } 134 for i := range caches { 135 if caches[i].GetGlobalId() == id { 136 return caches[i], nil 137 } 138 } 139 return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) 140 } 141 142 func (self *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) { 143 zones, err := self.GetIZones() 144 if err != nil { 145 return nil, err 146 } 147 ret := []cloudprovider.ICloudHost{} 148 for i := range zones { 149 hosts, err := zones[i].GetIHosts() 150 if err != nil { 151 return nil, err 152 } 153 ret = append(ret, hosts...) 154 } 155 return ret, nil 156 } 157 158 func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) { 159 zones, err := self.GetIZones() 160 if err != nil { 161 return nil, err 162 } 163 for i := range zones { 164 hosts, err := zones[i].GetIHosts() 165 if err != nil { 166 return nil, err 167 } 168 for i := range hosts { 169 if hosts[i].GetGlobalId() == id { 170 return hosts[i], nil 171 } 172 } 173 } 174 return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) 175 }