github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/storage/volumelist.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 "fmt" 8 9 "github.com/juju/cmd" 10 "launchpad.net/gnuflag" 11 12 "github.com/juju/juju/apiserver/params" 13 ) 14 15 const VolumeListCommandDoc = ` 16 List volumes (disks) in the environment. 17 18 options: 19 -e, --environment (= "") 20 juju environment to operate in 21 -o, --output (= "") 22 specify an output file 23 [machine] 24 machine ids for filtering the list 25 26 ` 27 28 // VolumeListCommand lists storage volumes. 29 type VolumeListCommand struct { 30 VolumeCommandBase 31 Ids []string 32 out cmd.Output 33 } 34 35 // Init implements Command.Init. 36 func (c *VolumeListCommand) Init(args []string) (err error) { 37 c.Ids = args 38 return nil 39 } 40 41 // Info implements Command.Info. 42 func (c *VolumeListCommand) Info() *cmd.Info { 43 return &cmd.Info{ 44 Name: "list", 45 Purpose: "list storage volumes", 46 Doc: VolumeListCommandDoc, 47 } 48 } 49 50 // SetFlags implements Command.SetFlags. 51 func (c *VolumeListCommand) SetFlags(f *gnuflag.FlagSet) { 52 c.StorageCommandBase.SetFlags(f) 53 54 c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{ 55 "yaml": cmd.FormatYaml, 56 "json": cmd.FormatJson, 57 "tabular": formatVolumeListTabular, 58 }) 59 } 60 61 // Run implements Command.Run. 62 func (c *VolumeListCommand) Run(ctx *cmd.Context) (err error) { 63 api, err := getVolumeListAPI(c) 64 if err != nil { 65 return err 66 } 67 defer api.Close() 68 69 found, err := api.ListVolumes(c.Ids) 70 if err != nil { 71 return err 72 } 73 // filter out valid output, if any 74 var valid []params.VolumeDetailsResult 75 for _, one := range found { 76 if one.Error == nil { 77 valid = append(valid, one) 78 continue 79 } 80 // display individual error 81 fmt.Fprintf(ctx.Stderr, "%v\n", one.Error) 82 } 83 if len(valid) == 0 { 84 return nil 85 } 86 87 info, err := convertToVolumeInfo(valid) 88 if err != nil { 89 return err 90 } 91 92 var output interface{} 93 switch c.out.Name() { 94 case "json", "yaml": 95 output = map[string]map[string]VolumeInfo{"volumes": info} 96 default: 97 output = info 98 } 99 return c.out.Write(ctx, output) 100 } 101 102 var getVolumeListAPI = (*VolumeListCommand).getVolumeListAPI 103 104 // VolumeListAPI defines the API methods that the volume list command use. 105 type VolumeListAPI interface { 106 Close() error 107 ListVolumes(machines []string) ([]params.VolumeDetailsResult, error) 108 } 109 110 func (c *VolumeListCommand) getVolumeListAPI() (VolumeListAPI, error) { 111 return c.NewStorageAPI() 112 }