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