github.com/vmware/govmomi@v0.43.0/govc/vm/rdm/ls.go (about)

     1  /*
     2  Copyright (c) 2017-2023 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package rdm
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  	"strings"
    26  	"text/tabwriter"
    27  
    28  	"github.com/vmware/govmomi/govc/cli"
    29  	"github.com/vmware/govmomi/govc/flags"
    30  	"github.com/vmware/govmomi/object"
    31  	"github.com/vmware/govmomi/vim25/types"
    32  )
    33  
    34  type ls struct {
    35  	*flags.VirtualMachineFlag
    36  	*flags.OutputFlag
    37  }
    38  
    39  func init() {
    40  	cli.Register("vm.rdm.ls", &ls{})
    41  }
    42  
    43  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    44  
    45  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    46  	cmd.VirtualMachineFlag.Register(ctx, f)
    47  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    48  	cmd.OutputFlag.Register(ctx, f)
    49  }
    50  
    51  func (cmd *ls) Description() string {
    52  	return `List available devices that could be attach to VM with RDM.
    53  
    54  Examples:
    55    govc vm.rdm.ls -vm VM`
    56  }
    57  
    58  func (cmd *ls) Process(ctx context.Context) error {
    59  
    60  	if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
    61  		return err
    62  	}
    63  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    64  		return err
    65  	}
    66  	return nil
    67  }
    68  
    69  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    70  	vm, err := cmd.VirtualMachine()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	if vm == nil {
    76  		return flag.ErrHelp
    77  	}
    78  
    79  	vmConfigOptions, err := queryConfigTarget(ctx, vm)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	res := infoResult{
    85  		Disks: vmConfigOptions.ScsiDisk,
    86  	}
    87  	return cmd.WriteResult(&res)
    88  }
    89  
    90  type infoResult struct {
    91  	Disks []types.VirtualMachineScsiDiskDeviceInfo `json:"disks"`
    92  }
    93  
    94  func (r *infoResult) Write(w io.Writer) error {
    95  	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
    96  	for _, disk := range r.Disks {
    97  		fmt.Fprintf(tw, "Name:\t%s\n", disk.Name)
    98  		fmt.Fprintf(tw, "  Device name:\t%s\n", disk.Disk.DeviceName)
    99  		fmt.Fprintf(tw, "  Device path:\t%s\n", disk.Disk.DevicePath)
   100  		fmt.Fprintf(tw, "  Canonical Name:\t%s\n", disk.Disk.CanonicalName)
   101  
   102  		var uids []string
   103  		for _, descriptor := range disk.Disk.Descriptor {
   104  			uids = append(uids, descriptor.Id)
   105  		}
   106  
   107  		fmt.Fprintf(tw, "  UIDS:\t%s\n", strings.Join(uids, " ,"))
   108  	}
   109  	return tw.Flush()
   110  }
   111  
   112  func queryConfigTarget(ctx context.Context, m *object.VirtualMachine) (*types.ConfigTarget, error) {
   113  	b, err := m.EnvironmentBrowser(ctx)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  	return b.QueryConfigTarget(ctx, nil)
   118  }