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