github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/retrystrategy/retrystrategy.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Copyright 2016 Cloudbase Solutions
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package retrystrategy
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/juju/errors"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/api/base"
    14  	apiwatcher "github.com/juju/juju/api/watcher"
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/watcher"
    17  )
    18  
    19  // Client provides access to the retry strategy api
    20  type Client struct {
    21  	facade base.FacadeCaller
    22  }
    23  
    24  // NewClient creates a client for accessing the retry strategy api
    25  func NewClient(apiCaller base.APICaller) *Client {
    26  	return &Client{base.NewFacadeCaller(apiCaller, "RetryStrategy")}
    27  }
    28  
    29  // RetryStrategy returns the configuration for the agent specified by the agentTag.
    30  func (c *Client) RetryStrategy(agentTag names.Tag) (params.RetryStrategy, error) {
    31  	var results params.RetryStrategyResults
    32  	args := params.Entities{
    33  		Entities: []params.Entity{{Tag: agentTag.String()}},
    34  	}
    35  	err := c.facade.FacadeCall("RetryStrategy", args, &results)
    36  	if err != nil {
    37  		return params.RetryStrategy{}, errors.Trace(err)
    38  	}
    39  	if len(results.Results) != 1 {
    40  		return params.RetryStrategy{}, fmt.Errorf("expected 1 result, got %d", len(results.Results))
    41  	}
    42  	result := results.Results[0]
    43  	if result.Error != nil {
    44  		return params.RetryStrategy{}, errors.Trace(result.Error)
    45  	}
    46  	return *result.Result, nil
    47  }
    48  
    49  // WatchRetryStrategy returns a notify watcher that looks for changes in the
    50  // retry strategy config for the agent specified by agentTag
    51  // Right now only the boolean that decides whether we retry can be modified.
    52  func (c *Client) WatchRetryStrategy(agentTag names.Tag) (watcher.NotifyWatcher, error) {
    53  	var results params.NotifyWatchResults
    54  	args := params.Entities{
    55  		Entities: []params.Entity{{Tag: agentTag.String()}},
    56  	}
    57  	err := c.facade.FacadeCall("WatchRetryStrategy", args, &results)
    58  	if err != nil {
    59  		return nil, errors.Trace(err)
    60  	}
    61  	if len(results.Results) != 1 {
    62  		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
    63  	}
    64  	result := results.Results[0]
    65  	if result.Error != nil {
    66  		return nil, errors.Trace(result.Error)
    67  	}
    68  	w := apiwatcher.NewNotifyWatcher(c.facade.RawAPICaller(), result)
    69  	return w, nil
    70  }