github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  )
    12  
    13  const PoolListCommandDoc = `
    14  Lists storage pools.
    15  The user can filter on pool type, name.
    16  
    17  If no filter is specified, all current pools are listed.
    18  If at least 1 name and type is specified, only pools that match both a name
    19  AND a type from criteria are listed.
    20  If only names are specified, only mentioned pools will be listed.
    21  If only types are specified, all pools of the specified types will be listed.
    22  
    23  Both pool types and names must be valid.
    24  Valid pool types are pool types that are registered for Juju environment.
    25  
    26  options:
    27  -e, --environment (= "")
    28     juju environment to operate in
    29  -o, --output (= "")
    30     specify an output file
    31  --format (= yaml)
    32     specify output format (json|tabular|yaml)
    33  --provider
    34     pool provider type
    35  --name
    36     pool name
    37  
    38  `
    39  
    40  // PoolListCommand lists storage pools.
    41  type PoolListCommand struct {
    42  	PoolCommandBase
    43  	Providers []string
    44  	Names     []string
    45  	out       cmd.Output
    46  }
    47  
    48  // Init implements Command.Init.
    49  func (c *PoolListCommand) Init(args []string) (err error) {
    50  	return nil
    51  }
    52  
    53  // Info implements Command.Info.
    54  func (c *PoolListCommand) Info() *cmd.Info {
    55  	return &cmd.Info{
    56  		Name:    "list",
    57  		Purpose: "list storage pools",
    58  		Doc:     PoolListCommandDoc,
    59  	}
    60  }
    61  
    62  // SetFlags implements Command.SetFlags.
    63  func (c *PoolListCommand) SetFlags(f *gnuflag.FlagSet) {
    64  	c.StorageCommandBase.SetFlags(f)
    65  	f.Var(cmd.NewAppendStringsValue(&c.Providers), "provider", "only show pools of these provider types")
    66  	f.Var(cmd.NewAppendStringsValue(&c.Names), "name", "only show pools with these names")
    67  
    68  	c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
    69  		"yaml":    cmd.FormatYaml,
    70  		"json":    cmd.FormatJson,
    71  		"tabular": formatPoolListTabular,
    72  	})
    73  }
    74  
    75  // Run implements Command.Run.
    76  func (c *PoolListCommand) Run(ctx *cmd.Context) (err error) {
    77  	api, err := getPoolListAPI(c)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	defer api.Close()
    82  
    83  	result, err := api.ListPools(c.Providers, c.Names)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	if len(result) == 0 {
    88  		return nil
    89  	}
    90  	output := formatPoolInfo(result)
    91  	return c.out.Write(ctx, output)
    92  }
    93  
    94  var (
    95  	getPoolListAPI = (*PoolListCommand).getPoolListAPI
    96  )
    97  
    98  // PoolListAPI defines the API methods that the storage commands use.
    99  type PoolListAPI interface {
   100  	Close() error
   101  	ListPools(providers, names []string) ([]params.StoragePool, error)
   102  }
   103  
   104  func (c *PoolListCommand) getPoolListAPI() (PoolListAPI, error) {
   105  	return c.NewStorageAPI()
   106  }