github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	jujucmd "github.com/juju/juju/cmd"
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  )
    14  
    15  // PoolCommandBase is a helper base structure for pool commands.
    16  type PoolCommandBase struct {
    17  	StorageCommandBase
    18  }
    19  
    20  // PoolInfo defines the serialization behaviour of the storage pool information.
    21  type PoolInfo struct {
    22  	Provider string                 `yaml:"provider" json:"provider"`
    23  	Attrs    map[string]interface{} `yaml:"attrs,omitempty" json:"attrs,omitempty"`
    24  }
    25  
    26  func formatPoolInfo(all []params.StoragePool) map[string]PoolInfo {
    27  	output := make(map[string]PoolInfo)
    28  	for _, one := range all {
    29  		output[one.Name] = PoolInfo{
    30  			Provider: one.Provider,
    31  			Attrs:    one.Attrs,
    32  		}
    33  	}
    34  	return output
    35  }
    36  
    37  const poolListCommandDoc = `
    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  
    50  // NewPoolListCommand returns a command that lists storage pools on a model
    51  func NewPoolListCommand() cmd.Command {
    52  	cmd := &poolListCommand{}
    53  	cmd.newAPIFunc = func() (PoolListAPI, error) {
    54  		return cmd.NewStorageAPI()
    55  	}
    56  	return modelcmd.Wrap(cmd)
    57  }
    58  
    59  // poolListCommand lists storage pools.
    60  type poolListCommand struct {
    61  	PoolCommandBase
    62  	newAPIFunc func() (PoolListAPI, error)
    63  	Providers  []string
    64  	Names      []string
    65  	out        cmd.Output
    66  }
    67  
    68  // Init implements Command.Init.
    69  func (c *poolListCommand) Init(args []string) (err error) {
    70  	return nil
    71  }
    72  
    73  // Info implements Command.Info.
    74  func (c *poolListCommand) Info() *cmd.Info {
    75  	return jujucmd.Info(&cmd.Info{
    76  		Name:    "storage-pools",
    77  		Purpose: "List storage pools.",
    78  		Doc:     poolListCommandDoc,
    79  		Aliases: []string{"list-storage-pools"},
    80  	})
    81  }
    82  
    83  // SetFlags implements Command.SetFlags.
    84  func (c *poolListCommand) SetFlags(f *gnuflag.FlagSet) {
    85  	c.StorageCommandBase.SetFlags(f)
    86  	f.Var(cmd.NewAppendStringsValue(&c.Providers), "provider", "Only show pools of these provider types")
    87  	f.Var(cmd.NewAppendStringsValue(&c.Names), "name", "Only show pools with these names")
    88  
    89  	c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
    90  		"yaml":    cmd.FormatYaml,
    91  		"json":    cmd.FormatJson,
    92  		"tabular": formatPoolListTabular,
    93  	})
    94  }
    95  
    96  // Run implements Command.Run.
    97  func (c *poolListCommand) Run(ctx *cmd.Context) (err error) {
    98  	api, err := c.newAPIFunc()
    99  	if err != nil {
   100  		return err
   101  	}
   102  	defer api.Close()
   103  	result, err := api.ListPools(c.Providers, c.Names)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	if len(result) == 0 {
   108  		ctx.Infof("No storage pools to display.")
   109  		return nil
   110  	}
   111  	output := formatPoolInfo(result)
   112  	return c.out.Write(ctx, output)
   113  }
   114  
   115  // PoolListAPI defines the API methods that the storage commands use.
   116  type PoolListAPI interface {
   117  	Close() error
   118  	ListPools(providers, names []string) ([]params.StoragePool, error)
   119  }