Skip to content

Commit 9ec11d4

Browse files
authored
feat(instance): add snapshot update (#3079)
1 parent e5fc7d4 commit 9ec11d4

11 files changed

Lines changed: 887 additions & 1 deletion
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
Update a snapshot
4+
5+
USAGE:
6+
scw instance snapshot update <snapshot-id ...> [arg=value ...]
7+
8+
ARGS:
9+
snapshot-id UUID of the snapshot.
10+
[name] Name of the snapshot.
11+
[tags.{index}] Tags of the snapshot.
12+
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config
13+
14+
FLAGS:
15+
-h, --help help for update
16+
-w, --wait wait until the snapshot is ready
17+
18+
GLOBAL FLAGS:
19+
-c, --config string The path to the config file
20+
-D, --debug Enable debug mode
21+
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
22+
-p, --profile string The config profile to use

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ AVAILABLE COMMANDS:
2525
export Export a snapshot
2626
get Get a snapshot
2727
list List snapshots
28+
update Update a snapshot
2829

2930
WORKFLOW COMMANDS:
3031
wait Wait for snapshot to reach a stable state

docs/commands/instance.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Instance API.
7676
- [Export a snapshot](#export-a-snapshot)
7777
- [Get a snapshot](#get-a-snapshot)
7878
- [List snapshots](#list-snapshots)
79+
- [Update a snapshot](#update-a-snapshot)
7980
- [Wait for snapshot to reach a stable state](#wait-for-snapshot-to-reach-a-stable-state)
8081
- [User data management commands](#user-data-management-commands)
8182
- [Delete user data](#delete-user-data)
@@ -2527,6 +2528,28 @@ scw instance snapshot list zone=fr-par-1
25272528

25282529

25292530

2531+
### Update a snapshot
2532+
2533+
2534+
2535+
**Usage:**
2536+
2537+
```
2538+
scw instance snapshot update <snapshot-id ...> [arg=value ...]
2539+
```
2540+
2541+
2542+
**Args:**
2543+
2544+
| Name | | Description |
2545+
|------|---|-------------|
2546+
| snapshot-id | Required | UUID of the snapshot. |
2547+
| name | | Name of the snapshot. |
2548+
| tags.{index} | | Tags of the snapshot. |
2549+
| zone | Default: `fr-par-1` | Zone to target. If none is passed will use default zone from the config |
2550+
2551+
2552+
25302553
### Wait for snapshot to reach a stable state
25312554

25322555
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.

internal/namespaces/instance/v1/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func GetCommands() *core.Commands {
109109
cmds.MustFind("instance", "snapshot", "list").Override(snapshotListBuilder)
110110
cmds.Merge(core.NewCommands(
111111
snapshotWaitCommand(),
112+
snapshotUpdateCommand(),
112113
))
113114

114115
//

internal/namespaces/instance/v1/custom_snapshot.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,44 @@ func snapshotWaitCommand() *core.Command {
142142
},
143143
}
144144
}
145+
146+
func snapshotUpdateCommand() *core.Command {
147+
return &core.Command{
148+
Short: `Update a snapshot`,
149+
Namespace: "instance",
150+
Resource: "snapshot",
151+
Verb: "update",
152+
ArgsType: reflect.TypeOf(instance.UpdateSnapshotRequest{}),
153+
ArgSpecs: core.ArgSpecs{
154+
{
155+
Name: "snapshot-id",
156+
Short: "UUID of the snapshot.",
157+
Required: true,
158+
Positional: true,
159+
},
160+
{
161+
Name: "name",
162+
Short: "Name of the snapshot.",
163+
},
164+
{
165+
Name: "tags.{index}",
166+
Short: "Tags of the snapshot.",
167+
},
168+
core.ZoneArgSpec(),
169+
},
170+
WaitFunc: func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
171+
snapshot := respI.(*instance.UpdateSnapshotResponse).Snapshot
172+
api := instance.NewAPI(core.ExtractClient(ctx))
173+
return api.WaitForSnapshot(&instance.WaitForSnapshotRequest{
174+
SnapshotID: snapshot.ID,
175+
Zone: snapshot.Zone,
176+
Timeout: scw.TimeDurationPtr(snapshotActionTimeout),
177+
RetryInterval: core.DefaultRetryInterval,
178+
})
179+
},
180+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
181+
api := instance.NewAPI(core.ExtractClient(ctx))
182+
return api.UpdateSnapshot(argsI.(*instance.UpdateSnapshotRequest))
183+
},
184+
}
185+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package instance
2+
3+
import (
4+
"testing"
5+
6+
"github.com/alecthomas/assert"
7+
"github.com/scaleway/scaleway-cli/v2/internal/core"
8+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
9+
)
10+
11+
func Test_UpdateSnapshot(t *testing.T) {
12+
t.Run("Simple", func(t *testing.T) {
13+
t.Run("Change tags", core.Test(&core.TestConfig{
14+
Commands: GetCommands(),
15+
BeforeFunc: core.BeforeFuncCombine(
16+
createVolume("Volume", 10, instance.VolumeVolumeTypeBSSD),
17+
core.ExecStoreBeforeCmd("CreateSnapshot", "scw instance snapshot create volume-id={{ .Volume.ID }} name=cli-test-snapshot-update-tags tags.0=foo tags.1=bar"),
18+
),
19+
Cmd: "scw instance snapshot update -w {{ .CreateSnapshot.Snapshot.ID }} tags.0=bar tags.1=foo",
20+
Check: core.TestCheckCombine(
21+
core.TestCheckGolden(),
22+
core.TestCheckExitCode(0),
23+
func(t *testing.T, ctx *core.CheckFuncCtx) {
24+
snapshot := ctx.Result.(*instance.Snapshot)
25+
assert.Equal(t, snapshot.Name, "cli-test-snapshot-update-tags")
26+
assert.Len(t, snapshot.Tags, 2)
27+
assert.Equal(t, snapshot.Tags[0], "bar")
28+
assert.Equal(t, snapshot.Tags[1], "foo")
29+
},
30+
),
31+
AfterFunc: core.AfterFuncCombine(
32+
deleteSnapshot("CreateSnapshot"),
33+
deleteVolume("Volume"),
34+
),
35+
}))
36+
t.Run("Change name", core.Test(&core.TestConfig{
37+
Commands: GetCommands(),
38+
BeforeFunc: core.BeforeFuncCombine(
39+
createVolume("Volume", 10, instance.VolumeVolumeTypeBSSD),
40+
core.ExecStoreBeforeCmd("CreateSnapshot", "scw instance snapshot create volume-id={{ .Volume.ID }} name=cli-test-snapshot-update-name tags.0=foo tags.1=bar"),
41+
),
42+
Cmd: "scw instance snapshot update -w {{ .CreateSnapshot.Snapshot.ID }} name=cli-test-snapshot-update-name-updated",
43+
Check: core.TestCheckCombine(
44+
core.TestCheckGolden(),
45+
core.TestCheckExitCode(0),
46+
func(t *testing.T, ctx *core.CheckFuncCtx) {
47+
snapshot := ctx.Result.(*instance.Snapshot)
48+
assert.Equal(t, snapshot.Name, "cli-test-snapshot-update-name-updated")
49+
assert.Len(t, snapshot.Tags, 2)
50+
assert.Equal(t, snapshot.Tags[0], "foo")
51+
assert.Equal(t, snapshot.Tags[1], "bar")
52+
},
53+
),
54+
AfterFunc: core.AfterFuncCombine(
55+
deleteSnapshot("CreateSnapshot"),
56+
deleteVolume("Volume"),
57+
),
58+
}))
59+
})
60+
}

internal/namespaces/instance/v1/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func createVolume(metaKey string, sizeInGb int, volumeType instance.VolumeVolume
6262
}
6363

6464
// deleteVolume deletes a volume previously registered in the context Meta at metaKey.
65-
func deleteVolume(metaKey string) core.AfterFunc {
65+
func deleteVolume(metaKey string) core.AfterFunc { //nolint: unparam
6666
return core.ExecAfterCmd("scw instance volume delete {{ ." + metaKey + ".ID }}")
6767
}
6868

0 commit comments

Comments
 (0)