host.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 qcloud
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/billing"
  22. api "yunion.io/x/cloudmux/pkg/apis/compute"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. "yunion.io/x/cloudmux/pkg/multicloud"
  25. )
  26. type SHost struct {
  27. multicloud.SHostBase
  28. zone *SZone
  29. }
  30. func (self *SHost) GetId() string {
  31. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
  32. }
  33. func (self *SHost) GetName() string {
  34. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId())
  35. }
  36. func (self *SHost) GetGlobalId() string {
  37. return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
  38. }
  39. func (self *SHost) GetInstanceById(instanceId string) (*SInstance, error) {
  40. inst, err := self.zone.region.GetInstance(instanceId)
  41. if err != nil {
  42. return nil, err
  43. }
  44. inst.host = self
  45. return inst, nil
  46. }
  47. func (self *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  48. vmId, err := self._createVM(desc.Name, desc.Hostname, desc.ExternalImageId,
  49. desc.SysDisk, desc.Cpu, desc.MemoryMB,
  50. desc.InstanceType, desc.ExternalNetworkId,
  51. desc.IpAddr, desc.Description, desc.Password,
  52. desc.DataDisks, desc.PublicKey, desc.ExternalSecgroupIds,
  53. desc.UserData, desc.BillingCycle, desc.ProjectId, desc.PublicIpBw, desc.PublicIpChargeType, desc.Tags, desc.OsType)
  54. if err != nil {
  55. return nil, err
  56. }
  57. vm, err := self.GetInstanceById(vmId)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return vm, err
  62. }
  63. func (self *SHost) _createVM(name, hostname string, imgId string, sysDisk cloudprovider.SDiskInfo, cpu int, memMB int, instanceType string,
  64. networkId string, ipAddr string, desc string, passwd string,
  65. diskSizes []cloudprovider.SDiskInfo, publicKey string, secgroupIds []string, userData string, bc *billing.SBillingCycle, projectId string,
  66. publicIpBw int, publicIpChargeType cloudprovider.TElasticipChargeType,
  67. tags map[string]string, osType string,
  68. ) (string, error) {
  69. var err error
  70. keypair := ""
  71. if len(publicKey) > 0 {
  72. keypair, err = self.zone.region.syncKeypair(publicKey)
  73. if err != nil {
  74. return "", err
  75. }
  76. }
  77. img, err := self.zone.region.GetImage(imgId)
  78. if err != nil {
  79. return "", errors.Wrapf(err, "GetImage(%s)", imgId)
  80. }
  81. if img.ImageState != ImageStatusNormal && img.ImageState != ImageStatusUsing {
  82. return "", fmt.Errorf("image %s not ready status is %s", imgId, img.ImageState)
  83. }
  84. disks := make([]SDisk, len(diskSizes)+1)
  85. disks[0].DiskSize = img.ImageSize
  86. if sysDisk.SizeGB > 0 && sysDisk.SizeGB > img.ImageSize {
  87. disks[0].DiskSize = sysDisk.SizeGB
  88. }
  89. if disks[0].DiskSize < 50 {
  90. disks[0].DiskSize = 50
  91. }
  92. disks[0].DiskType = strings.ToUpper(sysDisk.StorageType)
  93. for i, dataDisk := range diskSizes {
  94. disks[i+1].DiskSize = dataDisk.SizeGB
  95. disks[i+1].DiskType = strings.ToUpper(dataDisk.StorageType)
  96. }
  97. log.Debugf("Try instancetype : %s", instanceType)
  98. vmId, err := self.zone.region.CreateInstance(name, hostname, imgId, instanceType, secgroupIds, self.zone.Zone, desc, passwd, disks, networkId, ipAddr, keypair, userData, bc, projectId, publicIpBw, publicIpChargeType, tags, osType)
  99. if err != nil {
  100. return "", errors.Wrapf(err, "Failed to create specification %s", instanceType)
  101. }
  102. return vmId, nil
  103. }
  104. func (self *SHost) Refresh() error {
  105. return nil
  106. }
  107. func (self *SHost) GetAccessIp() string {
  108. return ""
  109. }
  110. func (self *SHost) GetAccessMac() string {
  111. return ""
  112. }
  113. func (self *SHost) GetSN() string {
  114. return ""
  115. }
  116. func (self *SHost) GetCpuCount() int {
  117. return 0
  118. }
  119. func (self *SHost) GetNodeCount() int8 {
  120. return 0
  121. }
  122. func (self *SHost) GetCpuDesc() string {
  123. return ""
  124. }
  125. func (self *SHost) GetCpuMhz() int {
  126. return 0
  127. }
  128. func (self *SHost) GetMemSizeMB() int {
  129. return 0
  130. }
  131. func (self *SHost) GetStorageSizeMB() int64 {
  132. return 0
  133. }
  134. func (self *SHost) GetEnabled() bool {
  135. return true
  136. }
  137. func (self *SHost) GetStatus() string {
  138. return api.HOST_STATUS_RUNNING
  139. }
  140. func (self *SHost) GetHostStatus() string {
  141. return api.HOST_ONLINE
  142. }
  143. func (self *SHost) GetHostType() string {
  144. return api.HOST_TYPE_QCLOUD
  145. }
  146. func (self *SHost) GetStorageType() string {
  147. return api.DISK_TYPE_HYBRID
  148. }
  149. func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  150. return self.zone.GetIStorageById(id)
  151. }
  152. func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  153. return self.zone.GetIStorages()
  154. }
  155. func (self *SHost) GetIVMById(gid string) (cloudprovider.ICloudVM, error) {
  156. if len(gid) == 0 {
  157. return nil, cloudprovider.ErrNotFound
  158. }
  159. parts, _, err := self.zone.region.GetInstances(self.zone.Zone, []string{gid}, 0, 1)
  160. if err != nil {
  161. return nil, err
  162. }
  163. if len(parts) == 0 {
  164. return nil, cloudprovider.ErrNotFound
  165. }
  166. if len(parts) > 1 {
  167. return nil, cloudprovider.ErrDuplicateId
  168. }
  169. parts[0].host = self
  170. return &parts[0], nil
  171. }
  172. func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  173. vms := make([]SInstance, 0)
  174. for {
  175. parts, total, err := self.zone.region.GetInstances(self.zone.Zone, nil, len(vms), 50)
  176. if err != nil {
  177. return nil, err
  178. }
  179. vms = append(vms, parts...)
  180. if len(vms) >= total {
  181. break
  182. }
  183. }
  184. ivms := make([]cloudprovider.ICloudVM, len(vms))
  185. for i := 0; i < len(vms); i++ {
  186. vms[i].host = self
  187. ivms[i] = &vms[i]
  188. }
  189. return ivms, nil
  190. }
  191. func (self *SHost) GetSysInfo() jsonutils.JSONObject {
  192. info := jsonutils.NewDict()
  193. info.Add(jsonutils.NewString(CLOUD_PROVIDER_QCLOUD), "manufacture")
  194. return info
  195. }
  196. func (self *SHost) IsEmulated() bool {
  197. return true
  198. }
  199. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  200. wires, err := host.zone.GetIWires()
  201. if err != nil {
  202. return nil, errors.Wrap(err, "GetIWires")
  203. }
  204. return cloudprovider.GetHostNetifs(host, wires), nil
  205. }
  206. func (host *SHost) GetIsMaintenance() bool {
  207. return false
  208. }
  209. func (host *SHost) GetVersion() string {
  210. return QCLOUD_API_VERSION
  211. }