github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/firewaller/firewaller.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package firewaller
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/api/base"
    11  	"github.com/juju/juju/api/common"
    12  	"github.com/juju/juju/api/common/cloudspec"
    13  	apiwatcher "github.com/juju/juju/api/watcher"
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/watcher"
    16  )
    17  
    18  const firewallerFacade = "Firewaller"
    19  
    20  // State provides access to the Firewaller API facade.
    21  type State struct {
    22  	facade base.FacadeCaller
    23  	*common.ModelWatcher
    24  	*cloudspec.CloudSpecAPI
    25  }
    26  
    27  // NewState creates a new client-side Firewaller API facade.
    28  func NewState(caller base.APICaller) *State {
    29  	facadeCaller := base.NewFacadeCaller(caller, firewallerFacade)
    30  	return &State{
    31  		facade:       facadeCaller,
    32  		ModelWatcher: common.NewModelWatcher(facadeCaller),
    33  		CloudSpecAPI: cloudspec.NewCloudSpecAPI(facadeCaller),
    34  	}
    35  }
    36  
    37  // BestAPIVersion returns the API version that we were able to
    38  // determine is supported by both the client and the API Server.
    39  func (st *State) BestAPIVersion() int {
    40  	return st.facade.BestAPIVersion()
    41  }
    42  
    43  // ModelTag returns the current model's tag.
    44  func (st *State) ModelTag() (names.ModelTag, bool) {
    45  	return st.facade.RawAPICaller().ModelTag()
    46  }
    47  
    48  // life requests the life cycle of the given entity from the server.
    49  func (st *State) life(tag names.Tag) (params.Life, error) {
    50  	return common.Life(st.facade, tag)
    51  }
    52  
    53  // Unit provides access to methods of a state.Unit through the facade.
    54  func (st *State) Unit(tag names.UnitTag) (*Unit, error) {
    55  	life, err := st.life(tag)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return &Unit{
    60  		tag:  tag,
    61  		life: life,
    62  		st:   st,
    63  	}, nil
    64  }
    65  
    66  // Machine provides access to methods of a state.Machine through the
    67  // facade.
    68  func (st *State) Machine(tag names.MachineTag) (*Machine, error) {
    69  	life, err := st.life(tag)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return &Machine{
    74  		tag:  tag,
    75  		life: life,
    76  		st:   st,
    77  	}, nil
    78  }
    79  
    80  // WatchModelMachines returns a StringsWatcher that notifies of
    81  // changes to the life cycles of the top level machines in the current
    82  // model.
    83  func (st *State) WatchModelMachines() (watcher.StringsWatcher, error) {
    84  	var result params.StringsWatchResult
    85  	err := st.facade.FacadeCall("WatchModelMachines", nil, &result)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	if err := result.Error; err != nil {
    90  		return nil, result.Error
    91  	}
    92  	w := apiwatcher.NewStringsWatcher(st.facade.RawAPICaller(), result)
    93  	return w, nil
    94  }
    95  
    96  // WatchOpenedPorts returns a StringsWatcher that notifies of
    97  // changes to the opened ports for the current model.
    98  func (st *State) WatchOpenedPorts() (watcher.StringsWatcher, error) {
    99  	modelTag, ok := st.ModelTag()
   100  	if !ok {
   101  		return nil, errors.New("API connection is controller-only (should never happen)")
   102  	}
   103  	var results params.StringsWatchResults
   104  	args := params.Entities{
   105  		Entities: []params.Entity{{Tag: modelTag.String()}},
   106  	}
   107  	if err := st.facade.FacadeCall("WatchOpenedPorts", args, &results); err != nil {
   108  		return nil, err
   109  	}
   110  	if len(results.Results) != 1 {
   111  		return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
   112  	}
   113  	result := results.Results[0]
   114  	if err := result.Error; err != nil {
   115  		return nil, result.Error
   116  	}
   117  	w := apiwatcher.NewStringsWatcher(st.facade.RawAPICaller(), result)
   118  	return w, nil
   119  }