github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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  	"launchpad.net/gnuflag"
     9  
    10  	"github.com/juju/errors"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/names"
    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
    26  [space separated storage ids]
    27  `
    28  
    29  // ShowCommand attempts to release storage instance.
    30  type ShowCommand struct {
    31  	StorageCommandBase
    32  	ids []string
    33  	out cmd.Output
    34  }
    35  
    36  // Init implements Command.Init.
    37  func (c *ShowCommand) Init(args []string) (err error) {
    38  	if len(args) < 1 {
    39  		return errors.New("must specify storage id(s)")
    40  	}
    41  	c.ids = args
    42  	return nil
    43  }
    44  
    45  // Info implements Command.Info.
    46  func (c *ShowCommand) Info() *cmd.Info {
    47  	return &cmd.Info{
    48  		Name:    "show",
    49  		Purpose: "shows storage instance",
    50  		Doc:     ShowCommandDoc,
    51  	}
    52  }
    53  
    54  // SetFlags implements Command.SetFlags.
    55  func (c *ShowCommand) SetFlags(f *gnuflag.FlagSet) {
    56  	c.StorageCommandBase.SetFlags(f)
    57  	c.out.AddFlags(f, "yaml", cmd.DefaultFormatters)
    58  }
    59  
    60  // StorageInfo defines the serialization behaviour of the storage information.
    61  type StorageInfo struct {
    62  	StorageTag    string   `yaml:"storage-tag" json:"storage-tag"`
    63  	StorageName   string   `yaml:"storage-name" json:"storage-name"`
    64  	OwnerTag      string   `yaml:"owner-tag" json:"owner-tag"`
    65  	Location      string   `yaml:"location,omitempty" json:"location,omitempty"`
    66  	AvailableSize uint64   `yaml:"available-size" json:"available-size"`
    67  	TotalSize     uint64   `yaml:"total-size" json:"total-size"`
    68  	Tags          []string `yaml:"tags,omitempty" json:"tags,omitempty"`
    69  }
    70  
    71  // Run implements Command.Run.
    72  func (c *ShowCommand) Run(ctx *cmd.Context) (err error) {
    73  	api, err := getStorageShowAPI(c)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	defer api.Close()
    78  
    79  	result, err := api.Show(c.getStorageTags())
    80  	if err != nil {
    81  		return err
    82  	}
    83  	output := c.apiStoragesToInstanceSlice(result)
    84  	return c.out.Write(ctx, output)
    85  }
    86  
    87  func (c *ShowCommand) getStorageTags() []names.StorageTag {
    88  	tags := make([]names.StorageTag, len(c.ids))
    89  	for i, id := range c.ids {
    90  		tags[i] = names.NewStorageTag(id)
    91  	}
    92  	return tags
    93  }
    94  
    95  var (
    96  	getStorageShowAPI = (*ShowCommand).getStorageShowAPI
    97  )
    98  
    99  // StorageAPI defines the API methods that the storage commands use.
   100  type StorageShowAPI interface {
   101  	Close() error
   102  	Show(tags []names.StorageTag) ([]params.StorageInstance, error)
   103  }
   104  
   105  func (c *ShowCommand) getStorageShowAPI() (StorageShowAPI, error) {
   106  	return c.NewStorageAPI()
   107  }
   108  
   109  func (c *ShowCommand) apiStoragesToInstanceSlice(all []params.StorageInstance) []StorageInfo {
   110  	var output []StorageInfo
   111  	for _, one := range all {
   112  		outInfo := StorageInfo{
   113  			StorageTag:    one.StorageTag,
   114  			StorageName:   one.StorageName,
   115  			Location:      one.Location,
   116  			OwnerTag:      one.OwnerTag,
   117  			AvailableSize: one.AvailableSize,
   118  			TotalSize:     one.TotalSize,
   119  			Tags:          one.Tags,
   120  		}
   121  		output = append(output, outInfo)
   122  	}
   123  	return output
   124  }