1/*
2Copyright (c) 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 pool
18
19import (
20	"context"
21	"flag"
22
23	"github.com/vmware/govmomi/find"
24	"github.com/vmware/govmomi/govc/cli"
25	"github.com/vmware/govmomi/govc/flags"
26)
27
28type destroy struct {
29	*flags.DatacenterFlag
30
31	children bool
32}
33
34func init() {
35	cli.Register("pool.destroy", &destroy{})
36}
37
38func (cmd *destroy) Register(ctx context.Context, f *flag.FlagSet) {
39	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
40	cmd.DatacenterFlag.Register(ctx, f)
41
42	f.BoolVar(&cmd.children, "children", false, "Remove all children pools")
43}
44
45func (cmd *destroy) Process(ctx context.Context) error {
46	if err := cmd.DatacenterFlag.Process(ctx); err != nil {
47		return err
48	}
49	return nil
50}
51
52func (cmd *destroy) Usage() string {
53	return "POOL..."
54}
55
56func (cmd *destroy) Description() string {
57	return "Destroy one or more resource POOLs.\n" + poolNameHelp
58}
59
60func (cmd *destroy) Run(ctx context.Context, f *flag.FlagSet) error {
61	if f.NArg() == 0 {
62		return flag.ErrHelp
63	}
64
65	finder, err := cmd.Finder()
66	if err != nil {
67		return err
68	}
69
70	for _, arg := range f.Args() {
71		pools, err := finder.ResourcePoolList(ctx, arg)
72		if err != nil {
73			if _, ok := err.(*find.NotFoundError); ok {
74				// Ignore if pool cannot be found
75				continue
76			}
77
78			return err
79		}
80
81		for _, pool := range pools {
82			if cmd.children {
83				err = pool.DestroyChildren(ctx)
84				if err != nil {
85					return err
86				}
87			} else {
88				task, err := pool.Destroy(ctx)
89				if err != nil {
90					return err
91				}
92				err = task.Wait(ctx)
93				if err != nil {
94					return err
95				}
96			}
97		}
98	}
99
100	return nil
101}
102