region.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 ksyun
  15. import (
  16. "fmt"
  17. api "yunion.io/x/cloudmux/pkg/apis/compute"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. )
  23. type SRegion struct {
  24. multicloud.SRegion
  25. multicloud.SNoObjectStorageRegion
  26. multicloud.SNoLbRegion
  27. client *SKsyunClient
  28. Region string
  29. RegionName string
  30. }
  31. func (region *SRegion) GetId() string {
  32. return region.Region
  33. }
  34. func (region *SRegion) GetGlobalId() string {
  35. return fmt.Sprintf("%s/%s", api.CLOUD_PROVIDER_KSYUN, region.Region)
  36. }
  37. func (region *SRegion) GetProvider() string {
  38. return api.CLOUD_PROVIDER_KSYUN
  39. }
  40. func (region *SRegion) GetCloudEnv() string {
  41. return api.CLOUD_PROVIDER_KSYUN
  42. }
  43. func (region *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
  44. geo, ok := map[string]cloudprovider.SGeographicInfo{
  45. "cn-northwest-1": api.RegionQingYang,
  46. "ap-singapore-1": api.RegionSingapore,
  47. "cn-beijing-6": api.RegionBeijing,
  48. "cn-guangzhou-1": api.RegionGuangzhou,
  49. "cn-shanghai-2": api.RegionShanghai,
  50. }[region.Region]
  51. if ok {
  52. return geo
  53. }
  54. return cloudprovider.SGeographicInfo{}
  55. }
  56. func (region *SRegion) GetName() string {
  57. return region.RegionName
  58. }
  59. func (region *SRegion) GetI18n() cloudprovider.SModelI18nTable {
  60. table := cloudprovider.SModelI18nTable{}
  61. table["name"] = cloudprovider.NewSModelI18nEntry(region.GetName()).CN(region.GetName()).EN(region.Region)
  62. return table
  63. }
  64. func (region *SRegion) GetStatus() string {
  65. return api.CLOUD_REGION_STATUS_INSERVER
  66. }
  67. func (region *SRegion) GetClient() *SKsyunClient {
  68. return region.client
  69. }
  70. func (region *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
  71. return region.CreateEip(opts)
  72. }
  73. func (region *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
  74. return region.CreateSecurityGroup(conf)
  75. }
  76. func (region *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
  77. return region.CreateVpc(opts)
  78. }
  79. func (region *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
  80. vpcs, err := region.GetVpcs([]string{})
  81. if err != nil {
  82. return nil, errors.Wrap(err, "GetVpcs")
  83. }
  84. res := []cloudprovider.ICloudVpc{}
  85. for i := 0; i < len(vpcs); i++ {
  86. vpcs[i].region = region
  87. res = append(res, &vpcs[i])
  88. }
  89. return res, nil
  90. }
  91. func (region *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
  92. vpc, err := region.GetVpc(id)
  93. if err != nil {
  94. return nil, errors.Wrap(err, "region.GetVpc")
  95. }
  96. vpc.region = region
  97. return vpc, nil
  98. }
  99. func (region *SRegion) GetCapabilities() []string {
  100. return region.client.GetCapabilities()
  101. }
  102. func (region *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) {
  103. group, err := region.GetSecurityGroup(secgroupId)
  104. if err != nil {
  105. return nil, errors.Wrap(err, "region.GetSecurityGroup")
  106. }
  107. group.region = region
  108. return group, nil
  109. }
  110. func (region *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error) {
  111. eip, err := region.GetEip(eipId)
  112. if err != nil {
  113. return nil, errors.Wrap(err, "GetEipById")
  114. }
  115. eip.region = region
  116. return eip, nil
  117. }
  118. func (region *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  119. zones, err := region.GetIZones()
  120. if err != nil {
  121. return nil, errors.Wrap(err, "GetIZones")
  122. }
  123. hosts := []cloudprovider.ICloudHost{}
  124. for _, zone := range zones {
  125. zoneHosts, err := zone.GetIHosts()
  126. if err != nil {
  127. return nil, errors.Wrap(err, "zone.GetIHosts")
  128. }
  129. hosts = append(hosts, zoneHosts...)
  130. }
  131. return hosts, nil
  132. }
  133. func (region *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  134. hosts, err := region.GetIHosts()
  135. if err != nil {
  136. return nil, err
  137. }
  138. for _, host := range hosts {
  139. if host.GetId() == id {
  140. return host, nil
  141. }
  142. }
  143. return nil, errors.ErrNotFound
  144. }
  145. func (region *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  146. zones, err := region.GetIZones()
  147. if err != nil {
  148. return nil, errors.Wrap(err, "region.GetIZones")
  149. }
  150. for _, zone := range zones {
  151. storage, err := zone.GetIStorageById(id)
  152. if err == nil {
  153. return storage, nil
  154. } else if errors.Cause(err) != cloudprovider.ErrNotFound {
  155. return nil, errors.Wrap(err, "GetIStorageById")
  156. }
  157. }
  158. return nil, errors.ErrNotFound
  159. }
  160. func (region *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
  161. eips, err := region.GetEips([]string{})
  162. if err != nil {
  163. return nil, errors.Wrap(err, "GetEips")
  164. }
  165. res := []cloudprovider.ICloudEIP{}
  166. for i := 0; i < len(eips); i++ {
  167. eips[i].region = region
  168. res = append(res, &eips[i])
  169. }
  170. return res, nil
  171. }
  172. func (region *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
  173. zones, err := region.GetZones()
  174. if err != nil {
  175. return nil, errors.Wrap(err, "GetZones")
  176. }
  177. res := []cloudprovider.ICloudZone{}
  178. for i := 0; i < len(zones); i++ {
  179. zones[i].region = region
  180. res = append(res, &zones[i])
  181. }
  182. return res, nil
  183. }
  184. func (region *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
  185. izones, err := region.GetIZones()
  186. if err != nil {
  187. return nil, errors.Wrap(err, "GetIZones")
  188. }
  189. for _, izone := range izones {
  190. if izone.GetGlobalId() == id {
  191. return izone, nil
  192. }
  193. }
  194. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "zone id:%s", id)
  195. }
  196. func (region *SRegion) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  197. iStores := make([]cloudprovider.ICloudStorage, 0)
  198. izones, err := region.GetIZones()
  199. if err != nil {
  200. return nil, err
  201. }
  202. for _, izone := range izones {
  203. iZoneStores, err := izone.GetIStorages()
  204. if err != nil {
  205. return nil, err
  206. }
  207. iStores = append(iStores, iZoneStores...)
  208. }
  209. return iStores, nil
  210. }
  211. func (r *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  212. vms, err := r.GetInstances("", nil)
  213. if err != nil {
  214. return nil, err
  215. }
  216. ret := []cloudprovider.ICloudVM{}
  217. for i := range vms {
  218. vms[i].region = r
  219. ret = append(ret, &vms[i])
  220. }
  221. return ret, nil
  222. }
  223. func (r *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  224. vm, err := r.GetInstance(id)
  225. if err != nil {
  226. return nil, err
  227. }
  228. vm.region = r
  229. return vm, nil
  230. }
  231. func (region *SRegion) ecsRequest(action string, params map[string]interface{}) (jsonutils.JSONObject, error) {
  232. return region.client.ec2Request(region.Region, action, params)
  233. }
  234. func (region *SRegion) tagRequest(action string, params map[string]interface{}) (jsonutils.JSONObject, error) {
  235. return region.client.tagRequest(region.Region, action, params)
  236. }
  237. func (region *SRegion) eipRequest(action string, params map[string]interface{}) (jsonutils.JSONObject, error) {
  238. return region.client.eipRequest(region.Region, action, params)
  239. }
  240. func (region *SRegion) ebsRequest(action string, params map[string]interface{}) (jsonutils.JSONObject, error) {
  241. return region.client.ebsRequest(region.Region, action, params)
  242. }
  243. func (region *SRegion) vpcRequest(action string, params map[string]interface{}) (jsonutils.JSONObject, error) {
  244. return region.client.vpcRequest(region.Region, action, params)
  245. }
  246. func (region *SRegion) rdsRequest(action string, params map[string]interface{}) (jsonutils.JSONObject, error) {
  247. return region.client.rdsRequest(region.Region, action, params)
  248. }