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