-
Notifications
You must be signed in to change notification settings - Fork 911
/
ls.go
156 lines (122 loc) · 3.01 KB
/
ls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vsan
import (
"context"
"flag"
"fmt"
"net/url"
"text/tabwriter"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
)
type ls struct {
*flags.DatastoreFlag
long bool
orphan bool
}
func init() {
cli.Register("datastore.vsan.dom.ls", &ls{})
}
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
cmd.DatastoreFlag.Register(ctx, f)
f.BoolVar(&cmd.long, "l", false, "Long listing")
f.BoolVar(&cmd.orphan, "o", false, "List orphan objects")
}
func (cmd *ls) Process(ctx context.Context) error {
if err := cmd.DatastoreFlag.Process(ctx); err != nil {
return err
}
return nil
}
func (cmd *ls) Usage() string {
return "[UUID]..."
}
func (cmd *ls) Description() string {
return `List vSAN DOM objects in DS.
Examples:
govc datastore.vsan.dom.ls
govc datastore.vsan.dom.ls -ds vsanDatastore -l
govc datastore.vsan.dom.ls -l d85aa758-63f5-500a-3150-0200308e589c`
}
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
ds, err := cmd.Datastore()
if err != nil {
return err
}
var mds mo.Datastore
err = ds.Properties(ctx, ds.Reference(), []string{"summary"}, &mds)
if err != nil {
return err
}
if mds.Summary.Type != "vsan" {
return flag.ErrHelp
}
hosts, err := ds.AttachedHosts(ctx)
if err != nil {
return err
}
if len(hosts) == 0 {
return flag.ErrHelp
}
m, err := hosts[0].ConfigManager().VsanInternalSystem(ctx)
if err != nil {
return err
}
ids, err := m.QueryVsanObjectUuidsByFilter(ctx, f.Args(), 0, 0)
if err != nil {
return err
}
if len(ids) == 0 {
return nil
}
if !cmd.long && !cmd.orphan {
for _, id := range ids {
fmt.Fprintln(cmd.Out, id)
}
return nil
}
objs, err := m.GetVsanObjExtAttrs(ctx, ids)
if err != nil {
return err
}
u, err := url.Parse(mds.Summary.Url)
if err != nil {
return err
}
tw := tabwriter.NewWriter(cmd.Out, 2, 0, 2, ' ', 0)
cmd.Out = tw
for id, obj := range objs {
path := obj.DatastorePath(u.Path)
if cmd.orphan {
_, err = ds.Stat(ctx, path)
if err == nil {
continue
}
switch err.(type) {
case object.DatastoreNoSuchDirectoryError, object.DatastoreNoSuchFileError:
default:
return err
}
if !cmd.long {
fmt.Fprintln(cmd.Out, id)
continue
}
}
fmt.Fprintf(cmd.Out, "%s\t%s\t%s\n", id, obj.Class, path)
}
return tw.Flush()
}