ucloud.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 hostdrivers
  15. import (
  16. "context"
  17. "fmt"
  18. api "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/compute/models"
  20. "yunion.io/x/onecloud/pkg/httperrors"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. )
  23. type SUCloudHostDriver struct {
  24. SManagedVirtualizationHostDriver
  25. }
  26. func init() {
  27. driver := SUCloudHostDriver{}
  28. models.RegisterHostDriver(&driver)
  29. }
  30. func (self *SUCloudHostDriver) GetHostType() string {
  31. return api.HOST_TYPE_UCLOUD
  32. }
  33. func (self *SUCloudHostDriver) GetHypervisor() string {
  34. return api.HYPERVISOR_UCLOUD
  35. }
  36. func (self *SUCloudHostDriver) GetProvider() string {
  37. return api.CLOUD_PROVIDER_UCLOUD
  38. }
  39. func (self *SUCloudHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error {
  40. if storage.StorageType == api.STORAGE_UCLOUD_CLOUD_NORMAL {
  41. if sizeGb < 20 || sizeGb > 8000 {
  42. return fmt.Errorf("The %s disk size must be in the range of 20G ~ 8000GB", storage.StorageType)
  43. }
  44. } else if storage.StorageType == api.STORAGE_UCLOUD_CLOUD_SSD {
  45. if sizeGb < 20 || sizeGb > 4000 {
  46. return fmt.Errorf("The %s disk size must be in the range of 20G ~ 4000GB", storage.StorageType)
  47. }
  48. } else if storage.StorageType == api.STORAGE_UCLOUD_LOCAL_SSD {
  49. if sizeGb < 20 || sizeGb > 1000 {
  50. return fmt.Errorf("The %s disk size must be in the range of 20G ~ 1000GB", storage.StorageType)
  51. }
  52. return fmt.Errorf("Not support create/resize %s disk", storage.StorageType)
  53. } else if storage.StorageType == api.STORAGE_UCLOUD_LOCAL_NORMAL {
  54. if sizeGb < 20 || sizeGb > 2000 {
  55. return fmt.Errorf("The %s disk size must be in the range of 20G ~ 2000GB", storage.StorageType)
  56. }
  57. return fmt.Errorf("Not support create/resize %s disk", storage.StorageType)
  58. } else {
  59. return fmt.Errorf("Not support create %s disk", storage.StorageType)
  60. }
  61. return nil
  62. }
  63. func (self *SUCloudHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) {
  64. if len(guests) > 0 {
  65. return nil, httperrors.NewInputParameterError("Ucloud reset disk operation required disk not be attached")
  66. }
  67. if disk.DiskType != api.DISK_TYPE_DATA {
  68. return nil, httperrors.NewInputParameterError("Ucloud only support data disk reset operation")
  69. }
  70. return input, nil
  71. }