github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/storage-add.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/apiserver/params"
    11  	"github.com/juju/juju/storage"
    12  )
    13  
    14  // StorageAddCommand implements the status-set command.
    15  type StorageAddCommand struct {
    16  	cmd.CommandBase
    17  	ctx Context
    18  	all map[string]params.StorageConstraints
    19  }
    20  
    21  // NewStorageAddCommand makes a jujuc storage-add command.
    22  func NewStorageAddCommand(ctx Context) (cmd.Command, error) {
    23  	return &StorageAddCommand{ctx: ctx}, nil
    24  }
    25  
    26  var StorageAddDoc = `
    27  Storage add adds storage instances to unit using provided storage directives.
    28  A storage directive consists of a storage name as per charm specification
    29  and optional storage COUNT.
    30  
    31  COUNT is a positive integer indicating how many instances
    32  of the storage to create. If unspecified, COUNT defaults to 1.
    33  `[1:]
    34  
    35  func (s *StorageAddCommand) Info() *cmd.Info {
    36  	return &cmd.Info{
    37  		Name:    "storage-add",
    38  		Args:    "<charm storage name>[=count] ...",
    39  		Purpose: "add storage instances",
    40  		Doc:     StorageAddDoc,
    41  	}
    42  }
    43  
    44  func (s *StorageAddCommand) Init(args []string) error {
    45  	if len(args) < 1 {
    46  		return errors.New("storage add requires a storage directive")
    47  	}
    48  
    49  	cons, err := storage.ParseConstraintsMap(args, false)
    50  	if err != nil {
    51  		return errors.Trace(err)
    52  	}
    53  
    54  	s.all = make(map[string]params.StorageConstraints, len(cons))
    55  	for k, v := range cons {
    56  		if v != (storage.Constraints{Count: v.Count}) {
    57  			return errors.Errorf("only count can be specified for %q", k)
    58  		}
    59  		s.all[k] = params.StorageConstraints{Count: &v.Count}
    60  	}
    61  	return nil
    62  }
    63  
    64  func (s *StorageAddCommand) Run(ctx *cmd.Context) error {
    65  	s.ctx.AddUnitStorage(s.all)
    66  	return nil
    67  }