github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/common/apiaddresser.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common
     5  
     6  import (
     7  	"github.com/juju/juju/api/base"
     8  	apiwatcher "github.com/juju/juju/api/watcher"
     9  	"github.com/juju/juju/apiserver/params"
    10  	"github.com/juju/juju/core/watcher"
    11  	"github.com/juju/juju/network"
    12  )
    13  
    14  // APIAddresser provides common client-side API
    15  // functions to call into apiserver.common.APIAddresser
    16  type APIAddresser struct {
    17  	facade base.FacadeCaller
    18  }
    19  
    20  // NewAPIAddresser returns a new APIAddresser that makes API calls
    21  // using caller and the specified facade name.
    22  func NewAPIAddresser(facade base.FacadeCaller) *APIAddresser {
    23  	return &APIAddresser{
    24  		facade: facade,
    25  	}
    26  }
    27  
    28  // APIAddresses returns the list of addresses used to connect to the API.
    29  func (a *APIAddresser) APIAddresses() ([]string, error) {
    30  	var result params.StringsResult
    31  	err := a.facade.FacadeCall("APIAddresses", nil, &result)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	if err := result.Error; err != nil {
    37  		return nil, err
    38  	}
    39  	return result.Result, nil
    40  }
    41  
    42  // ModelUUID returns the model UUID to connect to the model
    43  // that the current connection is for.
    44  //
    45  // TODO(axw) this has bugger all to do with addresses, and
    46  // so should not be on this type. Get it from somewhere else,
    47  // e.g. by passing it into the model-specific workers.
    48  func (a *APIAddresser) ModelUUID() (string, error) {
    49  	var result params.StringResult
    50  	err := a.facade.FacadeCall("ModelUUID", nil, &result)
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  	return result.Result, nil
    55  }
    56  
    57  // APIHostPorts returns the host/port addresses of the API servers.
    58  func (a *APIAddresser) APIHostPorts() ([][]network.HostPort, error) {
    59  	var result params.APIHostPortsResult
    60  	err := a.facade.FacadeCall("APIHostPorts", nil, &result)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return result.NetworkHostsPorts(), nil
    65  }
    66  
    67  // WatchAPIHostPorts watches the host/port addresses of the API servers.
    68  func (a *APIAddresser) WatchAPIHostPorts() (watcher.NotifyWatcher, error) {
    69  	var result params.NotifyWatchResult
    70  	err := a.facade.FacadeCall("WatchAPIHostPorts", nil, &result)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return apiwatcher.NewNotifyWatcher(a.facade.RawAPICaller(), result), nil
    75  }