github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/firewaller/service.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 "fmt" 8 9 "gopkg.in/juju/names.v2" 10 11 "github.com/juju/juju/api/common" 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/watcher" 14 ) 15 16 // Service represents the state of a service. 17 type Application struct { 18 st *State 19 tag names.ApplicationTag 20 life params.Life 21 } 22 23 // Name returns the service name. 24 func (s *Application) Name() string { 25 return s.tag.Id() 26 } 27 28 // Tag returns the service tag. 29 func (s *Application) Tag() names.ApplicationTag { 30 return s.tag 31 } 32 33 // Watch returns a watcher for observing changes to a service. 34 func (s *Application) Watch() (watcher.NotifyWatcher, error) { 35 return common.Watch(s.st.facade, s.tag) 36 } 37 38 // Life returns the service's current life state. 39 func (s *Application) Life() params.Life { 40 return s.life 41 } 42 43 // Refresh refreshes the contents of the Service from the underlying 44 // state. 45 func (s *Application) Refresh() error { 46 life, err := s.st.life(s.tag) 47 if err != nil { 48 return err 49 } 50 s.life = life 51 return nil 52 } 53 54 // IsExposed returns whether this service is exposed. The explicitly 55 // open ports (with open-port) for exposed services may be accessed 56 // from machines outside of the local deployment network. 57 // 58 // NOTE: This differs from state.Service.IsExposed() by returning 59 // an error as well, because it needs to make an API call. 60 func (s *Application) IsExposed() (bool, error) { 61 var results params.BoolResults 62 args := params.Entities{ 63 Entities: []params.Entity{{Tag: s.tag.String()}}, 64 } 65 err := s.st.facade.FacadeCall("GetExposed", args, &results) 66 if err != nil { 67 return false, err 68 } 69 if len(results.Results) != 1 { 70 return false, fmt.Errorf("expected 1 result, got %d", len(results.Results)) 71 } 72 result := results.Results[0] 73 if result.Error != nil { 74 return false, result.Error 75 } 76 return result.Result, nil 77 }