ucloud.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 guestdrivers
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/rbacscope"
  22. "yunion.io/x/pkg/utils"
  23. api "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/httperrors"
  27. "yunion.io/x/onecloud/pkg/mcclient"
  28. )
  29. type SUCloudGuestDriver struct {
  30. SManagedVirtualizedGuestDriver
  31. }
  32. func (self *SUCloudGuestDriver) GetHypervisor() string {
  33. return api.HYPERVISOR_UCLOUD
  34. }
  35. func (self *SUCloudGuestDriver) GetProvider() string {
  36. return api.CLOUD_PROVIDER_UCLOUD
  37. }
  38. func (self *SUCloudGuestDriver) GetComputeQuotaKeys(scope rbacscope.TRbacScope, ownerId mcclient.IIdentityProvider, brand string) models.SComputeResourceKeys {
  39. keys := models.SComputeResourceKeys{}
  40. keys.SBaseProjectQuotaKeys = quotas.OwnerIdProjectQuotaKeys(scope, ownerId)
  41. keys.CloudEnv = api.CLOUD_ENV_PUBLIC_CLOUD
  42. keys.Provider = api.CLOUD_PROVIDER_UCLOUD
  43. keys.Brand = api.CLOUD_PROVIDER_UCLOUD
  44. keys.Hypervisor = api.HYPERVISOR_UCLOUD
  45. return keys
  46. }
  47. func (self *SUCloudGuestDriver) GetDefaultSysDiskBackend() string {
  48. return api.STORAGE_UCLOUD_CLOUD_SSD
  49. }
  50. func (self *SUCloudGuestDriver) GetDeployStatus() ([]string, error) {
  51. return []string{api.VM_READY}, nil
  52. }
  53. func (self *SUCloudGuestDriver) GetMinimalSysDiskSizeGb() int {
  54. return 10
  55. }
  56. func (self *SUCloudGuestDriver) GetGuestInitialStateAfterCreate() string {
  57. return api.VM_RUNNING
  58. }
  59. func (self *SUCloudGuestDriver) GetDetachDiskStatus() ([]string, error) {
  60. return []string{api.VM_READY, api.VM_RUNNING}, nil
  61. }
  62. func (self *SUCloudGuestDriver) GetAttachDiskStatus() ([]string, error) {
  63. return []string{api.VM_READY, api.VM_RUNNING}, nil
  64. }
  65. func (self *SUCloudGuestDriver) GetChangeInstanceTypeStatus() ([]string, error) {
  66. return []string{api.VM_READY}, nil
  67. }
  68. func (self *SUCloudGuestDriver) GetRebuildRootStatus() ([]string, error) {
  69. return []string{api.VM_READY, api.VM_RUNNING}, nil
  70. }
  71. func (self *SUCloudGuestDriver) GetGuestInitialStateAfterRebuild() string {
  72. return api.VM_RUNNING
  73. }
  74. func (self *SUCloudGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error {
  75. if !utils.IsInStringArray(guest.Status, []string{api.VM_READY, api.VM_START_RESIZE_DISK, api.VM_RESIZE_DISK}) {
  76. return fmt.Errorf("Cannot resize disk when guest in status %s", guest.Status)
  77. }
  78. if !utils.IsInStringArray(storage.StorageType, []string{api.STORAGE_UCLOUD_CLOUD_SSD, api.STORAGE_UCLOUD_CLOUD_NORMAL}) {
  79. return fmt.Errorf("Cannot resize disk with unsupported volumes type %s", storage.StorageType)
  80. }
  81. return nil
  82. }
  83. func (self *SUCloudGuestDriver) GetInstanceCapability() cloudprovider.SInstanceCapability {
  84. return cloudprovider.SInstanceCapability{
  85. Hypervisor: self.GetHypervisor(),
  86. Provider: self.GetProvider(),
  87. DefaultAccount: cloudprovider.SDefaultAccount{
  88. Linux: cloudprovider.SOsDefaultAccount{
  89. DefaultAccount: api.VM_DEFAULT_LINUX_LOGIN_USER,
  90. },
  91. Windows: cloudprovider.SOsDefaultAccount{
  92. DefaultAccount: api.VM_DEFAULT_WINDOWS_LOGIN_USER,
  93. },
  94. },
  95. }
  96. }
  97. func init() {
  98. driver := SUCloudGuestDriver{}
  99. models.RegisterGuestDriver(&driver)
  100. }
  101. func (ucloud *SUCloudGuestDriver) ValidateGuestChangeConfigInput(ctx context.Context, guest *models.SGuest, input api.ServerChangeConfigInput) (*api.ServerChangeConfigSettings, error) {
  102. confs, err := ucloud.SBaseGuestDriver.ValidateGuestChangeConfigInput(ctx, guest, input)
  103. if err != nil {
  104. return nil, errors.Wrap(err, "SBaseGuestDriver.ValidateGuestChangeConfigInput")
  105. }
  106. if len(input.InstanceType) > 0 {
  107. if !strings.HasPrefix(guest.InstanceType, confs.InstanceTypeFamily) {
  108. return nil, httperrors.NewInputParameterError("Cannot change config with different instance family")
  109. }
  110. }
  111. return confs, nil
  112. }