host.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ctyun
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  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. multicloud.SHostBase
  25. zone *SZone
  26. }
  27. func (self *SHost) GetId() string {
  28. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
  29. }
  30. func (self *SHost) GetName() string {
  31. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId())
  32. }
  33. func (self *SHost) GetGlobalId() string {
  34. return self.GetId()
  35. }
  36. func (self *SHost) GetStatus() string {
  37. return api.HOST_STATUS_RUNNING
  38. }
  39. func (self *SHost) IsEmulated() bool {
  40. return true
  41. }
  42. func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  43. vms, err := self.zone.region.GetInstances(self.zone.Name, nil)
  44. if err != nil {
  45. return nil, err
  46. }
  47. ret := []cloudprovider.ICloudVM{}
  48. for i := range vms {
  49. vms[i].host = self
  50. ret = append(ret, &vms[i])
  51. }
  52. return ret, nil
  53. }
  54. func (self *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  55. vms, err := self.zone.region.GetInstances(self.zone.Name, []string{id})
  56. if err != nil {
  57. return nil, err
  58. }
  59. for i := range vms {
  60. vms[i].host = self
  61. if vms[i].GetGlobalId() == id {
  62. return &vms[i], nil
  63. }
  64. }
  65. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  66. }
  67. func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  68. return self.zone.GetIStorages()
  69. }
  70. func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  71. return self.zone.GetIStorageById(id)
  72. }
  73. func (self *SHost) GetEnabled() bool {
  74. return true
  75. }
  76. func (self *SHost) GetHostStatus() string {
  77. return api.HOST_ONLINE
  78. }
  79. func (self *SHost) GetAccessIp() string {
  80. return ""
  81. }
  82. func (self *SHost) GetAccessMac() string {
  83. return ""
  84. }
  85. func (self *SHost) GetSysInfo() jsonutils.JSONObject {
  86. info := jsonutils.NewDict()
  87. info.Add(jsonutils.NewString(api.CLOUD_PROVIDER_CTYUN), "manufacture")
  88. return info
  89. }
  90. func (self *SHost) GetSN() string {
  91. return ""
  92. }
  93. func (self *SHost) GetCpuCount() int {
  94. return 0
  95. }
  96. func (self *SHost) GetNodeCount() int8 {
  97. return 0
  98. }
  99. func (self *SHost) GetCpuDesc() string {
  100. return ""
  101. }
  102. func (self *SHost) GetCpuMhz() int {
  103. return 0
  104. }
  105. func (self *SHost) GetMemSizeMB() int {
  106. return 0
  107. }
  108. func (self *SHost) GetStorageSizeMB() int64 {
  109. return 0
  110. }
  111. func (self *SHost) GetStorageType() string {
  112. return api.DISK_TYPE_HYBRID
  113. }
  114. func (self *SHost) GetHostType() string {
  115. return api.HOST_TYPE_CTYUN
  116. }
  117. func (self *SHost) GetIsMaintenance() bool {
  118. return false
  119. }
  120. func (self *SHost) GetVersion() string {
  121. return ""
  122. }
  123. func (self *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  124. vmId, err := self.zone.region.CreateInstance(self.zone.Name, opts)
  125. if err != nil {
  126. return nil, err
  127. }
  128. vm, err := self.zone.region.GetInstance(vmId)
  129. if err != nil {
  130. return nil, err
  131. }
  132. vm.host = self
  133. return vm, nil
  134. }
  135. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  136. wires, err := host.zone.GetIWires()
  137. if err != nil {
  138. return nil, errors.Wrap(err, "GetIWires")
  139. }
  140. return cloudprovider.GetHostNetifs(host, wires), nil
  141. }