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