|
| 1 | +package block |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/scaleway/scaleway-cli/v2/core" |
| 9 | + block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" |
| 11 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + snapshotActionTimeout = 5 * time.Minute |
| 16 | +) |
| 17 | + |
| 18 | +type snapshotWaitRequest struct { |
| 19 | + Zone scw.Zone |
| 20 | + SnapshotID string |
| 21 | + Timeout time.Duration |
| 22 | + |
| 23 | + TerminalStatus *block.SnapshotStatus |
| 24 | +} |
| 25 | + |
| 26 | +func snapshotWaitCommand() *core.Command { |
| 27 | + snapshotsStatuses := block.SnapshotStatus("").Values() |
| 28 | + snapshotsStatusStrings := make([]string, len(snapshotsStatuses)) |
| 29 | + for k, v := range snapshotsStatuses { |
| 30 | + snapshotsStatusStrings[k] = v.String() |
| 31 | + } |
| 32 | + |
| 33 | + return &core.Command{ |
| 34 | + Short: `Wait for snapshot to reach a stable state`, |
| 35 | + Long: `Wait for snapshot to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the snapshot.`, |
| 36 | + Namespace: "block", |
| 37 | + Resource: "snapshot", |
| 38 | + Verb: "wait", |
| 39 | + Groups: []string{"workflow"}, |
| 40 | + ArgsType: reflect.TypeOf(snapshotWaitRequest{}), |
| 41 | + Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) { |
| 42 | + args := argsI.(*snapshotWaitRequest) |
| 43 | + |
| 44 | + return block.NewAPI(core.ExtractClient(ctx)).WaitForSnapshot(&block.WaitForSnapshotRequest{ |
| 45 | + Zone: args.Zone, |
| 46 | + SnapshotID: args.SnapshotID, |
| 47 | + Timeout: scw.TimeDurationPtr(args.Timeout), |
| 48 | + RetryInterval: core.DefaultRetryInterval, |
| 49 | + |
| 50 | + TerminalStatus: args.TerminalStatus, |
| 51 | + }) |
| 52 | + }, |
| 53 | + ArgSpecs: core.ArgSpecs{ |
| 54 | + core.WaitTimeoutArgSpec(snapshotActionTimeout), |
| 55 | + { |
| 56 | + Name: "snapshot-id", |
| 57 | + Short: `ID of the snapshot affected by the action.`, |
| 58 | + Required: true, |
| 59 | + Positional: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + Name: "terminal-status", |
| 63 | + Short: `Expected terminal status, will wait until this status is reached.`, |
| 64 | + EnumValues: snapshotsStatusStrings, |
| 65 | + }, |
| 66 | + core.ZoneArgSpec((*instance.API)(nil).Zones()...), |
| 67 | + }, |
| 68 | + Examples: []*core.Example{ |
| 69 | + { |
| 70 | + Short: "Wait for a snapshot to be available", |
| 71 | + ArgsJSON: `{"snapshot_id": "11111111-1111-1111-1111-111111111111", "terminal_status": "available"}`, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func blockSnapshotCreateBuilder(c *core.Command) *core.Command { |
| 78 | + c.WaitFunc = func(ctx context.Context, _, respI interface{}) (interface{}, error) { |
| 79 | + resp := respI.(*block.Snapshot) |
| 80 | + |
| 81 | + return block.NewAPI(core.ExtractClient(ctx)).WaitForSnapshot(&block.WaitForSnapshotRequest{ |
| 82 | + SnapshotID: resp.ID, |
| 83 | + Zone: resp.Zone, |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + return c |
| 88 | +} |
0 commit comments