github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/state/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/instance" 8 "github.com/juju/juju/state" 9 "github.com/juju/juju/state/api/params" 10 "github.com/juju/juju/state/watcher" 11 ) 12 13 // AddressAndCertGetter can be used to find out 14 // state server addresses and the CA public certificate. 15 type AddressAndCertGetter interface { 16 Addresses() ([]string, error) 17 APIAddressesFromMachines() ([]string, error) 18 CACert() string 19 APIHostPorts() ([][]instance.HostPort, error) 20 WatchAPIHostPorts() state.NotifyWatcher 21 } 22 23 // APIAddresser implements the APIAddresses method 24 type APIAddresser struct { 25 resources *Resources 26 getter AddressAndCertGetter 27 } 28 29 // NewAPIAddresser returns a new APIAddresser that uses the given getter to 30 // fetch its addresses. 31 func NewAPIAddresser(getter AddressAndCertGetter, resources *Resources) *APIAddresser { 32 return &APIAddresser{ 33 getter: getter, 34 resources: resources, 35 } 36 } 37 38 // APIHostPorts returns the API server addresses. 39 func (api *APIAddresser) APIHostPorts() (params.APIHostPortsResult, error) { 40 servers, err := api.getter.APIHostPorts() 41 if err != nil { 42 return params.APIHostPortsResult{}, err 43 } 44 return params.APIHostPortsResult{ 45 Servers: servers, 46 }, nil 47 } 48 49 // WatchAPIHostPorts watches the API server addresses. 50 func (api *APIAddresser) WatchAPIHostPorts() (params.NotifyWatchResult, error) { 51 watch := api.getter.WatchAPIHostPorts() 52 if _, ok := <-watch.Changes(); ok { 53 return params.NotifyWatchResult{ 54 NotifyWatcherId: api.resources.Register(watch), 55 }, nil 56 } 57 return params.NotifyWatchResult{}, watcher.MustErr(watch) 58 } 59 60 // APIAddresses returns the list of addresses used to connect to the API. 61 func (api *APIAddresser) APIAddresses() (params.StringsResult, error) { 62 apiHostPorts, err := api.getter.APIHostPorts() 63 if err != nil { 64 return params.StringsResult{}, err 65 } 66 var addrs = make([]string, 0, len(apiHostPorts)) 67 for _, hostPorts := range apiHostPorts { 68 addr := instance.SelectInternalHostPort(hostPorts, false) 69 if addr != "" { 70 addrs = append(addrs, addr) 71 } 72 } 73 return params.StringsResult{ 74 Result: addrs, 75 }, nil 76 } 77 78 // CACert returns the certificate used to validate the state connection. 79 func (a *APIAddresser) CACert() params.BytesResult { 80 return params.BytesResult{ 81 Result: []byte(a.getter.CACert()), 82 } 83 } 84 85 // StateAddresser implements a common set of methods for getting state 86 // server addresses, and the CA certificate used to authenticate them. 87 type StateAddresser struct { 88 getter AddressAndCertGetter 89 } 90 91 // NewStateAddresser returns a new StateAddresser that uses the given 92 // st value to fetch its addresses. 93 func NewStateAddresser(getter AddressAndCertGetter) *StateAddresser { 94 return &StateAddresser{getter} 95 } 96 97 // StateAddresses returns the list of addresses used to connect to the state. 98 func (a *StateAddresser) StateAddresses() (params.StringsResult, error) { 99 addrs, err := a.getter.Addresses() 100 if err != nil { 101 return params.StringsResult{}, err 102 } 103 return params.StringsResult{ 104 Result: addrs, 105 }, nil 106 }