-
Notifications
You must be signed in to change notification settings - Fork 18
/
snapshot_create.go
55 lines (45 loc) · 1.27 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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
func init() {
snapshotCmd.AddCommand(&cobra.Command{
Use: "create INSTANCE-NAME|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
}
resp, err := cs.GetWithContext(gContext, &egoscale.Volume{
VirtualMachineID: vm.ID,
Type: "ROOT",
})
if err != nil {
return nil, fmt.Errorf("unable to retrieve Compute instance volume: %v", err)
}
createSnapshotReq := &egoscale.CreateSnapshot{VolumeID: resp.(*egoscale.Volume).ID}
res, err := asyncRequest(createSnapshotReq, fmt.Sprintf("Creating snapshot of %q", vm.Name))
if err != nil {
return nil, err
}
if !gQuiet {
return showSnapshot(res.(*egoscale.Snapshot))
}
return nil, nil
}