snapshot.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 esxi
  15. import (
  16. "fmt"
  17. "github.com/vmware/govmomi/object"
  18. "github.com/vmware/govmomi/vim25/methods"
  19. "github.com/vmware/govmomi/vim25/types"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SVirtualMachineSnapshot struct {
  25. multicloud.SResourceBase
  26. multicloud.STagBase
  27. snapshotTree types.VirtualMachineSnapshotTree
  28. vm *SVirtualMachine
  29. }
  30. func NewSnapshot(st types.VirtualMachineSnapshotTree) *SVirtualMachineSnapshot {
  31. return &SVirtualMachineSnapshot{
  32. snapshotTree: st,
  33. }
  34. }
  35. func (s *SVirtualMachineSnapshot) GetId() string {
  36. return moRefId(s.snapshotTree.Snapshot)
  37. }
  38. func (s *SVirtualMachineSnapshot) GetName() string {
  39. return s.snapshotTree.Name
  40. }
  41. func (s *SVirtualMachineSnapshot) GetDescription() string {
  42. return s.snapshotTree.Description
  43. }
  44. func (s *SVirtualMachineSnapshot) GetGlobalId() string {
  45. return fmt.Sprintf("%s-%d", s.vm.GetGlobalId(), s.snapshotTree.Id)
  46. }
  47. func (s *SVirtualMachineSnapshot) GetStatus() string {
  48. return api.INSTANCE_SNAPSHOT_READY
  49. }
  50. func (s *SVirtualMachineSnapshot) Refresh() error {
  51. return nil
  52. }
  53. func (s *SVirtualMachineSnapshot) IsEmulated() bool {
  54. return false
  55. }
  56. func (s *SVirtualMachineSnapshot) GetProjectId() string {
  57. return s.vm.GetProjectId()
  58. }
  59. func (s *SVirtualMachineSnapshot) Delete() error {
  60. cTrue := true
  61. req := types.RemoveSnapshot_Task{
  62. This: s.snapshotTree.Snapshot.Reference(),
  63. RemoveChildren: false,
  64. Consolidate: &cTrue,
  65. }
  66. res, err := methods.RemoveSnapshot_Task(s.vm.manager.context, s.vm.manager.client.Client, &req)
  67. if err != nil {
  68. return errors.Wrap(err, "RemoveSnapshot_Task")
  69. }
  70. return object.NewTask(s.vm.manager.client.Client, res.Returnval).Wait(s.vm.manager.context)
  71. }