github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/action/status.go (about)

     1  // Copyright 2014-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package action
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	errors "github.com/juju/errors"
     9  	"github.com/juju/names"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  )
    14  
    15  // StatusCommand shows the status of an Action by ID.
    16  type StatusCommand struct {
    17  	ActionCommandBase
    18  	out         cmd.Output
    19  	requestedId string
    20  }
    21  
    22  const statusDoc = `
    23  Show the status of Actions matching given ID, partial ID prefix, or all Actions if no ID is supplied.
    24  `
    25  
    26  // Set up the output.
    27  func (c *StatusCommand) SetFlags(f *gnuflag.FlagSet) {
    28  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    29  }
    30  
    31  func (c *StatusCommand) Info() *cmd.Info {
    32  	return &cmd.Info{
    33  		Name:    "status",
    34  		Args:    "[<action ID>|<action ID prefix>]",
    35  		Purpose: "show results of all actions filtered by optional ID prefix",
    36  		Doc:     statusDoc,
    37  	}
    38  }
    39  
    40  func (c *StatusCommand) Init(args []string) error {
    41  	switch len(args) {
    42  	case 0:
    43  		c.requestedId = ""
    44  		return nil
    45  	case 1:
    46  		c.requestedId = args[0]
    47  		return nil
    48  	default:
    49  		return cmd.CheckEmpty(args[1:])
    50  	}
    51  }
    52  
    53  func (c *StatusCommand) Run(ctx *cmd.Context) error {
    54  	api, err := c.NewActionAPIClient()
    55  	if err != nil {
    56  		return err
    57  	}
    58  	defer api.Close()
    59  
    60  	actionTags, err := getActionTagsByPrefix(api, c.requestedId)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if len(actionTags) < 1 {
    66  		if len(c.requestedId) == 0 {
    67  			return errors.Errorf("no actions found")
    68  		} else {
    69  			return errors.Errorf("no actions found matching prefix %q", c.requestedId)
    70  		}
    71  	}
    72  
    73  	entities := []params.Entity{}
    74  	for _, tag := range actionTags {
    75  		entities = append(entities, params.Entity{tag.String()})
    76  	}
    77  
    78  	actions, err := api.Actions(params.Entities{Entities: entities})
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	if len(actions.Results) < 1 {
    84  		return errors.Errorf("identifier %q matched action(s) %v, but found no results", c.requestedId, actionTags)
    85  	}
    86  
    87  	return c.out.Write(ctx, resultsToMap(actions.Results))
    88  }
    89  
    90  func resultsToMap(results []params.ActionResult) map[string]interface{} {
    91  	items := []map[string]interface{}{}
    92  	for _, item := range results {
    93  		items = append(items, resultToMap(item))
    94  	}
    95  	return map[string]interface{}{"actions": items}
    96  }
    97  
    98  func resultToMap(result params.ActionResult) map[string]interface{} {
    99  	item := map[string]interface{}{}
   100  	if result.Error != nil {
   101  		item["error"] = result.Error.Error()
   102  	}
   103  	if result.Action != nil {
   104  		atag, err := names.ParseActionTag(result.Action.Tag)
   105  		if err != nil {
   106  			item["id"] = result.Action.Tag
   107  		} else {
   108  			item["id"] = atag.Id()
   109  		}
   110  
   111  		rtag, err := names.ParseUnitTag(result.Action.Receiver)
   112  		if err != nil {
   113  			item["unit"] = result.Action.Receiver
   114  		} else {
   115  			item["unit"] = rtag.Id()
   116  		}
   117  
   118  	}
   119  	item["status"] = result.Status
   120  	return item
   121  }