github.com/vmware/govmomi@v0.51.0/cli/gpu/host/profile/ls.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package profile
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"io"
    12  	"text/tabwriter"
    13  
    14  	"github.com/vmware/govmomi/cli"
    15  	"github.com/vmware/govmomi/cli/flags"
    16  	"github.com/vmware/govmomi/property"
    17  	"github.com/vmware/govmomi/vim25/mo"
    18  )
    19  
    20  type ls struct {
    21  	*flags.ClientFlag
    22  	*flags.HostSystemFlag
    23  }
    24  
    25  func init() {
    26  	cli.Register("gpu.host.profile.ls", &ls{})
    27  }
    28  
    29  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    30  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    31  	cmd.ClientFlag.Register(ctx, f)
    32  
    33  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    34  	cmd.HostSystemFlag.Register(ctx, f)
    35  }
    36  
    37  func (cmd *ls) Description() string {
    38  	return `List available vGPU profiles on host.
    39  
    40  Examples:
    41    govc gpu.host.profile.ls -host hostname
    42    govc gpu.host.profile.ls -host hostname -json | jq -r '.profiles[]'
    43    govc gpu.host.profile.ls -host hostname -json | jq -r '.profiles[] | select(contains("nvidia_a40"))'`
    44  }
    45  
    46  func (cmd *ls) Process(ctx context.Context) error {
    47  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    48  		return err
    49  	}
    50  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    51  		return err
    52  	}
    53  	return nil
    54  }
    55  
    56  type lsResult struct {
    57  	Profiles []string `json:"profiles"`
    58  }
    59  
    60  func (r *lsResult) Write(w io.Writer) error {
    61  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    62  	fmt.Fprintln(tw, "Available vGPU profiles:")
    63  	for _, profile := range r.Profiles {
    64  		fmt.Fprintf(tw, "  %s\n", profile)
    65  	}
    66  	return tw.Flush()
    67  }
    68  
    69  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    70  	host, err := cmd.HostSystem()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	if host == nil {
    76  		return flag.ErrHelp
    77  	}
    78  
    79  	var o mo.HostSystem
    80  	pc := property.DefaultCollector(host.Client())
    81  	err = pc.RetrieveOne(ctx, host.Reference(), []string{"config.sharedPassthruGpuTypes"}, &o)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	if o.Config == nil {
    87  		return fmt.Errorf("failed to get host configuration")
    88  	}
    89  
    90  	if len(o.Config.SharedPassthruGpuTypes) == 0 {
    91  		return fmt.Errorf("no vGPU profiles available on this host")
    92  	}
    93  
    94  	res := &lsResult{
    95  		Profiles: o.Config.SharedPassthruGpuTypes,
    96  	}
    97  
    98  	return cmd.WriteResult(res)
    99  }