github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/core/network"
    10  	"github.com/juju/juju/core/watcher"
    11  	"github.com/juju/juju/rpc/params"
    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  // APIHostPorts returns the host/port addresses of the API servers.
    43  func (a *APIAddresser) APIHostPorts() ([]network.ProviderHostPorts, error) {
    44  	var result params.APIHostPortsResult
    45  	err := a.facade.FacadeCall("APIHostPorts", nil, &result)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	return params.ToProviderHostsPorts(result.Servers), nil
    50  }
    51  
    52  // WatchAPIHostPorts watches the host/port addresses of the API servers.
    53  func (a *APIAddresser) WatchAPIHostPorts() (watcher.NotifyWatcher, error) {
    54  	var result params.NotifyWatchResult
    55  	err := a.facade.FacadeCall("WatchAPIHostPorts", nil, &result)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return apiwatcher.NewNotifyWatcher(a.facade.RawAPICaller(), result), nil
    60  }