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

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package action
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/api/base"
    10  	"github.com/juju/juju/apiserver/params"
    11  )
    12  
    13  // Client provides access to the action facade.
    14  type Client struct {
    15  	base.ClientFacade
    16  	facade base.FacadeCaller
    17  }
    18  
    19  // NewClient returns a new actions client.
    20  func NewClient(st base.APICallCloser) *Client {
    21  	frontend, backend := base.NewClientFacade(st, "Action")
    22  	return &Client{ClientFacade: frontend, facade: backend}
    23  }
    24  
    25  // Actions takes a list of ActionTags, and returns the full
    26  // Action for each ID.
    27  func (c *Client) Actions(arg params.Entities) (params.ActionResults, error) {
    28  	results := params.ActionResults{}
    29  	err := c.facade.FacadeCall("Actions", arg, &results)
    30  	return results, err
    31  }
    32  
    33  // FindActionTagsByPrefix takes a list of string prefixes and finds
    34  // corresponding ActionTags that match that prefix.
    35  func (c *Client) FindActionTagsByPrefix(arg params.FindTags) (params.FindTagsResults, error) {
    36  	results := params.FindTagsResults{}
    37  	err := c.facade.FacadeCall("FindActionTagsByPrefix", arg, &results)
    38  	return results, err
    39  }
    40  
    41  // Enqueue takes a list of Actions and queues them up to be executed by
    42  // the designated ActionReceiver, returning the params.Action for each
    43  // queued Action, or an error if there was a problem queueing up the
    44  // Action.
    45  func (c *Client) Enqueue(arg params.Actions) (params.ActionResults, error) {
    46  	results := params.ActionResults{}
    47  	err := c.facade.FacadeCall("Enqueue", arg, &results)
    48  	return results, err
    49  }
    50  
    51  // FindActionsByNames takes a list of action names and returns actions for
    52  // every name.
    53  func (c *Client) FindActionsByNames(arg params.FindActionsByNames) (params.ActionsByNames, error) {
    54  	results := params.ActionsByNames{}
    55  	err := c.facade.FacadeCall("FindActionsByNames", arg, &results)
    56  	return results, err
    57  }
    58  
    59  // ListAll takes a list of Entities representing ActionReceivers and returns
    60  // all of the Actions that have been queued or run by each of those
    61  // Entities.
    62  func (c *Client) ListAll(arg params.Entities) (params.ActionsByReceivers, error) {
    63  	results := params.ActionsByReceivers{}
    64  	err := c.facade.FacadeCall("ListAll", arg, &results)
    65  	return results, err
    66  }
    67  
    68  // ListPending takes a list of Entities representing ActionReceivers
    69  // and returns all of the Actions that are queued for each of those
    70  // Entities.
    71  func (c *Client) ListPending(arg params.Entities) (params.ActionsByReceivers, error) {
    72  	results := params.ActionsByReceivers{}
    73  	err := c.facade.FacadeCall("ListPending", arg, &results)
    74  	return results, err
    75  }
    76  
    77  // ListCompleted takes a list of Entities representing ActionReceivers
    78  // and returns all of the Actions that have been run on each of those
    79  // Entities.
    80  func (c *Client) ListCompleted(arg params.Entities) (params.ActionsByReceivers, error) {
    81  	results := params.ActionsByReceivers{}
    82  	err := c.facade.FacadeCall("ListCompleted", arg, &results)
    83  	return results, err
    84  }
    85  
    86  // Cancel attempts to cancel a queued up Action from running.
    87  func (c *Client) Cancel(arg params.Actions) (params.ActionResults, error) {
    88  	results := params.ActionResults{}
    89  	err := c.facade.FacadeCall("Cancel", arg, &results)
    90  	return results, err
    91  }
    92  
    93  // applicationsCharmActions is a batched query for the charm.Actions for a slice
    94  // of services by Entity.
    95  func (c *Client) applicationsCharmActions(arg params.Entities) (params.ApplicationsCharmActionsResults, error) {
    96  	results := params.ApplicationsCharmActionsResults{}
    97  	err := c.facade.FacadeCall("ApplicationsCharmsActions", arg, &results)
    98  	return results, err
    99  }
   100  
   101  // ApplicationCharmActions is a single query which uses ApplicationsCharmsActions to
   102  // get the charm.Actions for a single Service by tag.
   103  func (c *Client) ApplicationCharmActions(arg params.Entity) (map[string]params.ActionSpec, error) {
   104  	tags := params.Entities{Entities: []params.Entity{{Tag: arg.Tag}}}
   105  	results, err := c.applicationsCharmActions(tags)
   106  	if err != nil {
   107  		return nil, errors.Trace(err)
   108  	}
   109  	if len(results.Results) != 1 {
   110  		return nil, errors.Errorf("%d results, expected 1", len(results.Results))
   111  	}
   112  	result := results.Results[0]
   113  	if result.Error != nil {
   114  		return nil, result.Error
   115  	}
   116  	if result.ApplicationTag != arg.Tag {
   117  		return nil, errors.Errorf("action results received for wrong application %q", result.ApplicationTag)
   118  	}
   119  	return result.Actions, nil
   120  }