diff --git a/cmd/scw/testdata/test-all-usage-instance-image-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-image-usage.stderr.golden index c1d90f2572..e8e256fd93 100644 --- a/cmd/scw/testdata/test-all-usage-instance-image-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-image-usage.stderr.golden @@ -11,6 +11,7 @@ AVAILABLE COMMANDS: get Get image create Create image delete Delete image + wait Wait for image to reach a stable state FLAGS: -h, --help help for image diff --git a/cmd/scw/testdata/test-all-usage-instance-image-wait-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-image-wait-usage.stderr.golden new file mode 100644 index 0000000000..f6ea97f3e3 --- /dev/null +++ b/cmd/scw/testdata/test-all-usage-instance-image-wait-usage.stderr.golden @@ -0,0 +1,20 @@ +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. + +USAGE: + scw instance image wait [arg=value ...] + +EXAMPLES: + Wait for a image to reach a stable state + scw instance image wait 11111111-1111-1111-1111-111111111111 + +ARGS: + image-id ID of the image. + [zone] Zone to target. If none is passed will use default zone from the config + +FLAGS: + -h, --help help for wait + +GLOBAL FLAGS: + -D, --debug Enable debug mode + -o, --output string Output format: json or human + -p, --profile string The config profile to use diff --git a/cmd/scw/testdata/test-all-usage-instance-snapshot-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-snapshot-usage.stderr.golden index 985807d00c..10a2f71e64 100644 --- a/cmd/scw/testdata/test-all-usage-instance-snapshot-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-instance-snapshot-usage.stderr.golden @@ -18,6 +18,7 @@ AVAILABLE COMMANDS: create Create snapshot get Get snapshot delete Delete snapshot + wait Wait for snapshot to reach a stable state FLAGS: -h, --help help for snapshot diff --git a/cmd/scw/testdata/test-all-usage-instance-snapshot-wait-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-instance-snapshot-wait-usage.stderr.golden new file mode 100644 index 0000000000..8dfaf38182 --- /dev/null +++ b/cmd/scw/testdata/test-all-usage-instance-snapshot-wait-usage.stderr.golden @@ -0,0 +1,20 @@ +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. + +USAGE: + scw instance snapshot wait [arg=value ...] + +EXAMPLES: + Wait for a snapshot to reach a stable state + scw instance snapshot wait 11111111-1111-1111-1111-111111111111 + +ARGS: + snapshot-id ID of the snapshot. + [zone] Zone to target. If none is passed will use default zone from the config + +FLAGS: + -h, --help help for wait + +GLOBAL FLAGS: + -D, --debug Enable debug mode + -o, --output string Output format: json or human + -p, --profile string The config profile to use diff --git a/internal/namespaces/instance/v1/custom.go b/internal/namespaces/instance/v1/custom.go index c5abd5222b..edc05c6d1b 100644 --- a/internal/namespaces/instance/v1/custom.go +++ b/internal/namespaces/instance/v1/custom.go @@ -82,6 +82,9 @@ func GetCommands() *core.Commands { cmds.MustFind("instance", "image", "create").Override(imageCreateBuilder) cmds.MustFind("instance", "image", "list").Override(imageListBuilder) cmds.MustFind("instance", "image", "delete").Override(imageDeleteBuilder) + cmds.Merge(core.NewCommands( + imageWaitCommand(), + )) // // Snapshot @@ -90,6 +93,9 @@ func GetCommands() *core.Commands { cmds.MustFind("instance", "snapshot", "create").Override(snapshotCreateBuilder) cmds.MustFind("instance", "snapshot", "list").Override(snapshotListBuilder) + cmds.Merge(core.NewCommands( + snapshotWaitCommand(), + )) // // Volume diff --git a/internal/namespaces/instance/v1/custom_image.go b/internal/namespaces/instance/v1/custom_image.go index b245377ac8..1219f82f6c 100644 --- a/internal/namespaces/instance/v1/custom_image.go +++ b/internal/namespaces/instance/v1/custom_image.go @@ -11,6 +11,10 @@ import ( "github.com/scaleway/scaleway-sdk-go/scw" ) +const ( + imageActionTimeout = 60 * time.Minute +) + // // Builders // @@ -216,3 +220,38 @@ func imageDeleteBuilder(c *core.Command) *core.Command { return c } + +func imageWaitCommand() *core.Command { + return &core.Command{ + Short: `Wait for image to reach a stable state`, + 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.`, + Namespace: "instance", + Resource: "image", + Verb: "wait", + ArgsType: reflect.TypeOf(instance.WaitForImageRequest{}), + Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) { + api := instance.NewAPI(core.ExtractClient(ctx)) + return api.WaitForImage(&instance.WaitForImageRequest{ + Zone: argsI.(*instance.WaitForImageRequest).Zone, + ImageID: argsI.(*instance.WaitForImageRequest).ImageID, + Timeout: imageActionTimeout, + }) + + }, + ArgSpecs: core.ArgSpecs{ + { + Name: "image-id", + Short: `ID of the image.`, + Required: true, + Positional: true, + }, + core.ZoneArgSpec(), + }, + Examples: []*core.Example{ + { + Short: "Wait for a image to reach a stable state", + Request: `{"image_id": "11111111-1111-1111-1111-111111111111"}`, + }, + }, + } +} diff --git a/internal/namespaces/instance/v1/custom_snapshot.go b/internal/namespaces/instance/v1/custom_snapshot.go index ff3f8bb69a..9f0f8e4eb6 100644 --- a/internal/namespaces/instance/v1/custom_snapshot.go +++ b/internal/namespaces/instance/v1/custom_snapshot.go @@ -3,11 +3,16 @@ package instance import ( "context" "reflect" + "time" "github.com/scaleway/scaleway-cli/internal/core" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" ) +const ( + snapshotActionTimeout = 60 * time.Minute +) + // Builders func snapshotCreateBuilder(c *core.Command) *core.Command { @@ -69,3 +74,38 @@ func snapshotListBuilder(c *core.Command) *core.Command { }) return c } + +func snapshotWaitCommand() *core.Command { + return &core.Command{ + Short: `Wait for snapshot to reach a stable state`, + 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.`, + Namespace: "instance", + Resource: "snapshot", + Verb: "wait", + ArgsType: reflect.TypeOf(instance.WaitForSnapshotRequest{}), + Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) { + api := instance.NewAPI(core.ExtractClient(ctx)) + return api.WaitForSnapshot(&instance.WaitForSnapshotRequest{ + Zone: argsI.(*instance.WaitForSnapshotRequest).Zone, + SnapshotID: argsI.(*instance.WaitForSnapshotRequest).SnapshotID, + Timeout: snapshotActionTimeout, + }) + + }, + ArgSpecs: core.ArgSpecs{ + { + Name: "snapshot-id", + Short: `ID of the snapshot.`, + Required: true, + Positional: true, + }, + core.ZoneArgSpec(), + }, + Examples: []*core.Example{ + { + Short: "Wait for a snapshot to reach a stable state", + Request: `{"snapshot_id": "11111111-1111-1111-1111-111111111111"}`, + }, + }, + } +}