launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/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  	"launchpad.net/errgo/errors"
     8  	"launchpad.net/juju-core/environs/config"
     9  	"launchpad.net/juju-core/state/api/base"
    10  	"launchpad.net/juju-core/state/api/common"
    11  	"launchpad.net/juju-core/state/api/params"
    12  	"launchpad.net/juju-core/state/api/watcher"
    13  )
    14  
    15  var mask = errors.Mask
    16  
    17  // State provides access to the Firewaller API facade.
    18  type State struct {
    19  	caller base.Caller
    20  }
    21  
    22  // NewState creates a new client-side Firewaller facade.
    23  func NewState(caller base.Caller) *State {
    24  	return &State{caller}
    25  }
    26  
    27  // life requests the life cycle of the given entity from the server.
    28  func (st *State) life(tag string) (params.Life, error) {
    29  	return common.Life(st.caller, "Firewaller", tag)
    30  }
    31  
    32  // Unit provides access to methods of a state.Unit through the facade.
    33  func (st *State) Unit(tag string) (*Unit, error) {
    34  	life, err := st.life(tag)
    35  	if err != nil {
    36  		return nil, base.WrapError(err)
    37  	}
    38  	return &Unit{
    39  		tag:  tag,
    40  		life: life,
    41  		st:   st,
    42  	}, nil
    43  }
    44  
    45  // Machine provides access to methods of a state.Machine through the
    46  // facade.
    47  func (st *State) Machine(tag string) (*Machine, error) {
    48  	life, err := st.life(tag)
    49  	if err != nil {
    50  		return nil, base.WrapError(err)
    51  	}
    52  	return &Machine{
    53  		tag:  tag,
    54  		life: life,
    55  		st:   st,
    56  	}, nil
    57  }
    58  
    59  // WatchForEnvironConfigChanges return a NotifyWatcher waiting for the
    60  // environment configuration to change.
    61  func (st *State) WatchForEnvironConfigChanges() (watcher.NotifyWatcher, error) {
    62  	var result params.NotifyWatchResult
    63  	err := st.caller.Call("Firewaller", "", "WatchForEnvironConfigChanges", nil, &result)
    64  	if err != nil {
    65  		return nil, base.WrapError(err)
    66  	}
    67  	if err := result.Error; err != nil {
    68  		return nil, base.WrapError(err)
    69  	}
    70  	w := watcher.NewNotifyWatcher(st.caller, result)
    71  	return w, nil
    72  }
    73  
    74  // EnvironConfig returns the current environment configuration.
    75  func (st *State) EnvironConfig() (*config.Config, error) {
    76  	var result params.EnvironConfigResult
    77  	err := st.caller.Call("Firewaller", "", "EnvironConfig", nil, &result)
    78  	if err != nil {
    79  		return nil, base.WrapError(err)
    80  	}
    81  	if err := result.Error; err != nil {
    82  		return nil, base.WrapError(err)
    83  	}
    84  	conf, err := config.New(config.NoDefaults, result.Config)
    85  	if err != nil {
    86  		return nil, mask(err)
    87  	}
    88  	return conf, nil
    89  }
    90  
    91  // WatchEnvironMachines returns a StringsWatcher that notifies of
    92  // changes to the life cycles of the top level machines in the current
    93  // environment.
    94  func (st *State) WatchEnvironMachines() (watcher.StringsWatcher, error) {
    95  	var result params.StringsWatchResult
    96  	err := st.caller.Call("Firewaller", "", "WatchEnvironMachines", nil, &result)
    97  	if err != nil {
    98  		return nil, base.WrapError(err)
    99  	}
   100  	if err := result.Error; err != nil {
   101  		return nil, result.Error
   102  	}
   103  	w := watcher.NewStringsWatcher(st.caller, result)
   104  	return w, nil
   105  }