Skip to content

Commit 0e419de

Browse files
authored
feat(instance): add a wait command for image and snapshots (#996)
1 parent 76fbcaf commit 0e419de

7 files changed

+127
-0
lines changed

cmd/scw/testdata/test-all-usage-instance-image-usage.stderr.golden

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ AVAILABLE COMMANDS:
1111
get Get image
1212
create Create image
1313
delete Delete image
14+
wait Wait for image to reach a stable state
1415

1516
FLAGS:
1617
-h, --help help for image
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Wait for image to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the image.
2+
3+
USAGE:
4+
scw instance image wait <image-id ...> [arg=value ...]
5+
6+
EXAMPLES:
7+
Wait for a image to reach a stable state
8+
scw instance image wait 11111111-1111-1111-1111-111111111111
9+
10+
ARGS:
11+
image-id ID of the image.
12+
[zone] Zone to target. If none is passed will use default zone from the config
13+
14+
FLAGS:
15+
-h, --help help for wait
16+
17+
GLOBAL FLAGS:
18+
-D, --debug Enable debug mode
19+
-o, --output string Output format: json or human
20+
-p, --profile string The config profile to use

cmd/scw/testdata/test-all-usage-instance-snapshot-usage.stderr.golden

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ AVAILABLE COMMANDS:
1818
create Create snapshot
1919
get Get snapshot
2020
delete Delete snapshot
21+
wait Wait for snapshot to reach a stable state
2122

2223
FLAGS:
2324
-h, --help help for snapshot
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
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.
2+
3+
USAGE:
4+
scw instance snapshot wait <snapshot-id ...> [arg=value ...]
5+
6+
EXAMPLES:
7+
Wait for a snapshot to reach a stable state
8+
scw instance snapshot wait 11111111-1111-1111-1111-111111111111
9+
10+
ARGS:
11+
snapshot-id ID of the snapshot.
12+
[zone] Zone to target. If none is passed will use default zone from the config
13+
14+
FLAGS:
15+
-h, --help help for wait
16+
17+
GLOBAL FLAGS:
18+
-D, --debug Enable debug mode
19+
-o, --output string Output format: json or human
20+
-p, --profile string The config profile to use

internal/namespaces/instance/v1/custom.go

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func GetCommands() *core.Commands {
8282
cmds.MustFind("instance", "image", "create").Override(imageCreateBuilder)
8383
cmds.MustFind("instance", "image", "list").Override(imageListBuilder)
8484
cmds.MustFind("instance", "image", "delete").Override(imageDeleteBuilder)
85+
cmds.Merge(core.NewCommands(
86+
imageWaitCommand(),
87+
))
8588

8689
//
8790
// Snapshot
@@ -90,6 +93,9 @@ func GetCommands() *core.Commands {
9093

9194
cmds.MustFind("instance", "snapshot", "create").Override(snapshotCreateBuilder)
9295
cmds.MustFind("instance", "snapshot", "list").Override(snapshotListBuilder)
96+
cmds.Merge(core.NewCommands(
97+
snapshotWaitCommand(),
98+
))
9399

94100
//
95101
// Volume

internal/namespaces/instance/v1/custom_image.go

+39
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"github.com/scaleway/scaleway-sdk-go/scw"
1212
)
1313

14+
const (
15+
imageActionTimeout = 60 * time.Minute
16+
)
17+
1418
//
1519
// Builders
1620
//
@@ -216,3 +220,38 @@ func imageDeleteBuilder(c *core.Command) *core.Command {
216220

217221
return c
218222
}
223+
224+
func imageWaitCommand() *core.Command {
225+
return &core.Command{
226+
Short: `Wait for image to reach a stable state`,
227+
Long: `Wait for image to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the image.`,
228+
Namespace: "instance",
229+
Resource: "image",
230+
Verb: "wait",
231+
ArgsType: reflect.TypeOf(instance.WaitForImageRequest{}),
232+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
233+
api := instance.NewAPI(core.ExtractClient(ctx))
234+
return api.WaitForImage(&instance.WaitForImageRequest{
235+
Zone: argsI.(*instance.WaitForImageRequest).Zone,
236+
ImageID: argsI.(*instance.WaitForImageRequest).ImageID,
237+
Timeout: imageActionTimeout,
238+
})
239+
240+
},
241+
ArgSpecs: core.ArgSpecs{
242+
{
243+
Name: "image-id",
244+
Short: `ID of the image.`,
245+
Required: true,
246+
Positional: true,
247+
},
248+
core.ZoneArgSpec(),
249+
},
250+
Examples: []*core.Example{
251+
{
252+
Short: "Wait for a image to reach a stable state",
253+
Request: `{"image_id": "11111111-1111-1111-1111-111111111111"}`,
254+
},
255+
},
256+
}
257+
}

internal/namespaces/instance/v1/custom_snapshot.go

+40
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ package instance
33
import (
44
"context"
55
"reflect"
6+
"time"
67

78
"github.com/scaleway/scaleway-cli/internal/core"
89
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
910
)
1011

12+
const (
13+
snapshotActionTimeout = 60 * time.Minute
14+
)
15+
1116
// Builders
1217

1318
func snapshotCreateBuilder(c *core.Command) *core.Command {
@@ -69,3 +74,38 @@ func snapshotListBuilder(c *core.Command) *core.Command {
6974
})
7075
return c
7176
}
77+
78+
func snapshotWaitCommand() *core.Command {
79+
return &core.Command{
80+
Short: `Wait for snapshot to reach a stable state`,
81+
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.`,
82+
Namespace: "instance",
83+
Resource: "snapshot",
84+
Verb: "wait",
85+
ArgsType: reflect.TypeOf(instance.WaitForSnapshotRequest{}),
86+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
87+
api := instance.NewAPI(core.ExtractClient(ctx))
88+
return api.WaitForSnapshot(&instance.WaitForSnapshotRequest{
89+
Zone: argsI.(*instance.WaitForSnapshotRequest).Zone,
90+
SnapshotID: argsI.(*instance.WaitForSnapshotRequest).SnapshotID,
91+
Timeout: snapshotActionTimeout,
92+
})
93+
94+
},
95+
ArgSpecs: core.ArgSpecs{
96+
{
97+
Name: "snapshot-id",
98+
Short: `ID of the snapshot.`,
99+
Required: true,
100+
Positional: true,
101+
},
102+
core.ZoneArgSpec(),
103+
},
104+
Examples: []*core.Example{
105+
{
106+
Short: "Wait for a snapshot to reach a stable state",
107+
Request: `{"snapshot_id": "11111111-1111-1111-1111-111111111111"}`,
108+
},
109+
},
110+
}
111+
}

0 commit comments

Comments
 (0)