github.com/vmware/govmomi@v0.37.2/govc/sso/idp/ls.go (about)

     1  /*
     2  Copyright (c) 2022 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 idp
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"strings"
    25  	"text/tabwriter"
    26  
    27  	"github.com/vmware/govmomi/govc/cli"
    28  	"github.com/vmware/govmomi/govc/flags"
    29  	"github.com/vmware/govmomi/govc/sso"
    30  	"github.com/vmware/govmomi/ssoadmin"
    31  	"github.com/vmware/govmomi/ssoadmin/types"
    32  )
    33  
    34  type ls struct {
    35  	*flags.ClientFlag
    36  	*flags.OutputFlag
    37  }
    38  
    39  func init() {
    40  	cli.Register("sso.idp.ls", &ls{})
    41  }
    42  
    43  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    44  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    45  	cmd.ClientFlag.Register(ctx, f)
    46  
    47  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    48  	cmd.OutputFlag.Register(ctx, f)
    49  }
    50  
    51  func (cmd *ls) Description() string {
    52  	return `List SSO identity provider sources.
    53  
    54  Examples:
    55    govc sso.idp.ls
    56    govc sso.idp.ls -json`
    57  }
    58  
    59  func (cmd *ls) Process(ctx context.Context) error {
    60  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    61  		return err
    62  	}
    63  	return cmd.OutputFlag.Process(ctx)
    64  }
    65  
    66  type idpInfo struct {
    67  	IdentitySources *types.IdentitySources
    68  }
    69  
    70  func (r *idpInfo) Write(w io.Writer) error {
    71  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    72  
    73  	type entry struct {
    74  		name   string
    75  		url    string
    76  		kind   string
    77  		domain string
    78  		alias  string
    79  	}
    80  
    81  	var entries []entry
    82  
    83  	for _, domain := range r.IdentitySources.System.Domains {
    84  		entries = append(entries, entry{"-", "-", "System Domain", domain.Name, domain.Alias})
    85  	}
    86  
    87  	if r.IdentitySources.LocalOS != nil {
    88  		for _, domain := range r.IdentitySources.LocalOS.Domains {
    89  			entries = append(entries, entry{"-", "-", "Local OS", domain.Name, domain.Alias})
    90  		}
    91  	}
    92  
    93  	if ad := r.IdentitySources.NativeAD; ad != nil {
    94  		for _, domain := range ad.Domains {
    95  			entries = append(entries, entry{ad.Name, "-", "Active Directory", domain.Name, domain.Alias})
    96  		}
    97  	}
    98  
    99  	for _, ldap := range r.IdentitySources.LDAPS {
   100  		for _, domain := range ldap.Domains {
   101  			entries = append(entries, entry{ldap.Name, ldap.Details.PrimaryURL, ldap.Type, domain.Name, domain.Alias})
   102  		}
   103  	}
   104  
   105  	fmt.Fprintln(tw, "Name\tServer URL\tType\tDomain\tAlias")
   106  
   107  	for _, e := range entries {
   108  		fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\n", e.name, e.url, e.kind, strings.ToLower(e.domain), e.alias)
   109  	}
   110  
   111  	return tw.Flush()
   112  }
   113  
   114  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
   115  	return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
   116  		sources, err := c.IdentitySources(ctx)
   117  		if err != nil {
   118  			return err
   119  		}
   120  
   121  		return cmd.WriteResult(&idpInfo{sources})
   122  	})
   123  }