instance.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. "context"
  17. "fmt"
  18. "time"
  19. commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
  20. "github.com/jdcloud-api/jdcloud-sdk-go/services/vm/apis"
  21. "github.com/jdcloud-api/jdcloud-sdk-go/services/vm/client"
  22. "github.com/jdcloud-api/jdcloud-sdk-go/services/vm/models"
  23. "yunion.io/x/log"
  24. "yunion.io/x/pkg/errors"
  25. "yunion.io/x/pkg/util/billing"
  26. "yunion.io/x/pkg/util/sets"
  27. napis "yunion.io/x/cloudmux/pkg/apis"
  28. api "yunion.io/x/cloudmux/pkg/apis/compute"
  29. "yunion.io/x/cloudmux/pkg/cloudprovider"
  30. "yunion.io/x/cloudmux/pkg/multicloud"
  31. )
  32. type SInstance struct {
  33. multicloud.SInstanceBase
  34. multicloud.SBillingBase
  35. JdcloudTags
  36. host *SHost
  37. image *SImage
  38. models.Instance
  39. instanceType *SInstanceType
  40. }
  41. func (i *SInstance) GetBillingType() string {
  42. return billingType(&i.Charge)
  43. }
  44. func (i *SInstance) GetExpiredAt() time.Time {
  45. return expireAt(&i.Charge)
  46. }
  47. func (i *SInstance) GetId() string {
  48. return i.InstanceId
  49. }
  50. func (i *SInstance) GetName() string {
  51. return i.InstanceName
  52. }
  53. func (i *SInstance) GetHostname() string {
  54. return i.Hostname
  55. }
  56. func (i *SInstance) GetGlobalId() string {
  57. return i.GetId()
  58. }
  59. func (i *SInstance) GetStatus() string {
  60. switch i.Status {
  61. case "pending":
  62. return api.VM_DEPLOYING
  63. case "starting":
  64. return api.VM_STARTING
  65. case "running":
  66. return api.VM_RUNNING
  67. case "stopping":
  68. return api.VM_STOPPING
  69. case "stopped":
  70. return api.VM_READY
  71. case "reboot":
  72. return api.VM_STARTING
  73. case "rebuilding":
  74. return api.VM_REBUILD_ROOT
  75. case "resizing":
  76. return api.VM_CHANGE_FLAVOR
  77. case "deleting":
  78. return api.VM_DELETING
  79. default:
  80. return api.VM_UNKNOWN
  81. }
  82. }
  83. func (i *SInstance) Refresh() error {
  84. return nil
  85. }
  86. func (i *SInstance) IsEmulated() bool {
  87. return false
  88. }
  89. func (i *SInstance) GetBootOrder() string {
  90. return "dcn"
  91. }
  92. func (i *SInstance) GetVga() string {
  93. return "std"
  94. }
  95. func (i *SInstance) GetVdi() string {
  96. return "vnc"
  97. }
  98. func (i *SInstance) GetImage() (*SImage, error) {
  99. if i.image != nil {
  100. return i.image, nil
  101. }
  102. image, err := i.host.zone.region.GetImage(i.ImageId)
  103. if err != nil {
  104. return nil, err
  105. }
  106. i.image = image
  107. return i.image, nil
  108. }
  109. func (i *SInstance) GetOsType() cloudprovider.TOsType {
  110. image, err := i.GetImage()
  111. if err != nil {
  112. return cloudprovider.OsTypeLinux
  113. }
  114. return image.GetOsType()
  115. }
  116. func (i *SInstance) GetFullOsName() string {
  117. image, err := i.GetImage()
  118. if err != nil {
  119. return ""
  120. }
  121. return image.GetFullOsName()
  122. }
  123. func (i *SInstance) GetBios() cloudprovider.TBiosType {
  124. image, err := i.GetImage()
  125. if err != nil {
  126. return cloudprovider.BIOS
  127. }
  128. return image.GetBios()
  129. }
  130. func (i *SInstance) GetOsArch() string {
  131. image, err := i.GetImage()
  132. if err != nil {
  133. return napis.OS_ARCH_X86_64
  134. }
  135. return image.GetOsArch()
  136. }
  137. func (i *SInstance) GetOsDist() string {
  138. image, err := i.GetImage()
  139. if err != nil {
  140. return ""
  141. }
  142. return image.GetOsDist()
  143. }
  144. func (i *SInstance) GetOsVersion() string {
  145. image, err := i.GetImage()
  146. if err != nil {
  147. return ""
  148. }
  149. return image.GetOsVersion()
  150. }
  151. func (i *SInstance) GetOsLang() string {
  152. image, err := i.GetImage()
  153. if err != nil {
  154. return ""
  155. }
  156. return image.GetOsLang()
  157. }
  158. func (i *SInstance) GetMachine() string {
  159. return "pc"
  160. }
  161. func (i *SInstance) GetInstanceType() string {
  162. return i.InstanceType
  163. }
  164. func (in *SInstance) GetProjectId() string {
  165. return ""
  166. }
  167. func (in *SInstance) GetIHost() cloudprovider.ICloudHost {
  168. return in.host
  169. }
  170. func (in *SInstance) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
  171. disks := make([]cloudprovider.ICloudDisk, 0, len(in.DataDisks)+1)
  172. if in.SystemDisk.DiskCategory != "local" {
  173. disk := &SDisk{
  174. Disk: in.SystemDisk.CloudDisk,
  175. ImageId: in.ImageId,
  176. IsSystemDisk: true,
  177. }
  178. stroage, err := in.host.zone.getStorageByType(disk.DiskType)
  179. if err != nil {
  180. return nil, errors.Wrapf(err, "unable to find storage with type %s", disk.DiskType)
  181. }
  182. disk.storage = stroage
  183. disks = append(disks, disk)
  184. }
  185. for i := range in.DataDisks {
  186. disk := &SDisk{
  187. Disk: in.DataDisks[i].CloudDisk,
  188. }
  189. stroage, err := in.host.zone.getStorageByType(disk.DiskType)
  190. if err != nil {
  191. return nil, errors.Wrapf(err, "unable to find storage with type %s", disk.DiskType)
  192. }
  193. disk.storage = stroage
  194. disks = append(disks, disk)
  195. }
  196. return disks, nil
  197. }
  198. func (in *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) {
  199. nis := make([]cloudprovider.ICloudNic, 0, len(in.SecondaryNetworkInterfaces)+1)
  200. nis = append(nis, &SInstanceNic{
  201. instance: in,
  202. InstanceNetworkInterface: in.PrimaryNetworkInterface.NetworkInterface,
  203. })
  204. for i := range in.SecondaryNetworkInterfaces {
  205. nis = append(nis, &SInstanceNic{
  206. instance: in,
  207. InstanceNetworkInterface: in.SecondaryNetworkInterfaces[i].NetworkInterface,
  208. })
  209. }
  210. return nis, nil
  211. }
  212. func (in *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) {
  213. eip, err := in.host.zone.region.GetEIPById(in.ElasticIpId)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return eip, nil
  218. }
  219. func (in *SInstance) GetSecurityGroupIds() ([]string, error) {
  220. ids := sets.NewString()
  221. for _, sg := range in.PrimaryNetworkInterface.NetworkInterface.SecurityGroups {
  222. ids.Insert(sg.GroupId)
  223. }
  224. for i := range in.SecondaryNetworkInterfaces {
  225. for _, sg := range in.SecondaryNetworkInterfaces[i].NetworkInterface.SecurityGroups {
  226. ids.Insert(sg.GroupId)
  227. }
  228. }
  229. return ids.UnsortedList(), nil
  230. }
  231. func (in *SInstance) fetchInstanceType() error {
  232. its, err := in.host.zone.region.InstanceTypes(in.InstanceType)
  233. if err != nil {
  234. return err
  235. }
  236. if len(its) == 0 {
  237. return cloudprovider.ErrNotFound
  238. }
  239. for i := range its {
  240. if its[i].InstanceType.InstanceType == in.InstanceType {
  241. in.instanceType = &its[i]
  242. return nil
  243. }
  244. }
  245. return cloudprovider.ErrNotFound
  246. }
  247. func (in *SInstance) GetVcpuCount() int {
  248. if in.instanceType == nil {
  249. err := in.fetchInstanceType()
  250. if err != nil {
  251. log.Errorf("unable to get instance type %s: %v", in.InstanceType, err)
  252. return 0
  253. }
  254. }
  255. return in.instanceType.GetCpu()
  256. }
  257. func (in *SInstance) GetVmemSizeMB() int {
  258. if in.instanceType == nil {
  259. err := in.fetchInstanceType()
  260. if err != nil {
  261. log.Errorf("unable to get instance type %s", in.InstanceType)
  262. return 0
  263. }
  264. }
  265. return in.instanceType.GetMemoryMB()
  266. }
  267. func (in *SInstance) SetSecurityGroups(ids []string) error {
  268. return cloudprovider.ErrNotImplemented
  269. }
  270. func (in *SInstance) GetHypervisor() string {
  271. return api.HYPERVISOR_JDCLOUD
  272. }
  273. func (in *SInstance) StartVM(ctx context.Context) error {
  274. return cloudprovider.ErrNotImplemented
  275. }
  276. func (in *SInstance) StopVM(ctx context.Context, opts *cloudprovider.ServerStopOptions) error {
  277. return cloudprovider.ErrNotImplemented
  278. }
  279. func (in *SInstance) DeleteVM(ctx context.Context) error {
  280. return cloudprovider.ErrNotImplemented
  281. }
  282. func (in *SInstance) UpdateVM(ctx context.Context, input cloudprovider.SInstanceUpdateOptions) error {
  283. return cloudprovider.ErrNotSupported
  284. }
  285. func (in *SInstance) UpdateUserData(userData string) error {
  286. return cloudprovider.ErrNotSupported
  287. }
  288. func (self *SInstance) RebuildRoot(ctx context.Context, config *cloudprovider.SManagedVMRebuildRootConfig) (string, error) {
  289. return "", cloudprovider.ErrNotImplemented
  290. }
  291. func (self *SInstance) DeployVM(ctx context.Context, opts *cloudprovider.SInstanceDeployOptions) error {
  292. return cloudprovider.ErrNotImplemented
  293. }
  294. func (in *SInstance) ChangeConfig(ctx context.Context, config *cloudprovider.SManagedVMChangeConfig) error {
  295. return cloudprovider.ErrNotImplemented
  296. }
  297. func (in *SInstance) GetVNCInfo(input *cloudprovider.ServerVncInput) (*cloudprovider.ServerVncOutput, error) {
  298. region := in.host.zone.region
  299. req := apis.NewDescribeInstanceVncUrlRequest(region.ID, in.InstanceId)
  300. client := client.NewVmClient(region.getCredential())
  301. client.Logger = Logger{debug: region.client.debug}
  302. resp, err := client.DescribeInstanceVncUrl(req)
  303. if err != nil {
  304. return nil, err
  305. }
  306. ret := &cloudprovider.ServerVncOutput{
  307. Url: resp.Result.VncUrl,
  308. Protocol: "jdcloud",
  309. InstanceId: in.GetId(),
  310. Hypervisor: api.HYPERVISOR_JDCLOUD,
  311. }
  312. return ret, nil
  313. }
  314. func (in *SInstance) AttachDisk(ctx context.Context, diskId string) error {
  315. return cloudprovider.ErrNotImplemented
  316. }
  317. func (in *SInstance) DetachDisk(ctx context.Context, diskId string) error {
  318. return cloudprovider.ErrNotImplemented
  319. }
  320. func (self *SInstance) Renew(bc billing.SBillingCycle) error {
  321. return cloudprovider.ErrNotImplemented
  322. }
  323. func (self *SInstance) GetError() error {
  324. return nil
  325. }
  326. func (r *SRegion) GetInstances(zoneId string, ids []string, pangeNumber, pageSize int) ([]SInstance, int, error) {
  327. filters := []commodels.Filter{}
  328. if zoneId != "" {
  329. filters = append(filters, commodels.Filter{
  330. Name: "az",
  331. Values: []string{zoneId},
  332. })
  333. }
  334. if len(ids) > 0 {
  335. filters = append(filters, commodels.Filter{
  336. Name: "instanceId",
  337. Values: ids,
  338. })
  339. }
  340. req := apis.NewDescribeInstancesRequestWithAllParams(r.ID, &pangeNumber, &pageSize, filters)
  341. client := client.NewVmClient(r.getCredential())
  342. client.Logger = Logger{debug: r.client.debug}
  343. resp, err := client.DescribeInstances(req)
  344. if err != nil {
  345. return nil, 0, err
  346. }
  347. if resp.Error.Code >= 400 {
  348. return nil, 0, fmt.Errorf("%s", resp.Error.Message)
  349. }
  350. ins := make([]SInstance, len(resp.Result.Instances))
  351. for i := range ins {
  352. ins[i] = SInstance{
  353. Instance: resp.Result.Instances[i],
  354. }
  355. }
  356. return ins, resp.Result.TotalCount, nil
  357. }
  358. func (r *SRegion) GetInstanceById(id string) (*SInstance, error) {
  359. req := apis.NewDescribeInstanceRequest(r.ID, id)
  360. client := client.NewVmClient(r.getCredential())
  361. client.Logger = Logger{debug: r.client.debug}
  362. resp, err := client.DescribeInstance(req)
  363. if err != nil {
  364. return nil, err
  365. }
  366. return &SInstance{
  367. Instance: resp.Result.Instance,
  368. }, nil
  369. }
  370. func (r *SRegion) FillHost(instance *SInstance) {
  371. izone, err := r.getIZoneByRealId(instance.Az)
  372. if err != nil {
  373. log.Errorf("unable to find zone %s: %v", instance.Az, err)
  374. return
  375. }
  376. zone := izone.(*SZone)
  377. instance.host = &SHost{
  378. zone: zone,
  379. }
  380. }