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