github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/caasfirewaller/client.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasfirewaller
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/juju/api/common"
     9  	"gopkg.in/juju/names.v2"
    10  
    11  	"github.com/juju/juju/api/base"
    12  	apiwatcher "github.com/juju/juju/api/watcher"
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/core/application"
    15  	"github.com/juju/juju/core/life"
    16  	"github.com/juju/juju/core/watcher"
    17  )
    18  
    19  // Client allows access to the CAAS firewaller API endpoint.
    20  type Client struct {
    21  	facade base.FacadeCaller
    22  }
    23  
    24  // NewClient returns a client used to access the CAAS unit provisioner API.
    25  func NewClient(caller base.APICaller) *Client {
    26  	facadeCaller := base.NewFacadeCaller(caller, "CAASFirewaller")
    27  	return &Client{
    28  		facade: facadeCaller,
    29  	}
    30  }
    31  
    32  func applicationTag(application string) (names.ApplicationTag, error) {
    33  	if !names.IsValidApplication(application) {
    34  		return names.ApplicationTag{}, errors.NotValidf("application name %q", application)
    35  	}
    36  	return names.NewApplicationTag(application), nil
    37  }
    38  
    39  func entities(tags ...names.Tag) params.Entities {
    40  	entities := params.Entities{
    41  		Entities: make([]params.Entity, len(tags)),
    42  	}
    43  	for i, tag := range tags {
    44  		entities.Entities[i].Tag = tag.String()
    45  	}
    46  	return entities
    47  }
    48  
    49  // WatchApplications returns a StringsWatcher that notifies of
    50  // changes to the lifecycles of CAAS applications in the current model.
    51  func (c *Client) WatchApplications() (watcher.StringsWatcher, error) {
    52  	var result params.StringsWatchResult
    53  	if err := c.facade.FacadeCall("WatchApplications", nil, &result); err != nil {
    54  		return nil, err
    55  	}
    56  	if err := result.Error; err != nil {
    57  		return nil, result.Error
    58  	}
    59  	w := apiwatcher.NewStringsWatcher(c.facade.RawAPICaller(), result)
    60  	return w, nil
    61  }
    62  
    63  // WatchApplication returns a NotifyWatcher that notifies of
    64  // changes to the application in the current model.
    65  func (c *Client) WatchApplication(appName string) (watcher.NotifyWatcher, error) {
    66  	appTag, err := applicationTag(appName)
    67  	if err != nil {
    68  		return nil, errors.Trace(err)
    69  	}
    70  	return common.Watch(c.facade, "Watch", appTag)
    71  }
    72  
    73  // Life returns the lifecycle state for the specified CAAS application
    74  // in the current model.
    75  func (c *Client) Life(appName string) (life.Value, error) {
    76  	appTag, err := applicationTag(appName)
    77  	if err != nil {
    78  		return "", errors.Trace(err)
    79  	}
    80  	args := entities(appTag)
    81  
    82  	var results params.LifeResults
    83  	if err := c.facade.FacadeCall("Life", args, &results); err != nil {
    84  		return "", err
    85  	}
    86  	if n := len(results.Results); n != 1 {
    87  		return "", errors.Errorf("expected 1 result, got %d", n)
    88  	}
    89  	if err := results.Results[0].Error; err != nil {
    90  		return "", maybeNotFound(err)
    91  	}
    92  	return life.Value(results.Results[0].Life), nil
    93  }
    94  
    95  // ApplicationConfig returns the config for the specified application.
    96  func (c *Client) ApplicationConfig(applicationName string) (application.ConfigAttributes, error) {
    97  	var results params.ApplicationGetConfigResults
    98  	args := params.Entities{
    99  		Entities: []params.Entity{{Tag: names.NewApplicationTag(applicationName).String()}},
   100  	}
   101  	err := c.facade.FacadeCall("ApplicationsConfig", args, &results)
   102  	if err != nil {
   103  		return nil, errors.Trace(err)
   104  	}
   105  	if len(results.Results) != len(args.Entities) {
   106  		return nil, errors.Errorf("expected %d result(s), got %d", len(args.Entities), len(results.Results))
   107  	}
   108  	return application.ConfigAttributes(results.Results[0].Config), nil
   109  }
   110  
   111  // IsExposed returns whether the specified CAAS application
   112  // in the current model is exposed.
   113  func (c *Client) IsExposed(appName string) (bool, error) {
   114  	appTag, err := applicationTag(appName)
   115  	if err != nil {
   116  		return false, errors.Trace(err)
   117  	}
   118  	args := entities(appTag)
   119  
   120  	var results params.BoolResults
   121  	if err := c.facade.FacadeCall("IsExposed", args, &results); err != nil {
   122  		return false, err
   123  	}
   124  	if n := len(results.Results); n != 1 {
   125  		return false, errors.Errorf("expected 1 result, got %d", n)
   126  	}
   127  	if err := results.Results[0].Error; err != nil {
   128  		return false, maybeNotFound(err)
   129  	}
   130  	return results.Results[0].Result, nil
   131  }
   132  
   133  // maybeNotFound returns an error satisfying errors.IsNotFound
   134  // if the supplied error has a CodeNotFound error.
   135  func maybeNotFound(err *params.Error) error {
   136  	if err == nil || !params.IsCodeNotFound(err) {
   137  		return err
   138  	}
   139  	return errors.NewNotFound(err, "")
   140  }