github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/proxyupdater/proxyupdater.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package proxyupdater
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/proxy"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/api/base"
    14  	apiwatcher "github.com/juju/juju/api/watcher"
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/core/watcher"
    17  )
    18  
    19  const proxyUpdaterFacade = "ProxyUpdater"
    20  
    21  // API provides access to the ProxyUpdater API facade.
    22  type API struct {
    23  	tag    names.Tag
    24  	facade base.FacadeCaller
    25  }
    26  
    27  // NewAPI returns a new api client facade instance.
    28  func NewAPI(caller base.APICaller, tag names.Tag) (*API, error) {
    29  	if caller == nil {
    30  		return nil, fmt.Errorf("caller is nil")
    31  	}
    32  
    33  	if tag == nil {
    34  		return nil, fmt.Errorf("tag is nil")
    35  	}
    36  
    37  	return &API{
    38  		facade: base.NewFacadeCaller(caller, proxyUpdaterFacade),
    39  		tag:    tag,
    40  	}, nil
    41  }
    42  
    43  // WatchForProxyConfigAndAPIHostPortChanges returns a NotifyWatcher waiting for
    44  // changes in the proxy configuration or API host ports
    45  func (api *API) WatchForProxyConfigAndAPIHostPortChanges() (watcher.NotifyWatcher, error) {
    46  	var results params.NotifyWatchResults
    47  	args := params.Entities{
    48  		Entities: []params.Entity{{Tag: api.tag.String()}},
    49  	}
    50  	err := api.facade.FacadeCall("WatchForProxyConfigAndAPIHostPortChanges", args, &results)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	if len(results.Results) != 1 {
    55  		return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
    56  	}
    57  	result := results.Results[0]
    58  	if result.Error != nil {
    59  		return nil, result.Error
    60  	}
    61  
    62  	return newNotifyWatcher(api.facade.RawAPICaller(), result), nil
    63  }
    64  
    65  var newNotifyWatcher = apiwatcher.NewNotifyWatcher
    66  
    67  func proxySettingsParamToProxySettings(cfg params.ProxyConfig) proxy.Settings {
    68  	return proxy.Settings{
    69  		Http:    cfg.HTTP,
    70  		Https:   cfg.HTTPS,
    71  		Ftp:     cfg.FTP,
    72  		NoProxy: cfg.NoProxy,
    73  	}
    74  }
    75  
    76  // ProxyConfiguration contains the various proxy values for the model.
    77  type ProxyConfiguration struct {
    78  	LegacyProxy proxy.Settings
    79  	JujuProxy   proxy.Settings
    80  	APTProxy    proxy.Settings
    81  	SnapProxy   proxy.Settings
    82  
    83  	SnapStoreProxyId         string
    84  	SnapStoreProxyAssertions string
    85  }
    86  
    87  // ProxyConfig returns the proxy settings for the current model.
    88  func (api *API) ProxyConfig() (ProxyConfiguration, error) {
    89  	var empty ProxyConfiguration
    90  	if api.facade.BestAPIVersion() <= 1 {
    91  		legacyProxySettings, aptProxySettings, err := api.proxyConfigV1()
    92  		if err != nil {
    93  			return empty, err
    94  		}
    95  		return ProxyConfiguration{
    96  			LegacyProxy: legacyProxySettings,
    97  			APTProxy:    aptProxySettings,
    98  		}, nil
    99  	}
   100  
   101  	var results params.ProxyConfigResults
   102  	args := params.Entities{
   103  		Entities: []params.Entity{{Tag: api.tag.String()}},
   104  	}
   105  	err := api.facade.FacadeCall("ProxyConfig", args, &results)
   106  	if err != nil {
   107  		return empty, err
   108  	}
   109  	if len(results.Results) != 1 {
   110  		return empty, errors.NotFoundf("ProxyConfig for %q", api.tag)
   111  	}
   112  	result := results.Results[0]
   113  	return ProxyConfiguration{
   114  		LegacyProxy: proxySettingsParamToProxySettings(result.LegacyProxySettings),
   115  		JujuProxy:   proxySettingsParamToProxySettings(result.JujuProxySettings),
   116  		APTProxy:    proxySettingsParamToProxySettings(result.APTProxySettings),
   117  		SnapProxy:   proxySettingsParamToProxySettings(result.SnapProxySettings),
   118  
   119  		SnapStoreProxyId:         result.SnapStoreProxyId,
   120  		SnapStoreProxyAssertions: result.SnapStoreProxyAssertions,
   121  	}, nil
   122  }
   123  
   124  func (api *API) proxyConfigV1() (proxySettings, APTProxySettings proxy.Settings, err error) {
   125  	var results params.ProxyConfigResultsV1
   126  	args := params.Entities{
   127  		Entities: []params.Entity{{Tag: api.tag.String()}},
   128  	}
   129  	err = api.facade.FacadeCall("ProxyConfig", args, &results)
   130  	if err != nil {
   131  		return proxySettings, APTProxySettings, err
   132  	}
   133  	if len(results.Results) != 1 {
   134  		return proxySettings, APTProxySettings, errors.NotFoundf("ProxyConfig for %q", api.tag)
   135  	}
   136  	result := results.Results[0]
   137  	proxySettings = proxySettingsParamToProxySettings(result.ProxySettings)
   138  	APTProxySettings = proxySettingsParamToProxySettings(result.APTProxySettings)
   139  	return proxySettings, APTProxySettings, nil
   140  }