host.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 remotefile
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/utils"
  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 SHost struct {
  24. SResourceBase
  25. multicloud.SHostBase
  26. zone *SZone
  27. AccessIp string
  28. AccessMac string
  29. ZoneId string
  30. Enabled bool
  31. HostStatus string
  32. SN string
  33. CpuCount int
  34. NodeCount int8
  35. CpuDesc string
  36. CpuMbz int
  37. MemSizeMb int
  38. StorageSizeMb int64
  39. StorageType string
  40. AttachStorageTypes []string
  41. Wires []SWire
  42. }
  43. func (self *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  44. return nil, cloudprovider.ErrNotSupported
  45. }
  46. func (self *SHost) GetEnabled() bool {
  47. return self.Enabled
  48. }
  49. func (self *SHost) GetHostStatus() string {
  50. if len(self.HostStatus) == 0 {
  51. return api.HOST_ONLINE
  52. }
  53. return self.HostStatus
  54. }
  55. func (self *SHost) GetAccessIp() string {
  56. return self.AccessIp
  57. }
  58. func (self *SHost) GetAccessMac() string {
  59. return self.AccessMac
  60. }
  61. func (self *SHost) GetSysInfo() jsonutils.JSONObject {
  62. return jsonutils.NewDict()
  63. }
  64. func (self *SHost) GetSN() string {
  65. return self.SN
  66. }
  67. func (self *SHost) GetCpuCount() int {
  68. return self.CpuCount
  69. }
  70. func (self *SHost) GetNodeCount() int8 {
  71. return self.NodeCount
  72. }
  73. func (self *SHost) GetCpuDesc() string {
  74. return self.CpuDesc
  75. }
  76. func (self *SHost) GetCpuMhz() int {
  77. return self.CpuMbz
  78. }
  79. func (self *SHost) GetCpuCmtbound() float32 {
  80. return 1
  81. }
  82. func (self *SHost) GetMemSizeMB() int {
  83. return self.MemSizeMb
  84. }
  85. func (self *SHost) GetMemCmtbound() float32 {
  86. return 1
  87. }
  88. func (self *SHost) GetReservedMemoryMb() int {
  89. return 0
  90. }
  91. func (self *SHost) GetStorageSizeMB() int64 {
  92. return self.StorageSizeMb
  93. }
  94. func (self *SHost) GetStorageType() string {
  95. return self.StorageType
  96. }
  97. func (self *SHost) GetHostType() string {
  98. return api.HOST_TYPE_REMOTEFILE
  99. }
  100. func (self *SHost) GetIsMaintenance() bool {
  101. return false
  102. }
  103. func (self *SHost) GetVersion() string {
  104. return ""
  105. }
  106. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  107. wires, err := host.getIWires()
  108. if err != nil {
  109. return nil, errors.Wrap(err, "getIWires")
  110. }
  111. return cloudprovider.GetHostNetifs(host, wires), nil
  112. }
  113. func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  114. storages, err := self.zone.GetIStorages()
  115. if err != nil {
  116. return nil, err
  117. }
  118. ret := []cloudprovider.ICloudStorage{}
  119. for i := range storages {
  120. if utils.IsInStringArray(storages[i].GetStorageType(), self.AttachStorageTypes) {
  121. ret = append(ret, storages[i])
  122. }
  123. }
  124. return ret, nil
  125. }
  126. func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  127. storages, err := self.GetIStorages()
  128. if err != nil {
  129. return nil, err
  130. }
  131. for i := range storages {
  132. if storages[i].GetGlobalId() == id {
  133. return storages[i], nil
  134. }
  135. }
  136. return nil, cloudprovider.ErrNotFound
  137. }
  138. func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  139. vms, err := self.zone.region.client.GetInstances()
  140. if err != nil {
  141. return nil, err
  142. }
  143. ret := []cloudprovider.ICloudVM{}
  144. for i := range vms {
  145. if vms[i].HostId != self.GetId() {
  146. continue
  147. }
  148. vms[i].host = self
  149. ret = append(ret, &vms[i])
  150. }
  151. return ret, nil
  152. }
  153. func (self *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  154. vms, err := self.GetIVMs()
  155. if err != nil {
  156. return nil, err
  157. }
  158. for i := range vms {
  159. if vms[i].GetGlobalId() == id {
  160. return vms[i], nil
  161. }
  162. }
  163. return nil, cloudprovider.ErrNotFound
  164. }
  165. func (self *SHost) getIWires() ([]cloudprovider.ICloudWire, error) {
  166. wires := make([]cloudprovider.ICloudWire, len(self.Wires))
  167. for i := 0; i < len(self.Wires); i++ {
  168. wires[i] = &self.Wires[i]
  169. }
  170. return wires, nil
  171. }