-
Notifications
You must be signed in to change notification settings - Fork 20
/
instance_snapshot_create.go
81 lines (63 loc) · 2.25 KB
/
instance_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package cmd
import (
"context"
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type instanceSnapshotCreateCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"create"`
Instance string `cli-arg:"#" cli-usage:"INSTANCE-NAME|ID"`
Zone string `cli-short:"z" cli-usage:"instance zone"`
}
func (c *instanceSnapshotCreateCmd) cmdAliases() []string { return gCreateAlias }
func (c *instanceSnapshotCreateCmd) cmdShort() string { return "Create a Compute instance snapshot" }
func (c *instanceSnapshotCreateCmd) cmdLong() string {
return fmt.Sprintf(`This command creates a Compute instance snapshot.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&instanceSnapshotShowOutput{}), ", "))
}
func (c *instanceSnapshotCreateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceSnapshotCreateCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, c.Zone))
instance, err := globalstate.EgoscaleClient.FindInstance(ctx, c.Zone, c.Instance)
if err != nil {
return err
}
var snapshot *egoscale.Snapshot
decorateAsyncOperation(fmt.Sprintf("Creating snapshot of instance %q...", c.Instance), func() {
snapshot, err = globalstate.EgoscaleClient.CreateInstanceSnapshot(ctx, c.Zone, instance)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
err = fmt.Errorf("Request timeout reached. Snapshot creation is not canceled and might still be running, check the status with: exo c i snapshot list")
}
return
}
})
if err != nil {
return err
}
if !globalstate.Quiet {
return (&instanceSnapshotShowCmd{
cliCommandSettings: c.cliCommandSettings,
ID: *snapshot.ID,
Zone: c.Zone,
}).cmdRun(nil, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(instanceSnapshotCmd, &instanceSnapshotCreateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}