1/*
2Copyright (c) 2016 VMware, Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package snapshot
18
19import (
20	"context"
21	"flag"
22
23	"github.com/vmware/govmomi/govc/cli"
24	"github.com/vmware/govmomi/govc/flags"
25)
26
27type create struct {
28	*flags.VirtualMachineFlag
29
30	description string
31	memory      bool
32	quiesce     bool
33}
34
35func init() {
36	cli.Register("snapshot.create", &create{})
37}
38
39func (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
48func (cmd *create) Usage() string {
49	return "NAME"
50}
51
52func (cmd *create) Description() string {
53	return `Create snapshot of VM with NAME.
54
55Examples:
56  govc snapshot.create -vm my-vm happy-vm-state`
57}
58
59func (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
66func (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}
87