github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/storage/show.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/gnuflag"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  	"github.com/juju/juju/cmd/output"
    15  )
    16  
    17  // NewShowCommand returns a command that shows storage details
    18  // on the specified machine
    19  func NewShowCommand() cmd.Command {
    20  	cmd := &showCommand{}
    21  	cmd.newAPIFunc = func() (StorageShowAPI, error) {
    22  		return cmd.NewStorageAPI()
    23  	}
    24  	return modelcmd.Wrap(cmd)
    25  }
    26  
    27  const showCommandDoc = `
    28  Show extended information about storage instances.
    29  Storage instances to display are specified by storage ids.
    30  
    31  * note use of positional arguments
    32  `
    33  
    34  // showCommand attempts to release storage instance.
    35  type showCommand struct {
    36  	StorageCommandBase
    37  	ids        []string
    38  	out        cmd.Output
    39  	newAPIFunc func() (StorageShowAPI, error)
    40  }
    41  
    42  // Init implements Command.Init.
    43  func (c *showCommand) Init(args []string) (err error) {
    44  	if len(args) < 1 {
    45  		return errors.New("must specify storage id(s)")
    46  	}
    47  	c.ids = args
    48  	return nil
    49  }
    50  
    51  // Info implements Command.Info.
    52  func (c *showCommand) Info() *cmd.Info {
    53  	return &cmd.Info{
    54  		Name:    "show-storage",
    55  		Args:    "<storage ID> [...]",
    56  		Purpose: "Shows storage instance information.",
    57  		Doc:     showCommandDoc,
    58  	}
    59  }
    60  
    61  // SetFlags implements Command.SetFlags.
    62  func (c *showCommand) SetFlags(f *gnuflag.FlagSet) {
    63  	c.StorageCommandBase.SetFlags(f)
    64  	c.out.AddFlags(f, "yaml", output.DefaultFormatters)
    65  }
    66  
    67  // Run implements Command.Run.
    68  func (c *showCommand) Run(ctx *cmd.Context) (err error) {
    69  	api, err := c.newAPIFunc()
    70  	if err != nil {
    71  		return err
    72  	}
    73  	defer api.Close()
    74  
    75  	tags, err := c.getStorageTags()
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	results, err := api.StorageDetails(tags)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	var errs params.ErrorResults
    86  	var valid []params.StorageDetails
    87  	for _, result := range results {
    88  		if result.Error != nil {
    89  			errs.Results = append(errs.Results, params.ErrorResult{result.Error})
    90  			continue
    91  		}
    92  		valid = append(valid, *result.Result)
    93  	}
    94  	if len(errs.Results) > 0 {
    95  		return errs.Combine()
    96  	}
    97  
    98  	output, err := formatStorageDetails(valid)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	return c.out.Write(ctx, output)
   103  }
   104  
   105  func (c *showCommand) getStorageTags() ([]names.StorageTag, error) {
   106  	tags := make([]names.StorageTag, len(c.ids))
   107  	for i, id := range c.ids {
   108  		if !names.IsValidStorage(id) {
   109  			return nil, errors.Errorf("invalid storage id %v", id)
   110  		}
   111  		tags[i] = names.NewStorageTag(id)
   112  	}
   113  	return tags, nil
   114  }
   115  
   116  // StorageAPI defines the API methods that the storage commands use.
   117  type StorageShowAPI interface {
   118  	Close() error
   119  	StorageDetails(tags []names.StorageTag) ([]params.StorageDetailsResult, error)
   120  }