github.com/vmware/govmomi@v0.51.0/cli/sso/service/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 service
     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/lookup"
    17  	"github.com/vmware/govmomi/lookup/types"
    18  )
    19  
    20  type ls struct {
    21  	*flags.ClientFlag
    22  	*flags.OutputFlag
    23  
    24  	long bool
    25  	url  bool
    26  
    27  	types.LookupServiceRegistrationFilter
    28  }
    29  
    30  func init() {
    31  	cli.Register("sso.service.ls", &ls{})
    32  }
    33  
    34  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    35  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    36  	cmd.ClientFlag.Register(ctx, f)
    37  
    38  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    39  	cmd.OutputFlag.Register(ctx, f)
    40  
    41  	f.BoolVar(&cmd.long, "l", false, "Long listing format")
    42  	f.BoolVar(&cmd.url, "U", false, "List endpoint URL(s) only")
    43  
    44  	cmd.LookupServiceRegistrationFilter.EndpointType = new(types.LookupServiceRegistrationEndpointType)
    45  	cmd.LookupServiceRegistrationFilter.ServiceType = new(types.LookupServiceRegistrationServiceType)
    46  	f.StringVar(&cmd.SiteId, "s", "", "Site ID")
    47  	f.StringVar(&cmd.NodeId, "n", "", "Node ID")
    48  	f.StringVar(&cmd.ServiceType.Type, "t", "", "Service type")
    49  	f.StringVar(&cmd.ServiceType.Product, "p", "", "Service product")
    50  	f.StringVar(&cmd.EndpointType.Type, "T", "", "Endpoint type")
    51  	f.StringVar(&cmd.EndpointType.Protocol, "P", "", "Endpoint protocol")
    52  }
    53  
    54  func (cmd *ls) Description() string {
    55  	return `List platform services.
    56  
    57  Examples:
    58    govc sso.service.ls
    59    govc sso.service.ls -t vcenterserver -P vmomi
    60    govc sso.service.ls -t cs.identity
    61    govc sso.service.ls -t cs.identity -P wsTrust -U
    62    govc sso.service.ls -t cs.identity -json | jq -r .[].ServiceEndpoints[].Url`
    63  }
    64  
    65  func (cmd *ls) Process(ctx context.Context) error {
    66  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    67  		return err
    68  	}
    69  	return cmd.OutputFlag.Process(ctx)
    70  }
    71  
    72  type infoResult []types.LookupServiceRegistrationInfo
    73  
    74  func (r infoResult) Dump() any {
    75  	return []types.LookupServiceRegistrationInfo(r)
    76  }
    77  
    78  func (r infoResult) Write(w io.Writer) error {
    79  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    80  
    81  	for _, info := range r {
    82  		fmt.Fprintf(tw, "%s\t%s\t%s\n", info.ServiceType.Product, info.ServiceType.Type, info.ServiceId)
    83  	}
    84  
    85  	return tw.Flush()
    86  }
    87  
    88  type infoResultLong []types.LookupServiceRegistrationInfo
    89  
    90  func (r infoResultLong) Dump() any {
    91  	return []types.LookupServiceRegistrationInfo(r)
    92  }
    93  
    94  func (r infoResultLong) Write(w io.Writer) error {
    95  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    96  
    97  	for _, info := range r {
    98  		for _, s := range info.ServiceEndpoints {
    99  			fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\t%s\n",
   100  				info.ServiceType.Product, info.ServiceType.Type, info.ServiceId,
   101  				s.EndpointType.Protocol, s.EndpointType.Type, s.Url)
   102  		}
   103  	}
   104  
   105  	return tw.Flush()
   106  }
   107  
   108  type infoResultURL []types.LookupServiceRegistrationInfo
   109  
   110  func (r infoResultURL) Dump() any {
   111  	return []types.LookupServiceRegistrationInfo(r)
   112  }
   113  
   114  func (r infoResultURL) Write(w io.Writer) error {
   115  	for _, info := range r {
   116  		for _, s := range info.ServiceEndpoints {
   117  			fmt.Fprintln(w, s.Url)
   118  		}
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
   125  	vc, err := cmd.Client()
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	c, err := lookup.NewClient(ctx, vc)
   131  	if err != nil {
   132  		return err
   133  	}
   134  	c.RoundTripper = cmd.RoundTripper(c.Client)
   135  
   136  	info, err := c.List(ctx, &cmd.LookupServiceRegistrationFilter)
   137  	if err != nil {
   138  		return err
   139  	}
   140  
   141  	switch {
   142  	case cmd.long:
   143  		return cmd.WriteResult(infoResultLong(info))
   144  	case cmd.url:
   145  		return cmd.WriteResult(infoResultURL(info))
   146  	default:
   147  		return cmd.WriteResult(infoResult(info))
   148  	}
   149  }