-
Notifications
You must be signed in to change notification settings - Fork 19
/
snapshot_create.go
57 lines (47 loc) · 1.4 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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"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(output.TemplateAnnotations(&snapshotShowOutput{}), ", ")),
Aliases: gCreateAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}
return printOutput(createSnapshot(args[0]))
},
})
}
func createSnapshot(vmID string) (output.Outputter, error) {
vm, err := getVirtualMachineByNameOrID(vmID)
if err != nil {
return nil, err
}
resp, err := globalstate.EgoscaleClient.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 !globalstate.Quiet {
return showSnapshot(res.(*egoscale.Snapshot))
}
return nil, nil
}