github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/storage/poollist.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/juju/apiserver/params" 11 "github.com/juju/juju/cmd/modelcmd" 12 ) 13 14 // PoolCommandBase is a helper base structure for pool commands. 15 type PoolCommandBase struct { 16 StorageCommandBase 17 } 18 19 // PoolInfo defines the serialization behaviour of the storage pool information. 20 type PoolInfo struct { 21 Provider string `yaml:"provider" json:"provider"` 22 Attrs map[string]interface{} `yaml:"attrs,omitempty" json:"attrs,omitempty"` 23 } 24 25 func formatPoolInfo(all []params.StoragePool) map[string]PoolInfo { 26 output := make(map[string]PoolInfo) 27 for _, one := range all { 28 output[one.Name] = PoolInfo{ 29 Provider: one.Provider, 30 Attrs: one.Attrs, 31 } 32 } 33 return output 34 } 35 36 const poolListCommandDoc = ` 37 Lists storage pools. 38 The user can filter on pool type, name. 39 40 If no filter is specified, all current pools are listed. 41 If at least 1 name and type is specified, only pools that match both a name 42 AND a type from criteria are listed. 43 If only names are specified, only mentioned pools will be listed. 44 If only types are specified, all pools of the specified types will be listed. 45 46 Both pool types and names must be valid. 47 Valid pool types are pool types that are registered for Juju model. 48 49 options: 50 -m, --model (= "") 51 juju model to operate in 52 -o, --output (= "") 53 specify an output file 54 --format (= yaml) 55 specify output format (json|tabular|yaml) 56 --provider 57 pool provider type 58 --name 59 pool name 60 61 ` 62 63 // NewPoolListCommand returns a command that lists storage pools on a model 64 func NewPoolListCommand() cmd.Command { 65 cmd := &poolListCommand{} 66 cmd.newAPIFunc = func() (PoolListAPI, error) { 67 return cmd.NewStorageAPI() 68 } 69 return modelcmd.Wrap(cmd) 70 } 71 72 // poolListCommand lists storage pools. 73 type poolListCommand struct { 74 PoolCommandBase 75 newAPIFunc func() (PoolListAPI, error) 76 Providers []string 77 Names []string 78 out cmd.Output 79 } 80 81 // Init implements Command.Init. 82 func (c *poolListCommand) Init(args []string) (err error) { 83 return nil 84 } 85 86 // Info implements Command.Info. 87 func (c *poolListCommand) Info() *cmd.Info { 88 return &cmd.Info{ 89 Name: "list-storage-pools", 90 Purpose: "list storage pools", 91 Doc: poolListCommandDoc, 92 } 93 } 94 95 // SetFlags implements Command.SetFlags. 96 func (c *poolListCommand) SetFlags(f *gnuflag.FlagSet) { 97 c.StorageCommandBase.SetFlags(f) 98 f.Var(cmd.NewAppendStringsValue(&c.Providers), "provider", "only show pools of these provider types") 99 f.Var(cmd.NewAppendStringsValue(&c.Names), "name", "only show pools with these names") 100 101 c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{ 102 "yaml": cmd.FormatYaml, 103 "json": cmd.FormatJson, 104 "tabular": formatPoolListTabular, 105 }) 106 } 107 108 // Run implements Command.Run. 109 func (c *poolListCommand) Run(ctx *cmd.Context) (err error) { 110 api, err := c.newAPIFunc() 111 if err != nil { 112 return err 113 } 114 defer api.Close() 115 result, err := api.ListPools(c.Providers, c.Names) 116 if err != nil { 117 return err 118 } 119 if len(result) == 0 { 120 return nil 121 } 122 output := formatPoolInfo(result) 123 return c.out.Write(ctx, output) 124 } 125 126 // PoolListAPI defines the API methods that the storage commands use. 127 type PoolListAPI interface { 128 Close() error 129 ListPools(providers, names []string) ([]params.StoragePool, error) 130 }