github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/v3" 8 "github.com/juju/errors" 9 10 jujucmd "github.com/juju/juju/cmd" 11 "github.com/juju/juju/rpc/params" 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 constraintsMap, 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(constraintsMap)) 56 for k, v := range constraintsMap { 57 cons := v 58 if cons != (storage.Constraints{Count: cons.Count}) { 59 return errors.Errorf("only count can be specified for %q", k) 60 } 61 s.all[k] = params.StorageConstraints{Count: &cons.Count} 62 } 63 return nil 64 } 65 66 func (s *StorageAddCommand) Run(ctx *cmd.Context) error { 67 return s.ctx.AddUnitStorage(s.all) 68 }