Skip to content

Commit 93b5c42

Browse files
authored
feat(block): add volume wait command (scaleway#4434)
1 parent 4bfc00a commit 93b5c42

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-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

docs/commands/block.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This API allows you to manage your Block Storage volumes.
1616
- [Get a volume](#get-a-volume)
1717
- [List volumes](#list-volumes)
1818
- [Update a volume](#update-a-volume)
19+
- [Wait for volume to reach a stable state](#wait-for-volume-to-reach-a-stable-state)
1920
- [Block Storage volume types are determined by their storage class and their IOPS. There are two storage classes available: `bssd` and `sbs`. The IOPS can be chosen for volumes of the `sbs` storage class](#block-storage-volume-types-are-determined-by-their-storage-class-and-their-iops.-there-are-two-storage-classes-available:-`bssd`-and-`sbs`.-the-iops-can-be-chosen-for-volumes-of-the-`sbs`-storage-class)
2021
- [List volume types](#list-volume-types)
2122

@@ -306,6 +307,38 @@ scw block volume update <volume-id ...> [arg=value ...]
306307

307308

308309

310+
### Wait for volume to reach a stable state
311+
312+
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.
313+
314+
**Usage:**
315+
316+
```
317+
scw block volume wait <volume-id ...> [arg=value ...]
318+
```
319+
320+
321+
**Args:**
322+
323+
| Name | | Description |
324+
|------|---|-------------|
325+
| timeout | Default: `5m0s` | Timeout of the wait |
326+
| volume-id | Required | ID of the volume affected by the action. |
327+
| terminal-status | One of: `unknown_status`, `creating`, `available`, `in_use`, `deleting`, `deleted`, `resizing`, `error`, `snapshotting`, `locked`, `updating` | Expected terminal status, will wait until this status is reached. |
328+
| zone | Default: `fr-par-1`<br />One of: `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` | Zone to target. If none is passed will use default zone from the config |
329+
330+
331+
**Examples:**
332+
333+
334+
Wait for a volume to be available
335+
```
336+
scw block volume wait 11111111-1111-1111-1111-111111111111 terminal-status=available
337+
```
338+
339+
340+
341+
309342
## Block Storage volume types are determined by their storage class and their IOPS. There are two storage classes available: `bssd` and `sbs`. The IOPS can be chosen for volumes of the `sbs` storage class
310343

311344
Block Storage volume types are determined by their storage class and their IOPS. There are two storage classes available: `bssd` and `sbs`. The IOPS can be chosen for volumes of the `sbs` storage class.

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)