region.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. package bingocloud
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type SRegion struct {
  24. multicloud.SRegion
  25. multicloud.SRegionEipBase
  26. multicloud.SRegionLbBase
  27. multicloud.SRegionOssBase
  28. multicloud.SRegionSecurityGroupBase
  29. multicloud.SRegionVpcBase
  30. multicloud.SRegionZoneBase
  31. client *SBingoCloudClient
  32. RegionId string
  33. RegionName string
  34. Hypervisor string
  35. NetworkMode string
  36. RegionEndpoint string
  37. }
  38. func (self *SRegion) GetId() string {
  39. return self.RegionId
  40. }
  41. func (self *SRegion) GetName() string {
  42. return self.RegionName
  43. }
  44. func (self *SRegion) GetGlobalId() string {
  45. return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_BINGO_CLOUD, self.RegionId)
  46. }
  47. func (self *SRegion) GetClient() *SBingoCloudClient {
  48. return self.client
  49. }
  50. func (self *SRegion) GetCapabilities() []string {
  51. return self.client.GetCapabilities()
  52. }
  53. func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable {
  54. return cloudprovider.SModelI18nTable{}
  55. }
  56. func (self *SRegion) GetProvider() string {
  57. return api.CLOUD_PROVIDER_BINGO_CLOUD
  58. }
  59. func (self *SRegion) GetStatus() string {
  60. return api.CLOUD_REGION_STATUS_INSERVER
  61. }
  62. func (self *SRegion) GetCloudEnv() string {
  63. return ""
  64. }
  65. func (self *SRegion) getAccountUser() string {
  66. quotas, err := self.GetQuotas()
  67. if err != nil {
  68. return ""
  69. }
  70. ownerId := ""
  71. if len(quotas) > 0 {
  72. ownerId = quotas[0].OwnerId
  73. }
  74. return ownerId
  75. }
  76. func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
  77. return cloudprovider.SGeographicInfo{}
  78. }
  79. func (self *SRegion) invoke(action string, params map[string]string) (jsonutils.JSONObject, error) {
  80. return self.client.invoke(action, params)
  81. }
  82. func (self *SBingoCloudClient) GetRegions() ([]SRegion, error) {
  83. resp, err := self.invoke("DescribeRegions", nil)
  84. if err != nil {
  85. return nil, err
  86. }
  87. var ret []SRegion
  88. return ret, resp.Unmarshal(&ret, "regionInfo")
  89. }
  90. func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  91. storages, err := self.getStorages()
  92. if err != nil {
  93. return nil, err
  94. }
  95. for i := range storages {
  96. if storages[i].GetGlobalId() == id {
  97. storages[i].cluster = &SCluster{region: self}
  98. return &storages[i], nil
  99. }
  100. }
  101. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  102. }
  103. func (self *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) {
  104. var 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. var 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. func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) {
  129. caches, err := self.GetIStoragecaches()
  130. if err != nil {
  131. return nil, err
  132. }
  133. for i := range caches {
  134. if caches[i].GetGlobalId() == id {
  135. return caches[i], nil
  136. }
  137. }
  138. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  139. }
  140. func (self *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  141. zones, err := self.GetIZones()
  142. if err != nil {
  143. return nil, err
  144. }
  145. var ret []cloudprovider.ICloudHost
  146. for i := range zones {
  147. hosts, err := zones[i].GetIHosts()
  148. if err != nil {
  149. return nil, err
  150. }
  151. ret = append(ret, hosts...)
  152. }
  153. return ret, nil
  154. }
  155. func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  156. zones, err := self.GetIZones()
  157. if err != nil {
  158. return nil, err
  159. }
  160. for i := range zones {
  161. hosts, err := zones[i].GetIHosts()
  162. if err != nil {
  163. return nil, err
  164. }
  165. for i := range hosts {
  166. if hosts[i].GetGlobalId() == id {
  167. return hosts[i], nil
  168. }
  169. }
  170. }
  171. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  172. }
  173. func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  174. snapshots, err := self.getSnapshots("", "")
  175. if err != nil {
  176. return nil, err
  177. }
  178. iSnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots))
  179. for i := 0; i < len(snapshots); i++ {
  180. snapshots[i].region = self
  181. iSnapshots[i] = snapshots[i]
  182. }
  183. return iSnapshots, nil
  184. }
  185. func (self *SRegion) GetISnapshotById(id string) (cloudprovider.ICloudSnapshot, error) {
  186. snapshots, err := self.getSnapshots(id, "")
  187. if err != nil {
  188. return nil, err
  189. }
  190. for i := range snapshots {
  191. if snapshots[i].GetGlobalId() == id {
  192. snapshots[i].region = self
  193. return &snapshots[i], nil
  194. }
  195. }
  196. return nil, cloudprovider.ErrNotFound
  197. }
  198. func (self *SRegion) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  199. var groups []SSecurityGroup
  200. nextToken := ""
  201. for {
  202. part, _nextToken, err := self.GetSecurityGroups("", "", nextToken)
  203. if err != nil {
  204. return nil, err
  205. }
  206. groups = append(groups, part...)
  207. if len(_nextToken) == 0 || len(part) == 0 {
  208. break
  209. }
  210. nextToken = _nextToken
  211. }
  212. var ret []cloudprovider.ICloudSecurityGroup
  213. for i := range groups {
  214. groups[i].region = self
  215. ret = append(ret, &groups[i])
  216. }
  217. return ret, nil
  218. }
  219. func (region *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  220. var vms []SInstance
  221. nextToken := ""
  222. for {
  223. part, _nextToken, err := region.GetInstances("", "", MAX_RESULT, nextToken)
  224. if err != nil {
  225. return nil, err
  226. }
  227. vms = append(vms, part...)
  228. if len(part) == 0 || len(_nextToken) == 0 {
  229. break
  230. }
  231. nextToken = _nextToken
  232. }
  233. var ret []cloudprovider.ICloudVM
  234. for i := range vms {
  235. ret = append(ret, &vms[i])
  236. }
  237. return ret, nil
  238. }