github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  // ListAll takes a list of Entities representing ActionReceivers and returns
    53  // all of the Actions that have been queued or run by each of those
    54  // Entities.
    55  func (c *Client) ListAll(arg params.Entities) (params.ActionsByReceivers, error) {
    56  	results := params.ActionsByReceivers{}
    57  	err := c.facade.FacadeCall("ListAll", arg, &results)
    58  	return results, err
    59  }
    60  
    61  // ListPending takes a list of Entities representing ActionReceivers
    62  // and returns all of the Actions that are queued for each of those
    63  // Entities.
    64  func (c *Client) ListPending(arg params.Entities) (params.ActionsByReceivers, error) {
    65  	results := params.ActionsByReceivers{}
    66  	err := c.facade.FacadeCall("ListPending", arg, &results)
    67  	return results, err
    68  }
    69  
    70  // ListCompleted takes a list of Entities representing ActionReceivers
    71  // and returns all of the Actions that have been run on each of those
    72  // Entities.
    73  func (c *Client) ListCompleted(arg params.Entities) (params.ActionsByReceivers, error) {
    74  	results := params.ActionsByReceivers{}
    75  	err := c.facade.FacadeCall("ListCompleted", arg, &results)
    76  	return results, err
    77  }
    78  
    79  // Cancel attempts to cancel a queued up Action from running.
    80  func (c *Client) Cancel(arg params.Actions) (params.ActionResults, error) {
    81  	results := params.ActionResults{}
    82  	err := c.facade.FacadeCall("Cancel", arg, &results)
    83  	return results, err
    84  }
    85  
    86  // servicesCharmActions is a batched query for the charm.Actions for a slice
    87  // of services by Entity.
    88  func (c *Client) servicesCharmActions(arg params.Entities) (params.ServicesCharmActionsResults, error) {
    89  	results := params.ServicesCharmActionsResults{}
    90  	err := c.facade.FacadeCall("ServicesCharmActions", arg, &results)
    91  	return results, err
    92  }
    93  
    94  // ServiceCharmActions is a single query which uses ServicesCharmActions to
    95  // get the charm.Actions for a single Service by tag.
    96  func (c *Client) ServiceCharmActions(arg params.Entity) (*charm.Actions, error) {
    97  	none := &charm.Actions{}
    98  	tags := params.Entities{Entities: []params.Entity{{Tag: arg.Tag}}}
    99  	results, err := c.servicesCharmActions(tags)
   100  	if err != nil {
   101  		return none, err
   102  	}
   103  	if len(results.Results) != 1 {
   104  		return none, errors.Errorf("%d results, expected 1", len(results.Results))
   105  	}
   106  	result := results.Results[0]
   107  	if result.Error != nil {
   108  		return none, result.Error
   109  	}
   110  	if result.ServiceTag != arg.Tag {
   111  		return none, errors.Errorf("action results received for wrong service %q", result.ServiceTag)
   112  	}
   113  	return result.Actions, nil
   114  }