-
Notifications
You must be signed in to change notification settings - Fork 57
/
delete.go
101 lines (85 loc) · 2.62 KB
/
delete.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
98
99
100
101
package snapshotcommands
import (
"fmt"
"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 remove = cli.Command{
Name: "delete",
Usage: util.Usage(commandPrefix, "delete", "[--id <snapshotID> | --name <snapshotName> | --stdin id]"),
Description: "Deletes a snapshot",
Action: actionDelete,
Flags: commandoptions.CommandFlags(flagsDelete, keysDelete),
BashComplete: func(c *cli.Context) {
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsDelete, keysDelete))
},
}
func flagsDelete() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "id",
Usage: "[optional; required if `stdin` or `name` isn't provided] The ID of the snapshot.",
},
cli.StringFlag{
Name: "name",
Usage: "[optional; required if `stdin` or `id` isn't provided] The name of the snapshot.",
},
cli.StringFlag{
Name: "stdin",
Usage: "[optional; required if `id` or `name` isn't provided] The field being piped into STDIN. Valid values are: id",
},
}
}
var keysDelete = []string{}
type paramsDelete struct {
snapshotID string
}
type commandDelete handler.Command
func actionDelete(c *cli.Context) {
command := &commandDelete{
Ctx: &handler.Context{
CLIContext: c,
},
}
handler.Handle(command)
}
func (command *commandDelete) Context() *handler.Context {
return command.Ctx
}
func (command *commandDelete) Keys() []string {
return keysDelete
}
func (command *commandDelete) ServiceClientType() string {
return serviceClientType
}
func (command *commandDelete) HandleFlags(resource *handler.Resource) error {
resource.Params = ¶msDelete{}
return nil
}
func (command *commandDelete) HandlePipe(resource *handler.Resource, item string) error {
resource.Params.(*paramsDelete).snapshotID = item
return nil
}
func (command *commandDelete) HandleSingle(resource *handler.Resource) error {
snapshotID, err := command.Ctx.IDOrName(osSnapshots.IDFromName)
if err != nil {
return err
}
resource.Params.(*paramsDelete).snapshotID = snapshotID
return nil
}
func (command *commandDelete) Execute(resource *handler.Resource) {
snapshotID := resource.Params.(*paramsDelete).snapshotID
err := osSnapshots.Delete(command.Ctx.ServiceClient, snapshotID).ExtractErr()
if err != nil {
resource.Err = err
return
}
resource.Result = fmt.Sprintf("Deleting snapshot [%s]\n", snapshotID)
}
func (command *commandDelete) StdinField() string {
return "id"
}