github.com/vmware/govmomi@v0.37.2/vapi/appliance/networking/proxy.go (about)

     1  /*
     2  Copyright (c) 2021 VMware, Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0.
     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 networking
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  
    23  	"github.com/vmware/govmomi/vapi/rest"
    24  )
    25  
    26  const applianceProxyConfigPath = "/appliance/networking/proxy"
    27  const applianceNoProxyConfigPath = "/appliance/networking/noproxy"
    28  
    29  // Manager provides convenience methods to configure appliance proxy.
    30  type Manager struct {
    31  	*rest.Client
    32  }
    33  
    34  // NewManager creates a new Manager with the given client
    35  func NewManager(client *rest.Client) *Manager {
    36  	return &Manager{
    37  		Client: client,
    38  	}
    39  }
    40  
    41  // Proxy represents configuration for specific proxy - ftp, http, https.
    42  type Proxy struct {
    43  	Server   string `json:"server,omitempty"`
    44  	Port     int    `json:"port,omitempty"`
    45  	Username string `json:"username,omitempty"`
    46  	Password string `json:"password,omitempty"`
    47  	Enabled  bool   `json:"enabled,omitempty"`
    48  }
    49  
    50  // ProxyList represents configuration for vcenter proxy.
    51  type ProxyList struct {
    52  	Ftp   Proxy `json:"ftp,omitempty"`
    53  	Http  Proxy `json:"http,omitempty"`
    54  	Https Proxy `json:"https,omitempty"`
    55  }
    56  
    57  // ProxyList returns all Proxy configuration.
    58  func (m *Manager) ProxyList(ctx context.Context) (*ProxyList, error) {
    59  	var res ProxyList
    60  	var rawRes []struct {
    61  		Key   string
    62  		Value Proxy
    63  	}
    64  
    65  	r := m.Resource(applianceProxyConfigPath)
    66  	err := m.Do(ctx, r.Request(http.MethodGet), &rawRes)
    67  	if err != nil {
    68  		return &res, err
    69  	}
    70  
    71  	for _, c := range rawRes {
    72  		switch c.Key {
    73  		case "http":
    74  			res.Http = c.Value
    75  		case "https":
    76  			res.Https = c.Value
    77  		case "ftp":
    78  			res.Ftp = c.Value
    79  		}
    80  	}
    81  
    82  	return &res, nil
    83  }
    84  
    85  // NoProxy returns all excluded servers for proxying.
    86  func (m *Manager) NoProxy(ctx context.Context) ([]string, error) {
    87  	r := m.Resource(applianceNoProxyConfigPath)
    88  	var res []string
    89  	return res, m.Do(ctx, r.Request(http.MethodGet), &res)
    90  }