-
Notifications
You must be signed in to change notification settings - Fork 20
/
snapshot_create.go
60 lines (48 loc) · 1.23 KB
/
snapshot_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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
func init() {
snapshotCmd.AddCommand(&cobra.Command{
Use: "create <vm name | vm id>",
Short: "Create a snapshot of a Compute instance volume",
Long: fmt.Sprintf(`This command creates a snapshot of a Compute instance volume.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&snapshotShowOutput{}), ", ")),
Aliases: gCreateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}
return output(createSnapshot(args[0]))
},
})
}
func createSnapshot(vmID string) (outputter, error) {
vm, err := getVirtualMachineByNameOrID(vmID)
if err != nil {
return nil, err
}
query := &egoscale.Volume{
VirtualMachineID: vm.ID,
Type: "ROOT",
}
resp, err := cs.GetWithContext(gContext, query)
if err != nil {
return nil, err
}
createSnapshot := &egoscale.CreateSnapshot{
VolumeID: resp.(*egoscale.Volume).ID,
}
res, err := asyncRequest(createSnapshot, fmt.Sprintf("Creating snapshot of %q", vm.Name))
if err != nil {
return nil, err
}
if !gQuiet {
return showSnapshot(res.(*egoscale.Snapshot))
}
return nil, nil
}