github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/common/addresses.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common 5 6 import ( 7 "github.com/juju/juju/apiserver/params" 8 "github.com/juju/juju/network" 9 "github.com/juju/juju/state" 10 "github.com/juju/juju/state/watcher" 11 ) 12 13 // AddressAndCertGetter can be used to find out 14 // controller addresses and the CA public certificate. 15 type AddressAndCertGetter interface { 16 Addresses() ([]string, error) 17 APIAddressesFromMachines() ([]string, error) 18 CACert() string 19 ModelUUID() string 20 APIHostPorts() ([][]network.HostPort, error) 21 WatchAPIHostPorts() state.NotifyWatcher 22 } 23 24 // APIAddresser implements the APIAddresses method 25 type APIAddresser struct { 26 resources *Resources 27 getter AddressAndCertGetter 28 } 29 30 // NewAPIAddresser returns a new APIAddresser that uses the given getter to 31 // fetch its addresses. 32 func NewAPIAddresser(getter AddressAndCertGetter, resources *Resources) *APIAddresser { 33 return &APIAddresser{ 34 getter: getter, 35 resources: resources, 36 } 37 } 38 39 // APIHostPorts returns the API server addresses. 40 func (api *APIAddresser) APIHostPorts() (params.APIHostPortsResult, error) { 41 servers, err := api.getter.APIHostPorts() 42 if err != nil { 43 return params.APIHostPortsResult{}, err 44 } 45 return params.APIHostPortsResult{ 46 Servers: params.FromNetworkHostsPorts(servers), 47 }, nil 48 } 49 50 // WatchAPIHostPorts watches the API server addresses. 51 func (api *APIAddresser) WatchAPIHostPorts() (params.NotifyWatchResult, error) { 52 watch := api.getter.WatchAPIHostPorts() 53 if _, ok := <-watch.Changes(); ok { 54 return params.NotifyWatchResult{ 55 NotifyWatcherId: api.resources.Register(watch), 56 }, nil 57 } 58 return params.NotifyWatchResult{}, watcher.EnsureErr(watch) 59 } 60 61 // APIAddresses returns the list of addresses used to connect to the API. 62 func (api *APIAddresser) APIAddresses() (params.StringsResult, error) { 63 addrs, err := apiAddresses(api.getter) 64 if err != nil { 65 return params.StringsResult{}, err 66 } 67 return params.StringsResult{ 68 Result: addrs, 69 }, nil 70 } 71 72 func apiAddresses(getter APIHostPortsGetter) ([]string, error) { 73 apiHostPorts, err := getter.APIHostPorts() 74 if err != nil { 75 return nil, err 76 } 77 var addrs = make([]string, 0, len(apiHostPorts)) 78 for _, hostPorts := range apiHostPorts { 79 ordered := network.PrioritizeInternalHostPorts(hostPorts, false) 80 for _, addr := range ordered { 81 if addr != "" { 82 addrs = append(addrs, addr) 83 } 84 } 85 } 86 return addrs, nil 87 } 88 89 // CACert returns the certificate used to validate the state connection. 90 func (a *APIAddresser) CACert() params.BytesResult { 91 return params.BytesResult{ 92 Result: []byte(a.getter.CACert()), 93 } 94 } 95 96 // ModelUUID returns the model UUID to connect to the environment 97 // that the current connection is for. 98 func (a *APIAddresser) ModelUUID() params.StringResult { 99 return params.StringResult{Result: a.getter.ModelUUID()} 100 } 101 102 // StateAddresser implements a common set of methods for getting state 103 // server addresses, and the CA certificate used to authenticate them. 104 type StateAddresser struct { 105 getter AddressAndCertGetter 106 } 107 108 // NewStateAddresser returns a new StateAddresser that uses the given 109 // st value to fetch its addresses. 110 func NewStateAddresser(getter AddressAndCertGetter) *StateAddresser { 111 return &StateAddresser{getter} 112 } 113 114 // StateAddresses returns the list of addresses used to connect to the state. 115 func (a *StateAddresser) StateAddresses() (params.StringsResult, error) { 116 addrs, err := a.getter.Addresses() 117 if err != nil { 118 return params.StringsResult{}, err 119 } 120 return params.StringsResult{ 121 Result: addrs, 122 }, nil 123 }