Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

govc: Add support for getting the VC proxy and no-proxy configuration #2435

Merged
merged 3 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions govc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import (
_ "github.com/vmware/govmomi/govc/task"
_ "github.com/vmware/govmomi/govc/vapp"
_ "github.com/vmware/govmomi/govc/vcsa/log"
_ "github.com/vmware/govmomi/govc/vcsa/proxy"
_ "github.com/vmware/govmomi/govc/version"
_ "github.com/vmware/govmomi/govc/vm"
_ "github.com/vmware/govmomi/govc/vm/disk"
Expand Down
116 changes: 116 additions & 0 deletions govc/vcsa/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright (c) 2021 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package net

import (
"context"
"flag"
"fmt"
"io"
"strings"
"text/tabwriter"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
vnetworking "github.com/vmware/govmomi/vapi/appliance/networking"
)

type info struct {
*flags.ClientFlag
*flags.OutputFlag
}

func init() {
cli.Register("vcsa.net.proxy.info", &info{})
}

func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)

cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
cmd.OutputFlag.Register(ctx, f)
}

func (cmd *info) Process(ctx context.Context) error {
if err := cmd.ClientFlag.Process(ctx); err != nil {
return err
}
if err := cmd.OutputFlag.Process(ctx); err != nil {
return err
}
return nil
}

func (cmd *info) Description() string {
return `Retrieve the VC networking proxy configuration

Examples:
govc vcsa.net.proxy.info`
}

type proxyResult struct {
proxy *vnetworking.ProxyConfig
noProxy []string
}

func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
c, err := cmd.RestClient()
if err != nil {
return err
}

fwd := vnetworking.NewManager(c)

proxyRes, err := fwd.ProxyConfig(ctx)
if err != nil {
fmt.Println(err)
return nil
}

noProxyRes, err := fwd.NoProxyConfig(ctx)
if err != nil {
fmt.Println(err)
return nil
}

return cmd.WriteResult(&proxyResult{proxyRes, noProxyRes})
}

func (res proxyResult) Write(w io.Writer) error {
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
printProxyConfig("HTTP", res.proxy.Http, tw)
printProxyConfig("HTTPS", res.proxy.Https, tw)
printProxyConfig("FTP", res.proxy.Ftp, tw)
fmt.Fprintf(tw, "No Proxy addresses:\t%s\n", strings.Join(res.noProxy, ", "))

return tw.Flush()
}

func printProxyConfig(proxyName string, proxyProtocolConfig vnetworking.Proxy, w io.Writer) {
if !proxyProtocolConfig.Enabled {
fmt.Fprintf(w, "%s proxy:\tDisabled\n", proxyName)
return
}

fmt.Fprintf(w, "%s proxy:\tEnabled\n", proxyName)
fmt.Fprintf(w, "\tServer:\t%s\n", proxyProtocolConfig.Server)
fmt.Fprintf(w, "\tPort:\t%d\n", proxyProtocolConfig.Port)
if proxyProtocolConfig.Username != "" {
fmt.Fprintf(w, "\tUsername:\t%s\n", proxyProtocolConfig.Username)
}
}
90 changes: 90 additions & 0 deletions vapi/appliance/networking/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright (c) 2021 VMware, Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0.
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package networking

import (
"context"
"net/http"

"github.com/vmware/govmomi/vapi/rest"
)

const applianceProxyConfigPath = "/appliance/networking/proxy"
const applianceNoProxyConfigPath = "/appliance/networking/noproxy"

// Manager provides convenience methods to configure appliance proxy.
type Manager struct {
*rest.Client
}

// NewManager creates a new Manager with the given client
func NewManager(client *rest.Client) *Manager {
return &Manager{
Client: client,
}
}

// Proxy represents configuration for specific proxy - ftp, http, https.
type Proxy struct {
Server string `json:"server,omitempty"`
Port int `json:"port,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Enabled bool `json:"enabled,omitempty"`
}

// ProxyConfig represents configuration for vcenter proxy.
type ProxyConfig struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we should name this ProxyList to match the API
While this structure isn't a list itself, it does capture the data returned by the ProxyList API.

And same with the method below. While the structure differs from what the API returns, I don't imagine new protocols being added to the list. And if they are, we can just add to this structure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Renamed the struct and the method

Ftp Proxy `json:"ftp,omitempty"`
Http Proxy `json:"http,omitempty"`
Https Proxy `json:"https,omitempty"`
}

// Proxy returns all Proxy configuration.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also make sure the doc comment matches the type/method name.

func (m *Manager) ProxyConfig(ctx context.Context) (*ProxyConfig, error) {
var res ProxyConfig
var rawRes []struct {
Key string
Value Proxy
}

r := m.Resource(applianceProxyConfigPath)
err := m.Do(ctx, r.Request(http.MethodGet), &rawRes)
if err != nil {
return &res, err
}

for _, c := range rawRes {
switch c.Key {
case "http":
res.Http = c.Value
case "https":
res.Https = c.Value
case "ftp":
res.Ftp = c.Value
}
}

return &res, nil
}

// NoProxy returns all excluded servers for proxying.
func (m *Manager) NoProxyConfig(ctx context.Context) ([]string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave this as you had it, NoProxy, since it matches the API name + return values.

r := m.Resource(applianceNoProxyConfigPath)
var res []string
return res, m.Do(ctx, r.Request(http.MethodGet), &res)
}