github.com/vmware/govmomi@v0.43.0/govc/vcsa/proxy/proxy.go (about)

     1  /*
     2  Copyright (c) 2021-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 net
    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  	vnetworking "github.com/vmware/govmomi/vapi/appliance/networking"
    30  )
    31  
    32  type info struct {
    33  	*flags.ClientFlag
    34  	*flags.OutputFlag
    35  }
    36  
    37  func init() {
    38  	cli.Register("vcsa.net.proxy.info", &info{})
    39  }
    40  
    41  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    43  	cmd.ClientFlag.Register(ctx, f)
    44  
    45  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    46  	cmd.OutputFlag.Register(ctx, f)
    47  }
    48  
    49  func (cmd *info) Process(ctx context.Context) error {
    50  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    51  		return err
    52  	}
    53  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    54  		return err
    55  	}
    56  	return nil
    57  }
    58  
    59  func (cmd *info) Description() string {
    60  	return `Retrieve the VC networking proxy configuration
    61  
    62  Examples:
    63    govc vcsa.net.proxy.info`
    64  }
    65  
    66  type proxyResult struct {
    67  	Proxy   *vnetworking.ProxyList `json:"proxy"`
    68  	NoProxy []string               `json:"noProxy"`
    69  }
    70  
    71  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    72  	c, err := cmd.RestClient()
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	fwd := vnetworking.NewManager(c)
    78  
    79  	proxyRes, err := fwd.ProxyList(ctx)
    80  	if err != nil {
    81  		fmt.Println(err)
    82  		return nil
    83  	}
    84  
    85  	noProxyRes, err := fwd.NoProxy(ctx)
    86  	if err != nil {
    87  		fmt.Println(err)
    88  		return nil
    89  	}
    90  
    91  	return cmd.WriteResult(&proxyResult{proxyRes, noProxyRes})
    92  }
    93  
    94  func (res proxyResult) Write(w io.Writer) error {
    95  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    96  	printProxyConfig("HTTP", res.Proxy.Http, tw)
    97  	printProxyConfig("HTTPS", res.Proxy.Https, tw)
    98  	printProxyConfig("FTP", res.Proxy.Ftp, tw)
    99  	fmt.Fprintf(tw, "No Proxy addresses:\t%s\n", strings.Join(res.NoProxy, ", "))
   100  
   101  	return tw.Flush()
   102  }
   103  
   104  func printProxyConfig(proxyName string, proxyProtocolConfig vnetworking.Proxy, w io.Writer) {
   105  	if !proxyProtocolConfig.Enabled {
   106  		fmt.Fprintf(w, "%s proxy:\tDisabled\n", proxyName)
   107  		return
   108  	}
   109  
   110  	fmt.Fprintf(w, "%s proxy:\tEnabled\n", proxyName)
   111  	fmt.Fprintf(w, "\tServer:\t%s\n", proxyProtocolConfig.Server)
   112  	fmt.Fprintf(w, "\tPort:\t%d\n", proxyProtocolConfig.Port)
   113  	if proxyProtocolConfig.Username != "" {
   114  		fmt.Fprintf(w, "\tUsername:\t%s\n", proxyProtocolConfig.Username)
   115  	}
   116  }