Skip to content

Commit 18a3019

Browse files
committed
govc: add -b flag to volume.ls
Signed-off-by: Doug MacEachern <[email protected]>
1 parent cd78f76 commit 18a3019

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

cli/volume/ls.go

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"log"
25+
"strings"
2526
"text/tabwriter"
2627

2728
"github.com/vmware/govmomi/cli"
@@ -41,6 +42,7 @@ type ls struct {
4142
long bool
4243
id bool
4344
disk bool
45+
back bool
4446
}
4547

4648
func init() {
@@ -60,6 +62,7 @@ func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
6062
f.BoolVar(&cmd.long, "l", false, "Long listing format")
6163
f.BoolVar(&cmd.id, "i", false, "List volume ID only")
6264
f.BoolVar(&cmd.disk, "L", false, "List volume disk or file backing ID only")
65+
f.BoolVar(&cmd.back, "b", false, "List file backing path")
6366
}
6467

6568
func (cmd *ls) Process(ctx context.Context) error {
@@ -84,14 +87,24 @@ Examples:
8487
govc volume.ls -l
8588
govc volume.ls -ds vsanDatastore
8689
govc volume.ls df86393b-5ae0-4fca-87d0-b692dbc67d45
90+
govc volume.ls -json $id | jq -r .volume[].backingObjectDetails.backingDiskPath
91+
govc volume.ls -b $id # verify backingDiskPath exists
8792
govc disk.ls -l $(govc volume.ls -L pvc-9744a4ff-07f4-43c4-b8ed-48ea7a528734)`
8893
}
8994

9095
type lsWriter struct {
91-
Volume []types.CnsVolume `json:"volume"`
96+
Volume []types.CnsVolume `json:"volume"`
97+
Info []types.BaseCnsVolumeOperationResult `json:"info,omitempty"`
9298
cmd *ls
9399
}
94100

101+
func (r *lsWriter) Dump() any {
102+
if len(r.Info) != 0 {
103+
return r.Info
104+
}
105+
return r.Volume
106+
}
107+
95108
func (r *lsWriter) Write(w io.Writer) error {
96109
if r.cmd.id {
97110
for _, volume := range r.Volume {
@@ -123,6 +136,9 @@ func (r *lsWriter) Write(w io.Writer) error {
123136

124137
for _, volume := range r.Volume {
125138
fmt.Printf("%s\t%s", volume.VolumeId.Id, volume.Name)
139+
if r.cmd.back {
140+
fmt.Printf("\t%s", r.backing(volume.VolumeId))
141+
}
126142
if r.cmd.long {
127143
capacity := volume.BackingObjectDetails.GetCnsBackingObjectDetails().CapacityInMb
128144
c := volume.Metadata.ContainerCluster
@@ -134,6 +150,34 @@ func (r *lsWriter) Write(w io.Writer) error {
134150
return tw.Flush()
135151
}
136152

153+
func (r *lsWriter) backing(id types.CnsVolumeId) string {
154+
for _, info := range r.Info {
155+
res, ok := info.(*types.CnsQueryVolumeInfoResult)
156+
if !ok {
157+
continue
158+
}
159+
160+
switch vol := res.VolumeInfo.(type) {
161+
case *types.CnsBlockVolumeInfo:
162+
if vol.VStorageObject.Config.Id.Id == id.Id {
163+
switch backing := vol.VStorageObject.Config.BaseConfigInfo.Backing.(type) {
164+
case *vim.BaseConfigInfoDiskFileBackingInfo:
165+
return backing.FilePath
166+
}
167+
}
168+
}
169+
170+
if fault := res.Fault; fault != nil {
171+
if f, ok := fault.Fault.(types.CnsFault); ok {
172+
if strings.Contains(f.Reason, id.Id) {
173+
return f.Reason
174+
}
175+
}
176+
}
177+
}
178+
return "???"
179+
}
180+
137181
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
138182
ds, err := cmd.DatastoreIfSpecified()
139183
if err != nil {
@@ -154,6 +198,7 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
154198
}
155199

156200
var volumes []types.CnsVolume
201+
var info []types.BaseCnsVolumeOperationResult
157202

158203
for {
159204
res, err := c.QueryVolume(ctx, cmd.CnsQueryFilter)
@@ -170,5 +215,26 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
170215
cmd.Cursor = &res.Cursor
171216
}
172217

173-
return cmd.WriteResult(&lsWriter{volumes, cmd})
218+
if cmd.back {
219+
ids := make([]types.CnsVolumeId, len(volumes))
220+
for i := range volumes {
221+
ids[i] = volumes[i].VolumeId
222+
}
223+
224+
task, err := c.QueryVolumeInfo(ctx, ids)
225+
if err != nil {
226+
return err
227+
}
228+
229+
res, err := task.WaitForResult(ctx, nil)
230+
if err != nil {
231+
return err
232+
}
233+
234+
if batch, ok := res.Result.(types.CnsVolumeOperationBatchResult); ok {
235+
info = batch.VolumeResults
236+
}
237+
}
238+
239+
return cmd.WriteResult(&lsWriter{volumes, info, cmd})
174240
}

govc/USAGE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7398,10 +7398,13 @@ Examples:
73987398
govc volume.ls -l
73997399
govc volume.ls -ds vsanDatastore
74007400
govc volume.ls df86393b-5ae0-4fca-87d0-b692dbc67d45
7401+
govc volume.ls -json $id | jq -r .volume[].backingObjectDetails.backingDiskPath
7402+
govc volume.ls -b $id # verify backingDiskPath exists
74017403
govc disk.ls -l $(govc volume.ls -L pvc-9744a4ff-07f4-43c4-b8ed-48ea7a528734)
74027404
74037405
Options:
74047406
-L=false List volume disk or file backing ID only
7407+
-b=false List file backing path
74057408
-ds= Datastore [GOVC_DATASTORE]
74067409
-i=false List volume ID only
74077410
-l=false Long listing format

0 commit comments

Comments
 (0)