-
Notifications
You must be signed in to change notification settings - Fork 911
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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
Ftp Proxy `json:"ftp,omitempty"` | ||
Http Proxy `json:"http,omitempty"` | ||
Https Proxy `json:"https,omitempty"` | ||
} | ||
|
||
// Proxy returns all Proxy configuration. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leave this as you had it, |
||
r := m.Resource(applianceNoProxyConfigPath) | ||
var res []string | ||
return res, m.Do(ctx, r.Request(http.MethodGet), &res) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 APIWhile 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.
There was a problem hiding this comment.
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