host.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 google
  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 (host *SHost) GetId() string {
  28. return host.zone.GetGlobalId()
  29. }
  30. func (host *SHost) GetGlobalId() string {
  31. return host.GetId()
  32. }
  33. func (host *SHost) GetName() string {
  34. return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Name, host.zone.GetName())
  35. }
  36. func (host *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  37. return host.zone.GetIStorages()
  38. }
  39. func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  40. return host.zone.GetIStorageById(id)
  41. }
  42. func (host *SHost) IsEmulated() bool {
  43. return true
  44. }
  45. func (host *SHost) GetStatus() string {
  46. return api.HOST_STATUS_RUNNING
  47. }
  48. func (host *SHost) Refresh() error {
  49. return nil
  50. }
  51. func (host *SHost) GetHostStatus() string {
  52. return api.HOST_ONLINE
  53. }
  54. func (host *SHost) GetEnabled() bool {
  55. return true
  56. }
  57. func (host *SHost) GetAccessIp() string {
  58. return ""
  59. }
  60. func (host *SHost) GetAccessMac() string {
  61. return ""
  62. }
  63. func (host *SHost) GetSysInfo() jsonutils.JSONObject {
  64. info := jsonutils.NewDict()
  65. info.Add(jsonutils.NewString(CLOUD_PROVIDER_GOOGLE), "manufacture")
  66. return info
  67. }
  68. func (host *SHost) GetSN() string {
  69. return ""
  70. }
  71. func (host *SHost) GetCpuCount() int {
  72. return 0
  73. }
  74. func (host *SHost) GetNodeCount() int8 {
  75. return 0
  76. }
  77. func (host *SHost) GetCpuDesc() string {
  78. return ""
  79. }
  80. func (host *SHost) GetCpuMhz() int {
  81. return 0
  82. }
  83. func (host *SHost) GetMemSizeMB() int {
  84. return 0
  85. }
  86. func (host *SHost) GetStorageSizeMB() int64 {
  87. return 0
  88. }
  89. func (host *SHost) GetStorageType() string {
  90. return api.DISK_TYPE_HYBRID
  91. }
  92. func (host *SHost) GetHostType() string {
  93. return api.HOST_TYPE_GOOGLE
  94. }
  95. func (host *SHost) GetWire() *SWire {
  96. vpc := &SVpc{region: host.zone.region}
  97. return &SWire{vpc: vpc}
  98. }
  99. func (host *SHost) getIWires() ([]cloudprovider.ICloudWire, error) {
  100. ivpcs, err := host.zone.region.GetIVpcs()
  101. if err != nil {
  102. return nil, errors.Wrap(err, "region.GetIVpcs")
  103. }
  104. iwires := []cloudprovider.ICloudWire{}
  105. for i := range ivpcs {
  106. _iwires, err := ivpcs[i].GetIWires()
  107. if err != nil {
  108. return nil, errors.Wrap(err, "ivpcs[i].GetIWires")
  109. }
  110. iwires = append(iwires, _iwires...)
  111. }
  112. return iwires, nil
  113. }
  114. func (host *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  115. instances, err := host.zone.region.GetInstances(host.zone.Name, 0, "")
  116. if err != nil {
  117. return nil, err
  118. }
  119. iVMs := []cloudprovider.ICloudVM{}
  120. for i := range instances {
  121. instances[i].host = host
  122. iVMs = append(iVMs, &instances[i])
  123. }
  124. return iVMs, nil
  125. }
  126. func (host *SHost) GetIVMById(id string) (cloudprovider.ICloudVM, error) {
  127. instance, err := host.zone.region.GetInstance(id)
  128. if err != nil {
  129. return nil, err
  130. }
  131. if instance.Zone != host.zone.SelfLink {
  132. return nil, cloudprovider.ErrNotFound
  133. }
  134. instance.host = host
  135. return instance, nil
  136. }
  137. func (host *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  138. instance, err := host.zone.region._createVM(host.zone.Name, desc)
  139. if err != nil {
  140. return nil, err
  141. }
  142. instance.host = host
  143. return instance, nil
  144. }
  145. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  146. wires, err := host.getIWires()
  147. if err != nil {
  148. return nil, errors.Wrap(err, "getIWires")
  149. }
  150. return cloudprovider.GetHostNetifs(host, wires), nil
  151. }
  152. func (host *SHost) GetIsMaintenance() bool {
  153. return false
  154. }
  155. func (host *SHost) GetVersion() string {
  156. return GOOGLE_API_VERSION
  157. }