fakeregion.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 esxi
  15. import (
  16. "yunion.io/x/pkg/errors"
  17. "yunion.io/x/pkg/utils"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. )
  21. func (cli *SESXiClient) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  22. return nil, cloudprovider.ErrNotSupported
  23. }
  24. func (cli *SESXiClient) GetISnapshotById(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
  25. return nil, cloudprovider.ErrNotSupported
  26. }
  27. func (cli *SESXiClient) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  28. dcs, err := cli.GetDatacenters()
  29. if err != nil {
  30. return nil, err
  31. }
  32. ihosts := make([]cloudprovider.ICloudHost, 0)
  33. for i := 0; i < len(dcs); i += 1 {
  34. dcIHosts, err := dcs[i].GetIHosts()
  35. if err != nil {
  36. return nil, err
  37. }
  38. ihosts = append(ihosts, dcIHosts...)
  39. }
  40. return ihosts, nil
  41. }
  42. func (cli *SESXiClient) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  43. hosts, err := cli.GetIHosts()
  44. if err != nil {
  45. return nil, err
  46. }
  47. ret := []cloudprovider.ICloudVM{}
  48. for _, host := range hosts {
  49. vm, err := host.GetIVMs()
  50. if err != nil {
  51. return nil, err
  52. }
  53. ret = append(ret, vm...)
  54. }
  55. return ret, nil
  56. }
  57. func (cli *SESXiClient) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  58. hosts, err := cli.GetIHosts()
  59. if err != nil {
  60. return nil, err
  61. }
  62. for _, host := range hosts {
  63. vm, err := host.GetIVMById(id)
  64. if errors.Cause(err) != cloudprovider.ErrNotFound {
  65. return vm, err
  66. }
  67. }
  68. return nil, cloudprovider.ErrNotFound
  69. }
  70. func (self *SESXiClient) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
  71. storages, err := self.GetIStorages()
  72. if err != nil {
  73. return nil, err
  74. }
  75. for _, storage := range storages {
  76. disk, err := storage.GetIDiskById(id)
  77. if errors.Cause(err) != cloudprovider.ErrNotFound {
  78. return disk, err
  79. }
  80. }
  81. return nil, cloudprovider.ErrNotFound
  82. }
  83. func (cli *SESXiClient) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  84. return cli.FindHostByIp(id)
  85. }
  86. func (cli *SESXiClient) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  87. dcs, err := cli.GetDatacenters()
  88. if err != nil {
  89. return nil, err
  90. }
  91. iStorages := make([]cloudprovider.ICloudStorage, 0)
  92. for i := 0; i < len(dcs); i += 1 {
  93. dcIStorages, err := dcs[i].GetIStorages()
  94. if err != nil {
  95. return nil, err
  96. }
  97. iStorages = append(iStorages, dcIStorages...)
  98. }
  99. return iStorages, nil
  100. }
  101. func (cli *SESXiClient) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  102. iStorages, err := cli.GetIStorages()
  103. if err != nil {
  104. return nil, err
  105. }
  106. for i := 0; i < len(iStorages); i += 1 {
  107. if iStorages[i].GetGlobalId() == id {
  108. return iStorages[i], nil
  109. }
  110. }
  111. return nil, cloudprovider.ErrNotFound
  112. }
  113. func (cli *SESXiClient) GetProvider() string {
  114. return api.CLOUD_PROVIDER_VMWARE
  115. }
  116. func (cli *SESXiClient) GetIStoragecaches() ([]cloudprovider.ICloudStoragecache, error) {
  117. storages, err := cli.GetIStorages()
  118. if err != nil {
  119. return nil, err
  120. }
  121. caches := make([]cloudprovider.ICloudStoragecache, 0)
  122. cacheIds := make([]string, 0)
  123. for i := range storages {
  124. iCache := storages[i].GetIStoragecache()
  125. if !utils.IsInStringArray(iCache.GetGlobalId(), cacheIds) {
  126. caches = append(caches, iCache)
  127. cacheIds = append(cacheIds, iCache.GetGlobalId())
  128. }
  129. }
  130. return caches, nil
  131. }
  132. func (cli *SESXiClient) GetIStoragecacheById(idstr string) (cloudprovider.ICloudStoragecache, error) {
  133. caches, err := cli.GetIStoragecaches()
  134. if err != nil {
  135. return nil, err
  136. }
  137. for i := range caches {
  138. if caches[i].GetGlobalId() == idstr {
  139. return caches[i], nil
  140. }
  141. }
  142. return nil, cloudprovider.ErrNotFound
  143. }
  144. func (cli *SESXiClient) GetISkus() ([]cloudprovider.ICloudSku, error) {
  145. return nil, cloudprovider.ErrNotSupported
  146. }
  147. func (cli *SESXiClient) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
  148. return []cloudprovider.ICloudVpc{cli.fakeVpc}, nil
  149. }
  150. func (client *SESXiClient) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
  151. return nil, errors.ErrNotSupported
  152. }
  153. func (client *SESXiClient) GetId() string {
  154. return client.GetUUID()
  155. }
  156. func (client *SESXiClient) GetName() string {
  157. return client.cpcfg.Name
  158. }
  159. func (client *SESXiClient) GetGlobalId() string {
  160. return client.GetUUID()
  161. }
  162. func (client *SESXiClient) GetStatus() string {
  163. return "available"
  164. }
  165. func (client *SESXiClient) GetCloudEnv() string {
  166. return ""
  167. }
  168. func (client *SESXiClient) Refresh() error {
  169. return nil
  170. }
  171. func (client *SESXiClient) IsEmulated() bool {
  172. return true
  173. }
  174. func (client *SESXiClient) GetSysTags() map[string]string {
  175. return nil
  176. }
  177. func (client *SESXiClient) GetTags() (map[string]string, error) {
  178. return nil, errors.Wrap(errors.ErrNotImplemented, "GetTags")
  179. }
  180. func (client *SESXiClient) SetTags(tags map[string]string, replace bool) error {
  181. return errors.ErrNotImplemented
  182. }
  183. func (client *SESXiClient) GetGeographicInfo() cloudprovider.SGeographicInfo {
  184. return cloudprovider.SGeographicInfo{}
  185. }
  186. func (client *SESXiClient) GetIZones() ([]cloudprovider.ICloudZone, error) {
  187. return nil, errors.ErrNotSupported
  188. }
  189. func (client *SESXiClient) GetIZoneById(id string) (cloudprovider.ICloudZone, error) {
  190. return nil, errors.ErrNotSupported
  191. }
  192. func (client *SESXiClient) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
  193. return nil, errors.ErrNotSupported
  194. }
  195. func (client *SESXiClient) GetISecurityGroupById(id string) (cloudprovider.ICloudSecurityGroup, error) {
  196. return nil, errors.ErrNotSupported
  197. }
  198. func (client *SESXiClient) CreateISecurityGroup(conf *cloudprovider.SecurityGroupCreateInput) (cloudprovider.ICloudSecurityGroup, error) {
  199. return nil, errors.ErrNotSupported
  200. }