/
addresses.go
95 lines (84 loc) · 2.82 KB
/
addresses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/core/network"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
"github.com/juju/juju/state/watcher"
)
// APIAddressAccessor describes methods that allow agents to maintain
// up-to-date information on how to connect to the Juju API server.
type APIAddressAccessor interface {
Addresses() ([]string, error)
APIHostPortsForAgents() ([]network.SpaceHostPorts, error)
WatchAPIHostPortsForAgents() state.NotifyWatcher
}
// APIAddresser implements the APIAddresses method.
// Note that the getter backing for this implies that it is suitable for use by
// agents, which are bound by the configured controller management space.
// It is not suitable for callers requiring *all* available API addresses.
type APIAddresser struct {
resources facade.Resources
getter APIAddressAccessor
}
// NewAPIAddresser returns a new APIAddresser that uses the given getter to
// fetch its addresses.
func NewAPIAddresser(getter APIAddressAccessor, resources facade.Resources) *APIAddresser {
return &APIAddresser{
getter: getter,
resources: resources,
}
}
// APIHostPorts returns the API server addresses.
func (a *APIAddresser) APIHostPorts() (params.APIHostPortsResult, error) {
sSvrs, err := a.getter.APIHostPortsForAgents()
if err != nil {
return params.APIHostPortsResult{}, err
}
// Convert the SpaceHostPorts to the HostPorts indirection.
pSvrs := make([]network.HostPorts, len(sSvrs))
for i, sHPs := range sSvrs {
pSvrs[i] = sHPs.HostPorts()
}
return params.APIHostPortsResult{
Servers: params.FromHostsPorts(pSvrs),
}, nil
}
// WatchAPIHostPorts watches the API server addresses.
func (a *APIAddresser) WatchAPIHostPorts() (params.NotifyWatchResult, error) {
watch := a.getter.WatchAPIHostPortsForAgents()
if _, ok := <-watch.Changes(); ok {
return params.NotifyWatchResult{
NotifyWatcherId: a.resources.Register(watch),
}, nil
}
return params.NotifyWatchResult{}, watcher.EnsureErr(watch)
}
// APIAddresses returns the list of addresses used to connect to the API.
func (a *APIAddresser) APIAddresses() (params.StringsResult, error) {
addrs, err := apiAddresses(a.getter)
if err != nil {
return params.StringsResult{}, err
}
return params.StringsResult{
Result: addrs,
}, nil
}
func apiAddresses(getter APIHostPortsForAgentsGetter) ([]string, error) {
apiHostPorts, err := getter.APIHostPortsForAgents()
if err != nil {
return nil, err
}
var addrs = make([]string, 0, len(apiHostPorts))
for _, hostPorts := range apiHostPorts {
ordered := hostPorts.HostPorts().PrioritizedForScope(network.ScopeMatchCloudLocal)
for _, addr := range ordered {
if addr != "" {
addrs = append(addrs, addr)
}
}
}
return addrs, nil
}