esxi.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 regiondrivers
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. type SEsxiRegionDriver struct {
  27. SManagedVirtualizationRegionDriver
  28. }
  29. func init() {
  30. driver := SEsxiRegionDriver{}
  31. models.RegisterRegionDriver(&driver)
  32. }
  33. func (self *SEsxiRegionDriver) GetProvider() string {
  34. return api.CLOUD_PROVIDER_VMWARE
  35. }
  36. func (self *SEsxiRegionDriver) ValidateCreateSnapshotData(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, storage *models.SStorage, input *api.SnapshotCreateInput) error {
  37. return fmt.Errorf("%s does not support creating snapshot", self.GetProvider())
  38. }
  39. func (self *SEsxiRegionDriver) RequestCreateInstanceSnapshot(ctx context.Context, guest *models.SGuest, isp *models.SInstanceSnapshot, task taskman.ITask, params *jsonutils.JSONDict) error {
  40. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  41. ivm, err := guest.GetIVM(ctx)
  42. if err != nil {
  43. return nil, errors.Wrap(err, "unable to GetIVM")
  44. }
  45. cloudSP, err := ivm.CreateInstanceSnapshot(ctx, isp.GetName(), isp.Description)
  46. if err != nil {
  47. return nil, errors.Wrap(err, "unable to CreateInstanceSnapshot")
  48. }
  49. _, err = db.Update(isp, func() error {
  50. isp.SetExternalId(cloudSP.GetGlobalId())
  51. return nil
  52. })
  53. return nil, err
  54. })
  55. return nil
  56. }
  57. func (self *SEsxiRegionDriver) RequestDeleteInstanceSnapshot(ctx context.Context, isp *models.SInstanceSnapshot, task taskman.ITask) error {
  58. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  59. guest, err := isp.GetGuest()
  60. if err != nil {
  61. return nil, errors.Wrap(err, "GetGuest")
  62. }
  63. ivm, err := guest.GetIVM(ctx)
  64. if err != nil {
  65. return nil, errors.Wrap(err, "unable to GetIVM")
  66. }
  67. id := isp.GetExternalId()
  68. if len(id) == 0 {
  69. return nil, nil
  70. }
  71. cloudSP, err := ivm.GetInstanceSnapshot(id)
  72. if err != nil {
  73. return nil, errors.Wrap(err, "unable to GetInstanceSnapshot")
  74. }
  75. err = cloudSP.Delete()
  76. if err != nil {
  77. return nil, errors.Wrap(err, "unable to delete cloud instance snapshot")
  78. }
  79. return nil, nil
  80. })
  81. return nil
  82. }
  83. func (self *SEsxiRegionDriver) RequestResetToInstanceSnapshot(ctx context.Context, guest *models.SGuest, isp *models.SInstanceSnapshot, task taskman.ITask, params *jsonutils.JSONDict) error {
  84. taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
  85. ivm, err := guest.GetIVM(ctx)
  86. if err != nil {
  87. return nil, errors.Wrap(err, "unable to GetIVM")
  88. }
  89. err = ivm.ResetToInstanceSnapshot(ctx, isp.GetExternalId())
  90. if err != nil {
  91. return nil, errors.Wrap(err, "unable to ResetToInstanceSnapshot")
  92. }
  93. return nil, nil
  94. })
  95. return nil
  96. }