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