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