google.go 2.5 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. "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 SGoogleHostDriver struct {
  25. SManagedVirtualizationHostDriver
  26. }
  27. func init() {
  28. driver := SGoogleHostDriver{}
  29. models.RegisterHostDriver(&driver)
  30. }
  31. func (self *SGoogleHostDriver) GetHostType() string {
  32. return api.HOST_TYPE_GOOGLE
  33. }
  34. func (self *SGoogleHostDriver) GetHypervisor() string {
  35. return api.HYPERVISOR_GOOGLE
  36. }
  37. func (self *SGoogleHostDriver) GetProvider() string {
  38. return api.CLOUD_PROVIDER_GOOGLE
  39. }
  40. func (self *SGoogleHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb int) error {
  41. minGB := 10
  42. maxGB := -1
  43. switch storage.StorageType {
  44. case api.STORAGE_GOOGLE_PD_SSD, api.STORAGE_GOOGLE_PD_STANDARD, api.STORAGE_GOOGLE_PD_BALANCED:
  45. maxGB = 65536
  46. case api.STORAGE_GOOGLE_PD_EXTREME:
  47. minGB = 500
  48. maxGB = 65536
  49. case api.STORAGE_GOOGLE_HYPERDISK_BALANCED, api.STORAGE_GOOGLE_HYPERDISK_ML:
  50. minGB = 4
  51. maxGB = 65536
  52. case api.STORAGE_GOOGLE_HYPERDISK_THROUGHPUT:
  53. minGB = 2048
  54. maxGB = 32768
  55. case api.STORAGE_GOOGLE_HYPERDISK_EXTREME:
  56. minGB = 64
  57. maxGB = 65536
  58. default:
  59. return fmt.Errorf("Not support resize %s disk", storage.StorageType)
  60. }
  61. if sizeGb < minGB || sizeGb > maxGB {
  62. return fmt.Errorf("The %s disk size must be in the range of %dG ~ %dGB", storage.StorageType, minGB, maxGB)
  63. }
  64. return nil
  65. }
  66. func (self *SGoogleHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) {
  67. for _, guest := range guests {
  68. if !utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_READY}) {
  69. return nil, httperrors.NewBadGatewayError("%s reset disk required guest status is running or ready", self.GetHostType())
  70. }
  71. }
  72. return input, nil
  73. }