github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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  // state server addresses and the CA public certificate.
    15  type AddressAndCertGetter interface {
    16  	Addresses() ([]string, error)
    17  	APIAddressesFromMachines() ([]string, error)
    18  	CACert() string
    19  	EnvironUUID() 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  	apiHostPorts, err := api.getter.APIHostPorts()
    64  	if err != nil {
    65  		return params.StringsResult{}, err
    66  	}
    67  	var addrs = make([]string, 0, len(apiHostPorts))
    68  	for _, hostPorts := range apiHostPorts {
    69  		addr := network.SelectInternalHostPort(hostPorts, false)
    70  		if addr != "" {
    71  			addrs = append(addrs, addr)
    72  		}
    73  	}
    74  	return params.StringsResult{
    75  		Result: addrs,
    76  	}, nil
    77  }
    78  
    79  // CACert returns the certificate used to validate the state connection.
    80  func (a *APIAddresser) CACert() params.BytesResult {
    81  	return params.BytesResult{
    82  		Result: []byte(a.getter.CACert()),
    83  	}
    84  }
    85  
    86  // EnvironUUID returns the environment UUID to connect to the environment
    87  // that the current connection is for.
    88  func (a *APIAddresser) EnvironUUID() params.StringResult {
    89  	return params.StringResult{Result: a.getter.EnvironUUID()}
    90  }
    91  
    92  // StateAddresser implements a common set of methods for getting state
    93  // server addresses, and the CA certificate used to authenticate them.
    94  type StateAddresser struct {
    95  	getter AddressAndCertGetter
    96  }
    97  
    98  // NewStateAddresser returns a new StateAddresser that uses the given
    99  // st value to fetch its addresses.
   100  func NewStateAddresser(getter AddressAndCertGetter) *StateAddresser {
   101  	return &StateAddresser{getter}
   102  }
   103  
   104  // StateAddresses returns the list of addresses used to connect to the state.
   105  func (a *StateAddresser) StateAddresses() (params.StringsResult, error) {
   106  	addrs, err := a.getter.Addresses()
   107  	if err != nil {
   108  		return params.StringsResult{}, err
   109  	}
   110  	return params.StringsResult{
   111  		Result: addrs,
   112  	}, nil
   113  }