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