github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	"github.com/juju/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  The user can filter on pool type, name.
    38  
    39  If no filter is specified, all current pools are listed.
    40  If at least 1 name and type is specified, only pools that match both a name
    41  AND a type from criteria are listed.
    42  If only names are specified, only mentioned pools will be listed.
    43  If only types are specified, all pools of the specified types will be listed.
    44  
    45  Both pool types and names must be valid.
    46  Valid pool types are pool types that are registered for Juju model.
    47  `
    48  
    49  // NewPoolListCommand returns a command that lists storage pools on a model
    50  func NewPoolListCommand() cmd.Command {
    51  	cmd := &poolListCommand{}
    52  	cmd.newAPIFunc = func() (PoolListAPI, error) {
    53  		return cmd.NewStorageAPI()
    54  	}
    55  	return modelcmd.Wrap(cmd)
    56  }
    57  
    58  // poolListCommand lists storage pools.
    59  type poolListCommand struct {
    60  	PoolCommandBase
    61  	newAPIFunc func() (PoolListAPI, error)
    62  	Providers  []string
    63  	Names      []string
    64  	out        cmd.Output
    65  }
    66  
    67  // Init implements Command.Init.
    68  func (c *poolListCommand) Init(args []string) (err error) {
    69  	return nil
    70  }
    71  
    72  // Info implements Command.Info.
    73  func (c *poolListCommand) Info() *cmd.Info {
    74  	return &cmd.Info{
    75  		Name:    "storage-pools",
    76  		Purpose: "List storage pools.",
    77  		Doc:     poolListCommandDoc,
    78  		Aliases: []string{"list-storage-pools"},
    79  	}
    80  }
    81  
    82  // SetFlags implements Command.SetFlags.
    83  func (c *poolListCommand) SetFlags(f *gnuflag.FlagSet) {
    84  	c.StorageCommandBase.SetFlags(f)
    85  	f.Var(cmd.NewAppendStringsValue(&c.Providers), "provider", "Only show pools of these provider types")
    86  	f.Var(cmd.NewAppendStringsValue(&c.Names), "name", "Only show pools with these names")
    87  
    88  	c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
    89  		"yaml":    cmd.FormatYaml,
    90  		"json":    cmd.FormatJson,
    91  		"tabular": formatPoolListTabular,
    92  	})
    93  }
    94  
    95  // Run implements Command.Run.
    96  func (c *poolListCommand) Run(ctx *cmd.Context) (err error) {
    97  	api, err := c.newAPIFunc()
    98  	if err != nil {
    99  		return err
   100  	}
   101  	defer api.Close()
   102  	result, err := api.ListPools(c.Providers, c.Names)
   103  	if err != nil {
   104  		return err
   105  	}
   106  	if len(result) == 0 {
   107  		ctx.Infof("No storage pools to display.")
   108  		return nil
   109  	}
   110  	output := formatPoolInfo(result)
   111  	return c.out.Write(ctx, output)
   112  }
   113  
   114  // PoolListAPI defines the API methods that the storage commands use.
   115  type PoolListAPI interface {
   116  	Close() error
   117  	ListPools(providers, names []string) ([]params.StoragePool, error)
   118  }