host.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package volcengine
  2. import (
  3. "fmt"
  4. api "yunion.io/x/cloudmux/pkg/apis/compute"
  5. "yunion.io/x/cloudmux/pkg/cloudprovider"
  6. "yunion.io/x/cloudmux/pkg/multicloud"
  7. "yunion.io/x/jsonutils"
  8. "yunion.io/x/pkg/errors"
  9. )
  10. type SHost struct {
  11. multicloud.SHostBase
  12. zone *SZone
  13. }
  14. func (host *SHost) GetIWires() ([]cloudprovider.ICloudWire, error) {
  15. return host.zone.GetIWires()
  16. }
  17. func (host *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  18. return host.zone.GetIStorages()
  19. }
  20. func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  21. return host.zone.GetIStorageById(id)
  22. }
  23. func (host *SHost) GetId() string {
  24. return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Id, host.zone.GetId())
  25. }
  26. func (host *SHost) GetName() string {
  27. return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Name, host.zone.GetId())
  28. }
  29. func (host *SHost) GetGlobalId() string {
  30. return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Id, host.zone.GetId())
  31. }
  32. func (host *SHost) IsEmulated() bool {
  33. return true
  34. }
  35. func (host *SHost) GetStatus() string {
  36. return api.HOST_STATUS_RUNNING
  37. }
  38. func (host *SHost) Refresh() error {
  39. return nil
  40. }
  41. func (host *SHost) GetHostStatus() string {
  42. return api.HOST_ONLINE
  43. }
  44. func (host *SHost) GetEnabled() bool {
  45. return true
  46. }
  47. func (host *SHost) GetAccessIp() string {
  48. return ""
  49. }
  50. func (host *SHost) GetAccessMac() string {
  51. return ""
  52. }
  53. func (host *SHost) GetSysInfo() jsonutils.JSONObject {
  54. info := jsonutils.NewDict()
  55. info.Add(jsonutils.NewString(CLOUD_PROVIDER_VOLCENGINE), "manufacture")
  56. return info
  57. }
  58. func (host *SHost) GetSN() string {
  59. return ""
  60. }
  61. func (host *SHost) GetCpuCount() int {
  62. return 0
  63. }
  64. func (host *SHost) GetNodeCount() int8 {
  65. return 0
  66. }
  67. func (host *SHost) GetCpuDesc() string {
  68. return ""
  69. }
  70. func (host *SHost) GetCpuMhz() int {
  71. return 0
  72. }
  73. func (host *SHost) GetMemSizeMB() int {
  74. return 0
  75. }
  76. func (host *SHost) GetStorageSizeMB() int64 {
  77. return 0
  78. }
  79. func (host *SHost) GetStorageType() string {
  80. return api.DISK_TYPE_HYBRID
  81. }
  82. func (host *SHost) GetHostType() string {
  83. return api.HOST_TYPE_VOLCENGINE
  84. }
  85. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  86. wires, err := host.zone.GetIWires()
  87. if err != nil {
  88. return nil, errors.Wrap(err, "GetIWires")
  89. }
  90. return cloudprovider.GetHostNetifs(host, wires), nil
  91. }
  92. func (host *SHost) GetIsMaintenance() bool {
  93. return false
  94. }
  95. func (host *SHost) GetVersion() string {
  96. return VOLCENGINE_API_VERSION
  97. }
  98. func (host *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  99. vms, err := host.zone.region.GetInstances(host.zone.ZoneId, nil)
  100. if err != nil {
  101. return nil, err
  102. }
  103. ivms := make([]cloudprovider.ICloudVM, len(vms))
  104. for i := 0; i < len(vms); i += 1 {
  105. vms[i].host = host
  106. ivms[i] = &vms[i]
  107. }
  108. return ivms, nil
  109. }
  110. func (host *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  111. vm, err := host.zone.region.GetInstance(id)
  112. if err != nil {
  113. return nil, err
  114. }
  115. vm.host = host
  116. return vm, nil
  117. }
  118. func (host *SHost) GetInstanceById(instanceId string) (*SInstance, error) {
  119. inst, err := host.zone.region.GetInstance(instanceId)
  120. if err != nil {
  121. return nil, err
  122. }
  123. inst.host = host
  124. return inst, nil
  125. }
  126. func (host *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  127. vmId, err := host._createVM(opts)
  128. if err != nil {
  129. return nil, err
  130. }
  131. vm, err := host.GetInstanceById(vmId)
  132. if err != nil {
  133. return nil, errors.Wrapf(err, "GetInstanceById")
  134. }
  135. return vm, nil
  136. }
  137. func (host *SHost) _createVM(opts *cloudprovider.SManagedVMCreateConfig) (string, error) {
  138. var err error
  139. if len(opts.PublicKey) > 0 {
  140. opts.KeypairName, err = host.zone.region.syncKeypair(opts.PublicKey)
  141. if err != nil {
  142. return "", err
  143. }
  144. }
  145. if len(opts.InstanceType) == 0 {
  146. return "", fmt.Errorf("instance type must be specified")
  147. }
  148. vmId, err := host.zone.region.CreateInstance(host.zone.ZoneId, opts)
  149. if err != nil {
  150. return "", errors.Wrapf(err, "Failed to create %s", opts.InstanceType)
  151. }
  152. return vmId, nil
  153. }