Skip to content

Commit

Permalink
govc: add -b flag to volume.ls
Browse files Browse the repository at this point in the history
Signed-off-by: Doug MacEachern <dougm@broadcom.com>
  • Loading branch information
dougm committed Dec 24, 2024
1 parent cd78f76 commit 18a3019
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
70 changes: 68 additions & 2 deletions cli/volume/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"log"
"strings"
"text/tabwriter"

"github.com/vmware/govmomi/cli"
Expand All @@ -41,6 +42,7 @@ type ls struct {
long bool
id bool
disk bool
back bool
}

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

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

type lsWriter struct {
Volume []types.CnsVolume `json:"volume"`
Volume []types.CnsVolume `json:"volume"`
Info []types.BaseCnsVolumeOperationResult `json:"info,omitempty"`
cmd *ls
}

func (r *lsWriter) Dump() any {
if len(r.Info) != 0 {
return r.Info
}
return r.Volume
}

func (r *lsWriter) Write(w io.Writer) error {
if r.cmd.id {
for _, volume := range r.Volume {
Expand Down Expand Up @@ -123,6 +136,9 @@ func (r *lsWriter) Write(w io.Writer) error {

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

func (r *lsWriter) backing(id types.CnsVolumeId) string {
for _, info := range r.Info {
res, ok := info.(*types.CnsQueryVolumeInfoResult)
if !ok {
continue
}

switch vol := res.VolumeInfo.(type) {
case *types.CnsBlockVolumeInfo:
if vol.VStorageObject.Config.Id.Id == id.Id {
switch backing := vol.VStorageObject.Config.BaseConfigInfo.Backing.(type) {
case *vim.BaseConfigInfoDiskFileBackingInfo:
return backing.FilePath
}
}
}

if fault := res.Fault; fault != nil {
if f, ok := fault.Fault.(types.CnsFault); ok {
if strings.Contains(f.Reason, id.Id) {
return f.Reason
}
}
}
}
return "???"
}

func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
ds, err := cmd.DatastoreIfSpecified()
if err != nil {
Expand All @@ -154,6 +198,7 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
}

var volumes []types.CnsVolume
var info []types.BaseCnsVolumeOperationResult

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

return cmd.WriteResult(&lsWriter{volumes, cmd})
if cmd.back {
ids := make([]types.CnsVolumeId, len(volumes))
for i := range volumes {
ids[i] = volumes[i].VolumeId
}

task, err := c.QueryVolumeInfo(ctx, ids)
if err != nil {
return err
}

res, err := task.WaitForResult(ctx, nil)
if err != nil {
return err
}

if batch, ok := res.Result.(types.CnsVolumeOperationBatchResult); ok {
info = batch.VolumeResults
}
}

return cmd.WriteResult(&lsWriter{volumes, info, cmd})
}
3 changes: 3 additions & 0 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7398,10 +7398,13 @@ Examples:
govc volume.ls -l
govc volume.ls -ds vsanDatastore
govc volume.ls df86393b-5ae0-4fca-87d0-b692dbc67d45
govc volume.ls -json $id | jq -r .volume[].backingObjectDetails.backingDiskPath
govc volume.ls -b $id # verify backingDiskPath exists
govc disk.ls -l $(govc volume.ls -L pvc-9744a4ff-07f4-43c4-b8ed-48ea7a528734)
Options:
-L=false List volume disk or file backing ID only
-b=false List file backing path
-ds= Datastore [GOVC_DATASTORE]
-i=false List volume ID only
-l=false Long listing format
Expand Down

0 comments on commit 18a3019

Please sign in to comment.