region.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 proxmox
  15. import (
  16. "fmt"
  17. "net/url"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SRegion struct {
  25. multicloud.SRegion
  26. multicloud.SRegionEipBase
  27. multicloud.SNoObjectStorageRegion
  28. multicloud.SRegionLbBase
  29. client *SProxmoxClient
  30. }
  31. func (self *SRegion) GetClient() *SProxmoxClient {
  32. return self.client
  33. }
  34. func (self *SRegion) GetName() string {
  35. return self.client.cpcfg.Name
  36. }
  37. func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable {
  38. table := cloudprovider.SModelI18nTable{}
  39. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName())
  40. return table
  41. }
  42. func (self *SRegion) GetId() string {
  43. return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_PROXMOX, self.client.cpcfg.Id)
  44. }
  45. func (self *SRegion) GetGlobalId() string {
  46. return self.GetId()
  47. }
  48. func (self *SRegion) GetProvider() string {
  49. return CLOUD_PROVIDER_PROXMOX
  50. }
  51. func (self *SRegion) GetCloudEnv() string {
  52. return ""
  53. }
  54. func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
  55. return cloudprovider.SGeographicInfo{}
  56. }
  57. func (self *SRegion) GetStatus() string {
  58. return api.CLOUD_REGION_STATUS_INSERVER
  59. }
  60. func (self *SRegion) Refresh() error {
  61. return nil
  62. }
  63. func (self *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) {
  64. return nil, cloudprovider.ErrNotSupported
  65. }
  66. func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
  67. return nil, cloudprovider.ErrNotSupported
  68. }
  69. func (self *SRegion) CreateIVpc(conf *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
  70. return nil, cloudprovider.ErrNotSupported
  71. }
  72. func (self *SRegion) GetCapabilities() []string {
  73. return self.client.GetCapabilities()
  74. }
  75. func (self *SRegion) getVpc() *SVpc {
  76. return &SVpc{region: self}
  77. }
  78. func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
  79. vpc := self.getVpc()
  80. return []cloudprovider.ICloudVpc{vpc}, nil
  81. }
  82. func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
  83. vpcs, err := self.GetIVpcs()
  84. if err != nil {
  85. return nil, err
  86. }
  87. for i := range vpcs {
  88. if vpcs[i].GetGlobalId() == id {
  89. return vpcs[i], nil
  90. }
  91. }
  92. return nil, cloudprovider.ErrNotFound
  93. }
  94. func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
  95. zone, err := self.GetZone()
  96. if err != nil {
  97. return nil, errors.Wrapf(err, "GetZone()")
  98. }
  99. return zone, nil
  100. }
  101. func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
  102. zone, err := self.GetZone()
  103. if err != nil {
  104. return nil, err
  105. }
  106. ret := []cloudprovider.ICloudZone{}
  107. zone.region = self
  108. ret = append(ret, zone)
  109. return ret, nil
  110. }
  111. func (self *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  112. vm, err := self.GetInstance(id)
  113. if err != nil {
  114. return nil, err
  115. }
  116. return vm, nil
  117. }
  118. func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  119. host, err := self.GetHost(id)
  120. if err != nil {
  121. return nil, err
  122. }
  123. zone, err := self.GetZone()
  124. if err != nil {
  125. return nil, err
  126. }
  127. host.zone = zone
  128. return host, nil
  129. }
  130. func (self *SRegion) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) {
  131. storages, err := self.GetStorages()
  132. if err != nil {
  133. return nil, err
  134. }
  135. ret := []cloudprovider.ICloudStoragecache{}
  136. localMap := map[string]bool{}
  137. isShared := false
  138. for i := range storages {
  139. storage := &SStoragecache{
  140. region: self,
  141. Node: storages[i].Node,
  142. isShare: storages[i].Shared == 1,
  143. }
  144. if !isShared && storage.isShare {
  145. ret = append(ret, storage)
  146. isShared = true
  147. continue
  148. }
  149. _, ok := localMap[storages[i].Node]
  150. if !ok {
  151. localMap[storages[i].Node] = true
  152. ret = append(ret, storage)
  153. }
  154. }
  155. return ret, nil
  156. }
  157. func (self *SRegion) GetIStoragecacheById(id string) (cloudprovider.ICloudStoragecache, error) {
  158. caches, err := self.GetIStoragecaches()
  159. if err != nil {
  160. return nil, cloudprovider.ErrNotSupported
  161. }
  162. for i := range caches {
  163. if caches[i].GetGlobalId() == id {
  164. return caches[i], nil
  165. }
  166. }
  167. return nil, cloudprovider.ErrNotFound
  168. }
  169. func (self *SRegion) get(res string, params url.Values, retVal interface{}) error {
  170. return self.client.get(res, params, retVal)
  171. }
  172. func (self *SRegion) getAgent(res string, params url.Values, retVal interface{}) error {
  173. return self.client.getAgent(res, params, retVal)
  174. }
  175. func (self *SRegion) post(res string, params interface{}) (jsonutils.JSONObject, error) {
  176. return self.client.post(res, params)
  177. }
  178. func (self *SRegion) put(res string, params url.Values, body jsonutils.JSONObject) error {
  179. return self.client.put(res, params, body)
  180. }
  181. func (self *SRegion) del(res string, params url.Values, retVal interface{}) error {
  182. return self.client.del(res, params, retVal)
  183. }
  184. func (region *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  185. vms, err := region.GetInstances("")
  186. if err != nil {
  187. return nil, errors.Wrapf(err, "GetInstances")
  188. }
  189. ret := []cloudprovider.ICloudVM{}
  190. for i := range vms {
  191. ret = append(ret, &vms[i])
  192. }
  193. return ret, nil
  194. }