1/*
2Copyright (c) 2017 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 vsan
18
19import (
20	"context"
21	"flag"
22	"fmt"
23	"os"
24
25	"github.com/vmware/govmomi/govc/cli"
26	"github.com/vmware/govmomi/govc/flags"
27)
28
29type rm struct {
30	*flags.DatastoreFlag
31
32	force   bool
33	verbose bool
34}
35
36func init() {
37	cli.Register("datastore.vsan.dom.rm", &rm{})
38}
39
40func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) {
41	cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
42	cmd.DatastoreFlag.Register(ctx, f)
43
44	f.BoolVar(&cmd.force, "f", false, "Force delete")
45	f.BoolVar(&cmd.verbose, "v", false, "Print deleted UUIDs to stdout, failed to stderr")
46}
47
48func (cmd *rm) Process(ctx context.Context) error {
49	if err := cmd.DatastoreFlag.Process(ctx); err != nil {
50		return err
51	}
52	return nil
53}
54
55func (cmd *rm) Usage() string {
56	return "UUID..."
57}
58
59func (cmd *rm) Description() string {
60	return `Remove vSAN DOM objects in DS.
61
62Examples:
63  govc datastore.vsan.dom.rm d85aa758-63f5-500a-3150-0200308e589c
64  govc datastore.vsan.dom.rm -f d85aa758-63f5-500a-3150-0200308e589c
65  govc datastore.vsan.dom.ls -o | xargs govc datastore.vsan.dom.rm`
66}
67
68func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
69	if f.NArg() == 0 {
70		return flag.ErrHelp
71	}
72
73	ds, err := cmd.Datastore()
74	if err != nil {
75		return err
76	}
77
78	hosts, err := ds.AttachedHosts(ctx)
79	if err != nil {
80		return err
81	}
82
83	if len(hosts) == 0 {
84		return flag.ErrHelp
85	}
86
87	m, err := hosts[0].ConfigManager().VsanInternalSystem(ctx)
88	if err != nil {
89		return err
90	}
91
92	res, err := m.DeleteVsanObjects(ctx, f.Args(), &cmd.force)
93	if err != nil {
94		return err
95	}
96
97	if cmd.verbose {
98		for _, r := range res {
99			if r.Success {
100				fmt.Fprintln(cmd.Out, r.Uuid)
101			} else {
102				fmt.Fprintf(os.Stderr, "%s %s\n", r.Uuid, r.FailureReason[0].Message)
103			}
104		}
105	}
106
107	return nil
108}
109