host.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 jdcloud
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/sets"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SHost struct {
  25. multicloud.SHostBase
  26. zone *SZone
  27. }
  28. func (h *SHost) GetId() string {
  29. return fmt.Sprintf("%s-%s", h.zone.region.cpcfg.Id, h.zone.GetGlobalId())
  30. }
  31. func (h *SHost) GetName() string {
  32. return fmt.Sprintf("%s-%s", h.zone.region.Name, h.zone.GetName())
  33. }
  34. func (h *SHost) GetGlobalId() string {
  35. return h.GetId()
  36. }
  37. func (h *SHost) GetStatus() string {
  38. return api.HOST_STATUS_RUNNING
  39. }
  40. func (h *SHost) Refresh() error {
  41. return nil
  42. }
  43. func (h *SHost) IsEmulated() bool {
  44. return true
  45. }
  46. func (h *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  47. vms := make([]SInstance, 0)
  48. n := 1
  49. for {
  50. parts, total, err := h.zone.region.GetInstances(h.zone.ID, nil, n, 100)
  51. if err != nil {
  52. return nil, err
  53. }
  54. vms = append(vms, parts...)
  55. if len(vms) >= total {
  56. break
  57. }
  58. n++
  59. }
  60. // fill instanceType
  61. instanceTypes := sets.NewString()
  62. for i := range vms {
  63. instanceTypes.Insert(vms[i].InstanceType)
  64. }
  65. its, err := h.zone.region.InstanceTypes(instanceTypes.UnsortedList()...)
  66. if err != nil {
  67. return nil, errors.Wrap(err, "unable to fetch instanceTypes")
  68. }
  69. itMap := make(map[string]*SInstanceType, len(its))
  70. for i := range its {
  71. itMap[its[i].InstanceType.InstanceType] = &its[i]
  72. }
  73. ivms := make([]cloudprovider.ICloudVM, len(vms))
  74. for i := range vms {
  75. vms[i].host = h
  76. vms[i].instanceType = itMap[vms[i].InstanceType]
  77. ivms[i] = &vms[i]
  78. }
  79. return ivms, nil
  80. }
  81. func (h *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  82. in, err := h.zone.region.GetInstanceById(id)
  83. if err != nil {
  84. return nil, err
  85. }
  86. in.host = h
  87. return in, nil
  88. }
  89. func (h *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  90. return h.zone.GetIStorages()
  91. }
  92. func (h *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  93. return h.zone.GetIStorageById(id)
  94. }
  95. func (h *SHost) GetEnabled() bool {
  96. return true
  97. }
  98. func (h *SHost) GetHostStatus() string {
  99. return api.HOST_ONLINE
  100. }
  101. func (h *SHost) GetAccessIp() string {
  102. return ""
  103. }
  104. func (h *SHost) GetAccessMac() string {
  105. return ""
  106. }
  107. func (h *SHost) GetSysInfo() jsonutils.JSONObject {
  108. info := jsonutils.NewDict()
  109. info.Add(jsonutils.NewString(api.CLOUD_PROVIDER_JDCLOUD), "manufacture")
  110. return info
  111. }
  112. func (h *SHost) GetSN() string {
  113. return ""
  114. }
  115. func (h *SHost) GetCpuCount() int {
  116. return 0
  117. }
  118. func (h *SHost) GetNodeCount() int8 {
  119. return 0
  120. }
  121. func (h *SHost) GetCpuDesc() string {
  122. return ""
  123. }
  124. func (h *SHost) GetCpuMhz() int {
  125. return 0
  126. }
  127. func (h *SHost) GetMemSizeMB() int {
  128. return 0
  129. }
  130. func (h *SHost) GetStorageSizeMB() int64 {
  131. return 0
  132. }
  133. func (h *SHost) GetStorageType() string {
  134. return api.DISK_TYPE_HYBRID
  135. }
  136. func (h *SHost) GetHostType() string {
  137. return api.HOST_TYPE_JDCLOUD
  138. }
  139. func (h *SHost) GetIsMaintenance() bool {
  140. return false
  141. }
  142. func (h *SHost) GetVersion() string {
  143. return ""
  144. }
  145. func (h *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  146. return nil, cloudprovider.ErrNotImplemented
  147. }
  148. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  149. return nil, nil
  150. }