github.com/vmware/govmomi@v0.43.0/govc/vm/snapshot/create.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  )
    26  
    27  type create struct {
    28  	*flags.VirtualMachineFlag
    29  
    30  	description string
    31  	memory      bool
    32  	quiesce     bool
    33  }
    34  
    35  func init() {
    36  	cli.Register("snapshot.create", &create{})
    37  }
    38  
    39  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    41  	cmd.VirtualMachineFlag.Register(ctx, f)
    42  
    43  	f.BoolVar(&cmd.memory, "m", true, "Include memory state")
    44  	f.BoolVar(&cmd.quiesce, "q", false, "Quiesce guest file system")
    45  	f.StringVar(&cmd.description, "d", "", "Snapshot description")
    46  }
    47  
    48  func (cmd *create) Usage() string {
    49  	return "NAME"
    50  }
    51  
    52  func (cmd *create) Description() string {
    53  	return `Create snapshot of VM with NAME.
    54  
    55  Examples:
    56    govc snapshot.create -vm my-vm happy-vm-state`
    57  }
    58  
    59  func (cmd *create) 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 *create) 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  	task, err := vm.CreateSnapshot(ctx, f.Arg(0), cmd.description, cmd.memory, cmd.quiesce)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	return task.Wait(ctx)
    86  }