region.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 nutanix
  15. import (
  16. "fmt"
  17. "io"
  18. "net/http"
  19. "net/url"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  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. type SRegion struct {
  27. multicloud.SRegion
  28. multicloud.SNoObjectStorageRegion
  29. multicloud.SNoLbRegion
  30. cli *SNutanixClient
  31. }
  32. func (self *SRegion) GetId() string {
  33. return self.cli.cpcfg.Id
  34. }
  35. func (self *SRegion) GetGlobalId() string {
  36. return fmt.Sprintf("%s/%s", api.CLOUD_PROVIDER_NUTANIX, self.cli.cpcfg.Id)
  37. }
  38. func (self *SRegion) GetName() string {
  39. return self.cli.cpcfg.Name
  40. }
  41. func (self *SRegion) GetI18n() cloudprovider.SModelI18nTable {
  42. table := cloudprovider.SModelI18nTable{}
  43. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName())
  44. return table
  45. }
  46. func (self *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
  47. return nil, cloudprovider.ErrNotSupported
  48. }
  49. func (self *SRegion) GetISecurityGroupById(secgroupId string) (cloudprovider.ICloudSecurityGroup, error) {
  50. return nil, cloudprovider.ErrNotSupported
  51. }
  52. func (self *SRegion) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
  53. return nil, cloudprovider.ErrNotSupported
  54. }
  55. func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
  56. return self.CreateVpc(opts)
  57. }
  58. func (self *SRegion) GetCapabilities() []string {
  59. return self.cli.GetCapabilities()
  60. }
  61. func (self *SRegion) GetCloudEnv() string {
  62. return ""
  63. }
  64. func (self *SRegion) GetProvider() string {
  65. return api.CLOUD_PROVIDER_NUTANIX
  66. }
  67. func (self *SRegion) GetStatus() string {
  68. return api.CLOUD_REGION_STATUS_INSERVER
  69. }
  70. func (self *SRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
  71. return cloudprovider.SGeographicInfo{}
  72. }
  73. func (self *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
  74. return nil, cloudprovider.ErrNotFound
  75. }
  76. func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
  77. return []cloudprovider.ICloudEIP{}, nil
  78. }
  79. func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
  80. vpc, err := self.GetVpc(id)
  81. if err != nil {
  82. return nil, errors.Wrapf(err, "GetVpc(%s)", id)
  83. }
  84. return vpc, nil
  85. }
  86. func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
  87. vpcs, err := self.GetVpcs()
  88. if err != nil {
  89. return nil, errors.Wrapf(err, "GetVpcs")
  90. }
  91. ret := []cloudprovider.ICloudVpc{}
  92. for i := range vpcs {
  93. vpcs[i].region = self
  94. ret = append(ret, &vpcs[i])
  95. }
  96. return ret, nil
  97. }
  98. func (self *SRegion) GetIZones() ([]cloudprovider.ICloudZone, error) {
  99. clusters, err := self.GetClusters()
  100. if err != nil {
  101. return nil, errors.Wrapf(err, "GetClusters")
  102. }
  103. ret := []cloudprovider.ICloudZone{}
  104. for i := range clusters {
  105. ret = append(ret, &SZone{
  106. SCluster: clusters[i],
  107. region: self,
  108. })
  109. }
  110. return ret, nil
  111. }
  112. func (self *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
  113. zones, err := self.GetIZones()
  114. if err != nil {
  115. return nil, errors.Wrapf(err, "GetIZones")
  116. }
  117. for i := range zones {
  118. if zones[i].GetGlobalId() == id {
  119. return zones[i], nil
  120. }
  121. }
  122. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  123. }
  124. func (self *SRegion) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  125. zones, err := self.GetIZones()
  126. if err != nil {
  127. return nil, errors.Wrapf(err, "GetIZones")
  128. }
  129. ret := []cloudprovider.ICloudHost{}
  130. for i := range zones {
  131. part, err := zones[i].GetIHosts()
  132. if err != nil {
  133. return nil, errors.Wrapf(err, "GetIHost")
  134. }
  135. ret = append(ret, part...)
  136. }
  137. return ret, nil
  138. }
  139. func (self *SRegion) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  140. vm, err := self.GetInstance(id)
  141. if err != nil {
  142. return nil, err
  143. }
  144. return vm, nil
  145. }
  146. func (self *SRegion) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  147. hosts, err := self.GetIHosts()
  148. if err != nil {
  149. return nil, errors.Wrapf(err, "GetIHosts")
  150. }
  151. for i := range hosts {
  152. if hosts[i].GetGlobalId() == id {
  153. return hosts[i], nil
  154. }
  155. }
  156. return nil, cloudprovider.ErrNotFound
  157. }
  158. func (self *SRegion) list(res string, params url.Values, retVal interface{}) (int, error) {
  159. return self.cli.list(res, params, retVal)
  160. }
  161. func (self *SRegion) get(res, id string, params url.Values, retVal interface{}) error {
  162. return self.cli.get(res, id, params, retVal)
  163. }
  164. func (self *SRegion) listAll(res string, params url.Values, retVal interface{}) error {
  165. return self.cli.listAll(res, params, retVal)
  166. }
  167. func (self *SRegion) post(res string, body jsonutils.JSONObject, retVal interface{}) error {
  168. return self.cli.post(res, body, retVal)
  169. }
  170. func (self *SRegion) delete(res string, id string) error {
  171. return self.cli.delete(res, id)
  172. }
  173. func (self *SRegion) update(res string, id string, body jsonutils.JSONObject, retVal interface{}) error {
  174. return self.cli.update(res, id, body, retVal)
  175. }
  176. func (self *SRegion) upload(res string, id string, header http.Header, body io.Reader) (jsonutils.JSONObject, error) {
  177. return self.cli.upload(res, id, header, body)
  178. }
  179. func (self *SRegion) getTask(id string) (*STask, error) {
  180. task := &STask{}
  181. return task, self.get("tasks", id, nil, task)
  182. }
  183. func (region *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  184. vms, err := region.GetInstances()
  185. if err != nil {
  186. return nil, errors.Wrapf(err, "GetInstances")
  187. }
  188. ret := []cloudprovider.ICloudVM{}
  189. for i := range vms {
  190. ret = append(ret, &vms[i])
  191. }
  192. return ret, nil
  193. }