~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/api/proxyupdater/proxyupdater.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/juju/api/base"
 
11
        apiwatcher "github.com/juju/juju/api/watcher"
 
12
        "github.com/juju/juju/apiserver/params"
 
13
        "github.com/juju/juju/watcher"
 
14
        "github.com/juju/utils/proxy"
 
15
        "gopkg.in/juju/names.v2"
 
16
)
 
17
 
 
18
const proxyUpdaterFacade = "ProxyUpdater"
 
19
 
 
20
// API provides access to the ProxyUpdater API facade.
 
21
type API struct {
 
22
        tag    names.Tag
 
23
        facade base.FacadeCaller
 
24
}
 
25
 
 
26
// NewAPI returns a new api client facade instance.
 
27
func NewAPI(caller base.APICaller, tag names.Tag) (*API, error) {
 
28
        if caller == nil {
 
29
                return nil, fmt.Errorf("caller is nil")
 
30
        }
 
31
 
 
32
        if tag == nil {
 
33
                return nil, fmt.Errorf("tag is nil")
 
34
        }
 
35
 
 
36
        return &API{
 
37
                facade: base.NewFacadeCaller(caller, proxyUpdaterFacade),
 
38
                tag:    tag,
 
39
        }, nil
 
40
}
 
41
 
 
42
// WatchForProxyConfigAndAPIHostPortChanges returns a NotifyWatcher waiting for
 
43
// changes in the proxy configuration or API host ports
 
44
func (api *API) WatchForProxyConfigAndAPIHostPortChanges() (watcher.NotifyWatcher, error) {
 
45
        var results params.NotifyWatchResults
 
46
        args := params.Entities{
 
47
                Entities: []params.Entity{{Tag: api.tag.String()}},
 
48
        }
 
49
        err := api.facade.FacadeCall("WatchForProxyConfigAndAPIHostPortChanges", args, &results)
 
50
        if err != nil {
 
51
                return nil, err
 
52
        }
 
53
        if len(results.Results) != 1 {
 
54
                return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
 
55
        }
 
56
        result := results.Results[0]
 
57
        if result.Error != nil {
 
58
                return nil, result.Error
 
59
        }
 
60
 
 
61
        return newNotifyWatcher(api.facade.RawAPICaller(), result), nil
 
62
}
 
63
 
 
64
var newNotifyWatcher = apiwatcher.NewNotifyWatcher
 
65
 
 
66
func proxySettingsParamToProxySettings(cfg params.ProxyConfig) proxy.Settings {
 
67
        return proxy.Settings{
 
68
                Http:    cfg.HTTP,
 
69
                Https:   cfg.HTTPS,
 
70
                Ftp:     cfg.FTP,
 
71
                NoProxy: cfg.NoProxy,
 
72
        }
 
73
}
 
74
 
 
75
// ProxyConfig returns the proxy settings for the current environment
 
76
func (api *API) ProxyConfig() (proxySettings, APTProxySettings proxy.Settings, err error) {
 
77
        var results params.ProxyConfigResults
 
78
        args := params.Entities{
 
79
                Entities: []params.Entity{{Tag: api.tag.String()}},
 
80
        }
 
81
        err = api.facade.FacadeCall("ProxyConfig", args, &results)
 
82
        if err != nil {
 
83
                return proxySettings, APTProxySettings, err
 
84
        }
 
85
        if len(results.Results) != 1 {
 
86
                return proxySettings, APTProxySettings, errors.NotFoundf("ProxyConfig for %q", api.tag)
 
87
        }
 
88
        result := results.Results[0]
 
89
        proxySettings = proxySettingsParamToProxySettings(result.ProxySettings)
 
90
        APTProxySettings = proxySettingsParamToProxySettings(result.APTProxySettings)
 
91
        return proxySettings, APTProxySettings, nil
 
92
}