github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/storage/poolupdate.go (about)

     1  // Copyright 2019 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  
    11  	jujucmd "github.com/juju/juju/cmd"
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  )
    14  
    15  const (
    16  	Type = "type"
    17  )
    18  
    19  // PoolUpdateAPI defines the API methods that the storage commands use.
    20  type PoolUpdateAPI interface {
    21  	Close() error
    22  	UpdatePool(name, provider string, attr map[string]interface{}) error
    23  	BestAPIVersion() int
    24  }
    25  
    26  const poolUpdateCommandDoc = `
    27  Update configuration attributes for a single existing storage pool.
    28  
    29  Example:
    30      # Update the storage-pool named iops with new configuration details
    31  
    32        juju update-storage-pool operator-storage volume-type=provisioned-iops iops=40
    33  
    34      # Update which provider the pool is for
    35        juju update-storage-pool lxd-storage type=lxd-zfs
    36  
    37  See also:
    38      create-storage-pool
    39      remove-storage-pool
    40      storage-pools
    41  `
    42  
    43  // NewPoolUpdateCommand returns a command that replaces the named storage pools' attributes.
    44  func NewPoolUpdateCommand() cmd.Command {
    45  	cmd := &poolUpdateCommand{}
    46  	cmd.newAPIFunc = func() (PoolUpdateAPI, error) {
    47  		return cmd.NewStorageAPI()
    48  	}
    49  	return modelcmd.Wrap(cmd)
    50  }
    51  
    52  // poolUpdateCommand updates a storage pool configuration attributes.
    53  type poolUpdateCommand struct {
    54  	PoolCommandBase
    55  	newAPIFunc  func() (PoolUpdateAPI, error)
    56  	poolName    string
    57  	configAttrs map[string]interface{}
    58  	provider    string
    59  }
    60  
    61  // Init implements Command.Init.
    62  func (c *poolUpdateCommand) Init(args []string) (err error) {
    63  	if len(args) < 2 {
    64  		return errors.New("pool update requires name and configuration attributes")
    65  	}
    66  
    67  	c.poolName = args[0]
    68  
    69  	config, err := keyvalues.Parse(args[1:], false)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	if providerType, ok := config[Type]; ok {
    75  		delete(config, Type)
    76  		c.provider = providerType
    77  	}
    78  	c.configAttrs = make(map[string]interface{})
    79  	for key, value := range config {
    80  		c.configAttrs[key] = value
    81  	}
    82  	return nil
    83  }
    84  
    85  // Info implements Command.Info.
    86  func (c *poolUpdateCommand) Info() *cmd.Info {
    87  	return jujucmd.Info(&cmd.Info{
    88  		Name:    "update-storage-pool",
    89  		Purpose: "Update storage pool attributes.",
    90  		Doc:     poolUpdateCommandDoc,
    91  		Args:    "<name> [<key>=<value> [<key>=<value>...]]",
    92  	})
    93  }
    94  
    95  // Run implements Command.Run.
    96  func (c *poolUpdateCommand) Run(ctx *cmd.Context) (err error) {
    97  	api, err := c.newAPIFunc()
    98  	if err != nil {
    99  		return err
   100  	}
   101  	defer api.Close()
   102  	if api.BestAPIVersion() < 5 {
   103  		return errors.New("updating storage pools is not supported by this version of Juju")
   104  	}
   105  	err = api.UpdatePool(c.poolName, c.provider, c.configAttrs)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	return nil
   110  }