-
Notifications
You must be signed in to change notification settings - Fork 57
/
create.go
97 lines (81 loc) · 2.35 KB
/
create.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package snapshotcommands
import (
"github.com/rackspace/rack/commandoptions"
"github.com/rackspace/rack/handler"
"github.com/rackspace/rack/internal/github.com/codegangsta/cli"
osSnapshots "github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
"github.com/rackspace/rack/util"
)
var create = cli.Command{
Name: "create",
Usage: util.Usage(commandPrefix, "create", "--volume-id <volumeID>"),
Description: "Creates a volume",
Action: actionCreate,
Flags: commandoptions.CommandFlags(flagsCreate, keysCreate),
BashComplete: func(c *cli.Context) {
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsCreate, keysCreate))
},
}
func flagsCreate() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "volume-id",
Usage: "[required] The volume ID from which to create this snapshot.",
},
cli.StringFlag{
Name: "name",
Usage: "[optional] A name for this snapshot.",
},
cli.StringFlag{
Name: "description",
Usage: "[optional] A description for this snapshot.",
},
}
}
var keysCreate = []string{"ID", "Name", "Description", "Size", "VolumeType", "SnapshotID", "Attachments", "CreatedAt"}
type paramsCreate struct {
opts *osSnapshots.CreateOpts
}
type commandCreate handler.Command
func actionCreate(c *cli.Context) {
command := &commandCreate{
Ctx: &handler.Context{
CLIContext: c,
},
}
handler.Handle(command)
}
func (command *commandCreate) Context() *handler.Context {
return command.Ctx
}
func (command *commandCreate) Keys() []string {
return keysCreate
}
func (command *commandCreate) ServiceClientType() string {
return serviceClientType
}
func (command *commandCreate) HandleFlags(resource *handler.Resource) error {
err := command.Ctx.CheckFlagsSet([]string{"volume-id"})
if err != nil {
return err
}
c := command.Ctx.CLIContext
opts := &osSnapshots.CreateOpts{
VolumeID: c.String("volume-id"),
Name: c.String("name"),
Description: c.String("description"),
}
resource.Params = ¶msCreate{
opts: opts,
}
return nil
}
func (command *commandCreate) Execute(resource *handler.Resource) {
opts := resource.Params.(*paramsCreate).opts
snapshot, err := osSnapshots.Create(command.Ctx.ServiceClient, opts).Extract()
if err != nil {
resource.Err = err
return
}
resource.Result = snapshotSingle(snapshot)
}