github.com/vmware/govmomi@v0.43.0/govc/vm/snapshot/revert.go (about)

     1  /*
     2  Copyright (c) 2016 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package snapshot
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  	"github.com/vmware/govmomi/object"
    26  )
    27  
    28  type revert struct {
    29  	*flags.VirtualMachineFlag
    30  
    31  	suppressPowerOn bool
    32  }
    33  
    34  func init() {
    35  	cli.Register("snapshot.revert", &revert{})
    36  }
    37  
    38  func (cmd *revert) Register(ctx context.Context, f *flag.FlagSet) {
    39  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    40  	cmd.VirtualMachineFlag.Register(ctx, f)
    41  
    42  	f.BoolVar(&cmd.suppressPowerOn, "s", false, "Suppress power on")
    43  }
    44  
    45  func (cmd *revert) Usage() string {
    46  	return "[NAME]"
    47  }
    48  
    49  func (cmd *revert) Description() string {
    50  	return `Revert to snapshot of VM with given NAME.
    51  
    52  If NAME is not provided, revert to the current snapshot.
    53  Otherwise, NAME can be the snapshot name, tree path or moid.
    54  
    55  Examples:
    56    govc snapshot.revert -vm my-vm happy-vm-state`
    57  }
    58  
    59  func (cmd *revert) Process(ctx context.Context) error {
    60  	if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
    61  		return err
    62  	}
    63  	return nil
    64  }
    65  
    66  func (cmd *revert) Run(ctx context.Context, f *flag.FlagSet) error {
    67  	if f.NArg() > 1 {
    68  		return flag.ErrHelp
    69  	}
    70  
    71  	vm, err := cmd.VirtualMachine()
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	if vm == nil {
    77  		return flag.ErrHelp
    78  	}
    79  
    80  	var task *object.Task
    81  
    82  	if f.NArg() == 1 {
    83  		task, err = vm.RevertToSnapshot(ctx, f.Arg(0), cmd.suppressPowerOn)
    84  	} else {
    85  		task, err = vm.RevertToCurrentSnapshot(ctx, cmd.suppressPowerOn)
    86  	}
    87  
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	return task.Wait(ctx)
    93  }