github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/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 "launchpad.net/juju-core/names" 10 "launchpad.net/juju-core/state/api/params" 11 "launchpad.net/juju-core/state/api/watcher" 12 ) 13 14 // Service represents the state of a service. 15 type Service struct { 16 st *State 17 tag string 18 life params.Life 19 } 20 21 // Name returns the service name. 22 func (s *Service) Name() string { 23 _, serviceName, err := names.ParseTag(s.tag, names.ServiceTagKind) 24 if err != nil { 25 panic(fmt.Sprintf("%q is not a valid service tag", s.tag)) 26 } 27 return serviceName 28 } 29 30 // Watch returns a watcher for observing changes to a service. 31 func (s *Service) Watch() (watcher.NotifyWatcher, error) { 32 var results params.NotifyWatchResults 33 args := params.Entities{ 34 Entities: []params.Entity{{Tag: s.tag}}, 35 } 36 err := s.st.caller.Call("Firewaller", "", "Watch", args, &results) 37 if err != nil { 38 return nil, err 39 } 40 if len(results.Results) != 1 { 41 return nil, fmt.Errorf("expected one result, got %d", len(results.Results)) 42 } 43 result := results.Results[0] 44 if result.Error != nil { 45 return nil, result.Error 46 } 47 w := watcher.NewNotifyWatcher(s.st.caller, result) 48 return w, nil 49 } 50 51 // Life returns the service's current life state. 52 func (s *Service) Life() params.Life { 53 return s.life 54 } 55 56 // Refresh refreshes the contents of the Service from the underlying 57 // state. 58 func (s *Service) Refresh() error { 59 life, err := s.st.life(s.tag) 60 if err != nil { 61 return err 62 } 63 s.life = life 64 return nil 65 } 66 67 // IsExposed returns whether this service is exposed. The explicitly 68 // open ports (with open-port) for exposed services may be accessed 69 // from machines outside of the local deployment network. 70 // 71 // NOTE: This differs from state.Service.IsExposed() by returning 72 // an error as well, because it needs to make an API call. 73 func (s *Service) IsExposed() (bool, error) { 74 var results params.BoolResults 75 args := params.Entities{ 76 Entities: []params.Entity{{Tag: s.tag}}, 77 } 78 err := s.st.caller.Call("Firewaller", "", "GetExposed", args, &results) 79 if err != nil { 80 return false, err 81 } 82 if len(results.Results) != 1 { 83 return false, fmt.Errorf("expected one result, got %d", len(results.Results)) 84 } 85 result := results.Results[0] 86 if result.Error != nil { 87 return false, result.Error 88 } 89 return result.Result, nil 90 }