github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/storage/pooldelete.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  
    10  	jujucmd "github.com/juju/juju/cmd"
    11  	"github.com/juju/juju/cmd/modelcmd"
    12  )
    13  
    14  // PoolRemoveAPI defines the API methods that the storage commands use.
    15  type PoolRemoveAPI interface {
    16  	Close() error
    17  	RemovePool(name string) error
    18  	BestAPIVersion() int
    19  }
    20  
    21  const poolRemoveCommandDoc = `
    22  Remove a single existing storage pool.
    23  
    24  Example:
    25      # Remove the storage-pool named fast-storage
    26  
    27        juju remove-storage-pool fast-storage
    28  
    29  See also:
    30      create-storage-pool
    31      update-storage-pool
    32      storage-pools
    33  `
    34  
    35  // NewPoolRemoveCommand returns a command that removes the named storage pool.
    36  func NewPoolRemoveCommand() cmd.Command {
    37  	cmd := &poolRemoveCommand{}
    38  	cmd.newAPIFunc = func() (PoolRemoveAPI, error) {
    39  		return cmd.NewStorageAPI()
    40  	}
    41  	return modelcmd.Wrap(cmd)
    42  }
    43  
    44  // poolRemoveCommand removes a storage pool.
    45  type poolRemoveCommand struct {
    46  	PoolCommandBase
    47  	newAPIFunc func() (PoolRemoveAPI, error)
    48  	poolName   string
    49  }
    50  
    51  // Init implements Command.Init.
    52  func (c *poolRemoveCommand) Init(args []string) (err error) {
    53  	if len(args) < 1 {
    54  		return errors.New("pool removal requires storage pool name")
    55  	}
    56  	c.poolName = args[0]
    57  	return cmd.CheckEmpty(args[1:])
    58  }
    59  
    60  // Info implements Command.Info.
    61  func (c *poolRemoveCommand) Info() *cmd.Info {
    62  	return jujucmd.Info(&cmd.Info{
    63  		Name:    "remove-storage-pool",
    64  		Purpose: "Remove an existing storage pool.",
    65  		Doc:     poolRemoveCommandDoc,
    66  		Args:    "<name>",
    67  	})
    68  }
    69  
    70  // Run implements Command.Run.
    71  func (c *poolRemoveCommand) Run(ctx *cmd.Context) (err error) {
    72  	api, err := c.newAPIFunc()
    73  	if err != nil {
    74  		return err
    75  	}
    76  	defer api.Close()
    77  	if api.BestAPIVersion() < 5 {
    78  		return errors.New("removing storage pools is not supported by this version of Juju")
    79  	}
    80  	return api.RemovePool(c.poolName)
    81  }