Skip to content

Commit 1f6dcdc

Browse files
authored
feat(instance): add support for unified in the backup command (#2349)
1 parent 664e01f commit 1f6dcdc

16 files changed

+9863
-7330
lines changed

cmd/scw/testdata/test-all-usage-instance-server-backup-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ EXAMPLES:
1818
ARGS:
1919
server-id ID of the server to backup.
2020
[name=<generated>] Name of your backup.
21+
[unified] Whether or not the type of the snapshot is unified.
2122
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config
2223

2324
FLAGS:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ EXAMPLES:
1818
ARGS:
1919
[name=<generated>] Name of the snapshot
2020
volume-id UUID of the volume
21+
[unified] Whether a snapshot is unified or not.
2122
[tags.{index}] The tags of the snapshot
2223
[project-id] Project ID to use. If none is passed the default project ID will be used
23-
[volume-type] The volume type of the snapshot (unknown_volume_type | l_ssd | b_ssd | unified)
2424
[organization-id] Organization ID to use. If none is passed the default organization ID will be used
2525
[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 | pl-waw-1)
2626

docs/commands/instance.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ scw instance server backup <server-id ...> [arg=value ...]
12561256
|------|---|-------------|
12571257
| server-id | Required | ID of the server to backup. |
12581258
| name | Default: `<generated>` | Name of your backup. |
1259+
| unified | | Whether or not the type of the snapshot is unified. |
12591260
| zone | Default: `fr-par-1` | Zone to target. If none is passed will use default zone from the config |
12601261

12611262

@@ -1927,9 +1928,9 @@ scw instance snapshot create [arg=value ...]
19271928
|------|---|-------------|
19281929
| name | Default: `<generated>` | Name of the snapshot |
19291930
| volume-id | Required | UUID of the volume |
1931+
| unified | | Whether a snapshot is unified or not. |
19301932
| tags.{index} | | The tags of the snapshot |
19311933
| project-id | | Project ID to use. If none is passed the default project ID will be used |
1932-
| volume-type | One of: `unknown_volume_type`, `l_ssd`, `b_ssd`, `unified` | The volume type of the snapshot |
19331934
| organization-id | | Organization ID to use. If none is passed the default organization ID will be used |
19341935
| zone | Default: `fr-par-1`<br />One of: `fr-par-1`, `fr-par-2`, `fr-par-3`, `nl-ams-1`, `nl-ams-2`, `pl-waw-1` | Zone to target. If none is passed will use default zone from the config |
19351936

internal/namespaces/instance/v1/custom_server.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ func serverBackupCommand() *core.Command {
733733
Zone scw.Zone
734734
ServerID string
735735
Name string
736+
Unified bool
736737
}
737738

738739
return &core.Command{
@@ -754,12 +755,35 @@ Once your image is ready you will be able to create a new server based on this i
754755

755756
client := core.ExtractClient(ctx)
756757
api := instance.NewAPI(client)
757-
res, err := api.ServerAction(&instance.ServerActionRequest{
758+
server, err := api.GetServer(&instance.GetServerRequest{
759+
ServerID: args.ServerID,
760+
Zone: args.Zone,
761+
})
762+
if err != nil {
763+
return nil, err
764+
}
765+
766+
req := &instance.ServerActionRequest{
758767
Zone: args.Zone,
759768
ServerID: args.ServerID,
760769
Action: instance.ServerActionBackup,
761770
Name: &args.Name,
762-
})
771+
Volumes: map[string]*instance.ServerActionRequestVolumeBackupTemplate{},
772+
}
773+
for _, v := range server.Server.Volumes {
774+
var template *instance.ServerActionRequestVolumeBackupTemplate
775+
if args.Unified {
776+
template = &instance.ServerActionRequestVolumeBackupTemplate{
777+
VolumeType: instance.SnapshotVolumeTypeUnified,
778+
}
779+
} else {
780+
template = &instance.ServerActionRequestVolumeBackupTemplate{
781+
VolumeType: instance.SnapshotVolumeType(v.VolumeType),
782+
}
783+
}
784+
req.Volumes[v.ID] = template
785+
}
786+
res, err := api.ServerAction(req)
763787
if err != nil {
764788
return nil, err
765789
}
@@ -792,6 +816,10 @@ Once your image is ready you will be able to create a new server based on this i
792816
Short: `Name of your backup.`,
793817
Default: core.RandomValueGenerator("backup"),
794818
},
819+
{
820+
Name: "unified",
821+
Short: "Whether or not the type of the snapshot is unified.",
822+
},
795823
core.ZoneArgSpec(),
796824
},
797825
Examples: []*core.Example{

internal/namespaces/instance/v1/custom_snapshot.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ func snapshotCreateBuilder(c *core.Command) *core.Command {
2121
*instance.CreateSnapshotRequest
2222
OrganizationID *string
2323
ProjectID *string
24+
Unified bool
2425
}
2526

2627
renameOrganizationIDArgSpec(c.ArgSpecs)
2728
renameProjectIDArgSpec(c.ArgSpecs)
29+
c.ArgSpecs.DeleteByName("volume-type")
30+
c.ArgSpecs.AddBefore("tags.{index}", &core.ArgSpec{
31+
Name: "unified",
32+
Short: "Whether a snapshot is unified or not.",
33+
})
2834

2935
c.ArgsType = reflect.TypeOf(customCreateSnapshotRequest{})
3036

@@ -39,6 +45,23 @@ func snapshotCreateBuilder(c *core.Command) *core.Command {
3945
request.Organization = args.OrganizationID
4046
request.Project = args.ProjectID
4147

48+
client := core.ExtractClient(ctx)
49+
api := instance.NewAPI(client)
50+
if args.Unified {
51+
request.VolumeType = instance.SnapshotVolumeTypeUnified
52+
} else {
53+
// If the snapshot is not unified, we need to set the snapshot volume type to the same type as the volume we target.
54+
volume, err := api.GetVolume(&instance.GetVolumeRequest{
55+
VolumeID: args.VolumeID,
56+
Zone: args.Zone,
57+
})
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
request.VolumeType = instance.SnapshotVolumeType(volume.Volume.VolumeType)
63+
}
64+
4265
return runner(ctx, request)
4366
})
4467

0 commit comments

Comments
 (0)