github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/storage/poolcreate.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/errors"
     9  	"github.com/juju/utils/keyvalues"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  )
    14  
    15  // PoolCreateAPI defines the API methods that pool create command uses.
    16  type PoolCreateAPI interface {
    17  	Close() error
    18  	CreatePool(pname, ptype string, pconfig map[string]interface{}) error
    19  }
    20  
    21  const poolCreateCommandDoc = `
    22  Create or define a storage pool.
    23  
    24  Pools are a mechanism for administrators to define sources of storage that
    25  they will use to satisfy service storage requirements.
    26  
    27  A single pool might be used for storage from units of many different services -
    28  it is a resource from which different stores may be drawn.
    29  
    30  A pool describes provider-specific parameters for creating storage,
    31  such as performance (e.g. IOPS), media type (e.g. magnetic vs. SSD),
    32  or durability.
    33  
    34  For many providers, there will be a shared resource
    35  where storage can be requested (e.g. EBS in amazon).
    36  Creating pools there maps provider specific settings
    37  into named resources that can be used during deployment.
    38  
    39  Pools defined at the model level are easily reused across services.
    40  
    41  options:
    42      -m, --model (= "")
    43          juju model to operate in
    44      -o, --output (= "")
    45          specify an output file
    46      <name>
    47          pool name
    48      <provider type>
    49          pool provider type
    50      <key>=<value> (<key>=<value> ...)
    51          pool configuration attributes as space-separated pairs, 
    52          for e.g. tags, size, path, etc...
    53  `
    54  
    55  // NewPoolCreateCommand returns a command that creates or defines a storage pool
    56  func NewPoolCreateCommand() cmd.Command {
    57  	cmd := &poolCreateCommand{}
    58  	cmd.newAPIFunc = func() (PoolCreateAPI, error) {
    59  		return cmd.NewStorageAPI()
    60  	}
    61  	return modelcmd.Wrap(cmd)
    62  }
    63  
    64  // poolCreateCommand lists storage pools.
    65  type poolCreateCommand struct {
    66  	PoolCommandBase
    67  	newAPIFunc func() (PoolCreateAPI, error)
    68  	poolName   string
    69  	// TODO(anastasiamac 2015-01-29) type will need to become optional
    70  	// if type is unspecified, use the environment's default provider type
    71  	provider string
    72  	attrs    map[string]interface{}
    73  }
    74  
    75  // Init implements Command.Init.
    76  func (c *poolCreateCommand) Init(args []string) (err error) {
    77  	if len(args) < 3 {
    78  		return errors.New("pool creation requires names, provider type and attrs for configuration")
    79  	}
    80  
    81  	c.poolName = args[0]
    82  	c.provider = args[1]
    83  
    84  	options, err := keyvalues.Parse(args[2:], false)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	if len(options) == 0 {
    90  		return errors.New("pool creation requires attrs for configuration")
    91  	}
    92  	c.attrs = make(map[string]interface{})
    93  	for key, value := range options {
    94  		c.attrs[key] = value
    95  	}
    96  	return nil
    97  }
    98  
    99  // Info implements Command.Info.
   100  func (c *poolCreateCommand) Info() *cmd.Info {
   101  	return &cmd.Info{
   102  		Name:    "create-storage-pool",
   103  		Args:    "<name> <provider> [<key>=<value> [<key>=<value>...]]",
   104  		Purpose: "create storage pool",
   105  		Doc:     poolCreateCommandDoc,
   106  	}
   107  }
   108  
   109  // SetFlags implements Command.SetFlags.
   110  func (c *poolCreateCommand) SetFlags(f *gnuflag.FlagSet) {
   111  	c.StorageCommandBase.SetFlags(f)
   112  }
   113  
   114  // Run implements Command.Run.
   115  func (c *poolCreateCommand) Run(ctx *cmd.Context) (err error) {
   116  	api, err := c.newAPIFunc()
   117  	if err != nil {
   118  		return err
   119  	}
   120  	defer api.Close()
   121  	return api.CreatePool(c.poolName, c.provider, c.attrs)
   122  }