yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/snapshot.go (about)

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