Skip to content

Commit 2a556d2

Browse files
committed
feat(block): add volume wait command
1 parent 172349b commit 2a556d2

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

cmd/scw/testdata/test-all-usage-block-volume-usage.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ AVAILABLE COMMANDS:
1212
list List volumes
1313
update Update a volume
1414

15+
WORKFLOW COMMANDS:
16+
wait Wait for volume to reach a stable state
17+
1518
FLAGS:
1619
-h, --help help for volume
1720

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
Wait for volume to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the volume.
4+
5+
USAGE:
6+
scw block volume wait <volume-id ...> [arg=value ...]
7+
8+
EXAMPLES:
9+
Wait for a volume to be available
10+
scw block volume wait 11111111-1111-1111-1111-111111111111 terminal-status=available
11+
12+
ARGS:
13+
[timeout=5m0s] Timeout of the wait
14+
volume-id ID of the volume affected by the action.
15+
[terminal-status] Expected terminal status, will wait until this status is reached. (unknown_status | creating | available | in_use | deleting | deleted | resizing | error | snapshotting | locked | updating)
16+
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config (fr-par-1 | fr-par-2 | fr-par-3 | nl-ams-1 | nl-ams-2 | nl-ams-3 | pl-waw-1 | pl-waw-2 | pl-waw-3)
17+
18+
FLAGS:
19+
-h, --help help for wait
20+
21+
GLOBAL FLAGS:
22+
-c, --config string The path to the config file
23+
-D, --debug Enable debug mode
24+
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
25+
-p, --profile string The config profile to use

internal/namespaces/block/v1alpha1/custom.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ var (
4545
func GetCommands() *core.Commands {
4646
cmds := GetGeneratedCommands()
4747

48+
cmds.Add(volumeWaitCommand())
49+
4850
human.RegisterMarshalerFunc(block.VolumeStatus(""), human.EnumMarshalFunc(volumeStatusMarshalSpecs))
4951
human.RegisterMarshalerFunc(block.SnapshotStatus(""), human.EnumMarshalFunc(snapshotStatusMarshalSpecs))
5052
human.RegisterMarshalerFunc(block.ReferenceStatus(""), human.EnumMarshalFunc(referenceStatusMarshalSpecs))
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
volumeActionTimeout = 5 * time.Minute
16+
)
17+
18+
type volumeWaitRequest struct {
19+
Zone scw.Zone
20+
VolumeID string
21+
Timeout time.Duration
22+
23+
TerminalStatus *block.VolumeStatus
24+
}
25+
26+
func volumeWaitCommand() *core.Command {
27+
terminalStatus := block.VolumeStatus("").Values()
28+
terminalStatusStrings := make([]string, len(terminalStatus))
29+
for k, v := range terminalStatus {
30+
terminalStatusStrings[k] = v.String()
31+
}
32+
33+
return &core.Command{
34+
Short: `Wait for volume to reach a stable state`,
35+
Long: `Wait for volume to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the volume.`,
36+
Namespace: "block",
37+
Resource: "volume",
38+
Verb: "wait",
39+
Groups: []string{"workflow"},
40+
ArgsType: reflect.TypeOf(volumeWaitRequest{}),
41+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
42+
args := argsI.(*volumeWaitRequest)
43+
44+
return block.NewAPI(core.ExtractClient(ctx)).WaitForVolume(&block.WaitForVolumeRequest{
45+
Zone: args.Zone,
46+
VolumeID: args.VolumeID,
47+
Timeout: scw.TimeDurationPtr(args.Timeout),
48+
RetryInterval: core.DefaultRetryInterval,
49+
50+
TerminalStatus: args.TerminalStatus,
51+
})
52+
},
53+
ArgSpecs: core.ArgSpecs{
54+
core.WaitTimeoutArgSpec(volumeActionTimeout),
55+
{
56+
Name: "volume-id",
57+
Short: `ID of the volume 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: terminalStatusStrings,
65+
},
66+
core.ZoneArgSpec((*instance.API)(nil).Zones()...),
67+
},
68+
Examples: []*core.Example{
69+
{
70+
Short: "Wait for a volume to be available",
71+
ArgsJSON: `{"volume_id": "11111111-1111-1111-1111-111111111111", "terminal_status": "available"}`,
72+
},
73+
},
74+
}
75+
}

0 commit comments

Comments
 (0)