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