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