1/*
2Copyright (c) 2014-2015 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 vm
18
19import (
20	"context"
21	"flag"
22
23	"github.com/vmware/govmomi/govc/cli"
24	"github.com/vmware/govmomi/govc/flags"
25)
26
27type markasvm struct {
28	*flags.SearchFlag
29	*flags.ResourcePoolFlag
30	*flags.HostSystemFlag
31}
32
33func init() {
34	cli.Register("vm.markasvm", &markasvm{})
35}
36
37func (cmd *markasvm) Register(ctx context.Context, f *flag.FlagSet) {
38	cmd.SearchFlag, ctx = flags.NewSearchFlag(ctx, flags.SearchVirtualMachines)
39	cmd.SearchFlag.Register(ctx, f)
40	cmd.ResourcePoolFlag, ctx = flags.NewResourcePoolFlag(ctx)
41	cmd.ResourcePoolFlag.Register(ctx, f)
42	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
43	cmd.HostSystemFlag.Register(ctx, f)
44}
45
46func (cmd *markasvm) Process(ctx context.Context) error {
47	if err := cmd.SearchFlag.Process(ctx); err != nil {
48		return err
49	}
50	if err := cmd.ResourcePoolFlag.Process(ctx); err != nil {
51		return err
52	}
53	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
54		return err
55	}
56	return nil
57}
58
59func (cmd *markasvm) Usage() string {
60	return "VM..."
61}
62
63func (cmd *markasvm) Description() string {
64	return `Mark VM template as a virtual machine.
65
66Examples:
67  govc vm.markasvm $name -host host1
68  govc vm.markasvm $name -pool cluster1/Resources`
69}
70
71func (cmd *markasvm) Run(ctx context.Context, f *flag.FlagSet) error {
72	vms, err := cmd.VirtualMachines(f.Args())
73	if err != nil {
74		return err
75	}
76
77	pool, err := cmd.ResourcePoolIfSpecified()
78	if err != nil {
79		return err
80	}
81
82	host, err := cmd.HostSystemFlag.HostSystemIfSpecified()
83	if err != nil {
84		return err
85	}
86
87	if pool == nil {
88		if host == nil {
89			return flag.ErrHelp
90		}
91
92		pool, err = host.ResourcePool(ctx)
93		if err != nil {
94			return err
95		}
96	}
97
98	for _, vm := range vms {
99		err := vm.MarkAsVirtualMachine(ctx, *pool, host)
100		if err != nil {
101			return err
102		}
103	}
104
105	return nil
106}
107