github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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 "github.com/juju/juju/state/api/base" 8 "github.com/juju/juju/state/api/common" 9 "github.com/juju/juju/state/api/params" 10 "github.com/juju/juju/state/api/watcher" 11 ) 12 13 const firewallerFacade = "Firewaller" 14 15 // State provides access to the Firewaller API facade. 16 type State struct { 17 caller base.Caller 18 *common.EnvironWatcher 19 } 20 21 func (st *State) call(method string, params, result interface{}) error { 22 return st.caller.Call(firewallerFacade, "", method, params, result) 23 } 24 25 // NewState creates a new client-side Firewaller facade. 26 func NewState(caller base.Caller) *State { 27 return &State{ 28 caller: caller, 29 EnvironWatcher: common.NewEnvironWatcher(firewallerFacade, caller), 30 } 31 } 32 33 // life requests the life cycle of the given entity from the server. 34 func (st *State) life(tag string) (params.Life, error) { 35 return common.Life(st.caller, firewallerFacade, tag) 36 } 37 38 // Unit provides access to methods of a state.Unit through the facade. 39 func (st *State) Unit(tag string) (*Unit, error) { 40 life, err := st.life(tag) 41 if err != nil { 42 return nil, err 43 } 44 return &Unit{ 45 tag: tag, 46 life: life, 47 st: st, 48 }, nil 49 } 50 51 // Machine provides access to methods of a state.Machine through the 52 // facade. 53 func (st *State) Machine(tag string) (*Machine, error) { 54 life, err := st.life(tag) 55 if err != nil { 56 return nil, err 57 } 58 return &Machine{ 59 tag: tag, 60 life: life, 61 st: st, 62 }, nil 63 } 64 65 // WatchEnvironMachines returns a StringsWatcher that notifies of 66 // changes to the life cycles of the top level machines in the current 67 // environment. 68 func (st *State) WatchEnvironMachines() (watcher.StringsWatcher, error) { 69 var result params.StringsWatchResult 70 err := st.call("WatchEnvironMachines", nil, &result) 71 if err != nil { 72 return nil, err 73 } 74 if err := result.Error; err != nil { 75 return nil, result.Error 76 } 77 w := watcher.NewStringsWatcher(st.caller, result) 78 return w, nil 79 }