aliyun.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "yunion.io/x/pkg/utils"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/compute/models"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. )
  24. type SAliyunHostDriver struct {
  25. SManagedVirtualizationHostDriver
  26. }
  27. func init() {
  28. driver := SAliyunHostDriver{}
  29. models.RegisterHostDriver(&driver)
  30. }
  31. func (self *SAliyunHostDriver) GetHostType() string {
  32. return api.HOST_TYPE_ALIYUN
  33. }
  34. func (self *SAliyunHostDriver) GetHypervisor() string {
  35. return api.HYPERVISOR_ALIYUN
  36. }
  37. func (self *SAliyunHostDriver) GetProvider() string {
  38. return api.CLOUD_PROVIDER_ALIYUN
  39. }
  40. func (self *SAliyunHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error {
  41. minGB := -1
  42. maxGB := -1
  43. switch storage.StorageType {
  44. case api.STORAGE_CLOUD_EFFICIENCY, api.STORAGE_CLOUD_SSD, api.STORAGE_CLOUD_ESSD:
  45. minGB = 20
  46. maxGB = 32768
  47. case api.STORAGE_CLOUD_ESSD_PL0:
  48. minGB = 1
  49. maxGB = 65536
  50. case api.STORAGE_CLOUD_ESSD_PL2:
  51. minGB = 461
  52. maxGB = 65536
  53. case api.STORAGE_CLOUD_ESSD_PL3:
  54. minGB = 1261
  55. maxGB = 65536
  56. case api.STORAGE_PUBLIC_CLOUD:
  57. minGB = 5
  58. maxGB = 2000
  59. case api.STORAGE_CLOUD_AUTO:
  60. minGB = 1
  61. maxGB = 65536
  62. case api.STORAGE_CLOUD_ESSD_ENTRY:
  63. minGB = 10
  64. maxGB = 32768
  65. default:
  66. minGB = 1
  67. maxGB = 65536
  68. }
  69. if sizeGb < minGB || sizeGb > maxGB {
  70. return fmt.Errorf("The %s disk size must be in the range of %dG ~ %dGB", storage.StorageType, minGB, maxGB)
  71. }
  72. return nil
  73. }
  74. func (self *SAliyunHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) {
  75. for _, guest := range guests {
  76. if !utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_READY}) {
  77. return nil, httperrors.NewBadGatewayError("Aliyun reset disk required guest status is running or ready")
  78. }
  79. }
  80. return input, nil
  81. }