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