Skip to content

Commit

Permalink
feat(instance): add support for unified in the backup command (#2349)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Jun 16, 2022
1 parent 664e01f commit 1f6dcdc
Show file tree
Hide file tree
Showing 16 changed files with 9,863 additions and 7,330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ EXAMPLES:
ARGS:
server-id ID of the server to backup.
[name=<generated>] Name of your backup.
[unified] Whether or not the type of the snapshot is unified.
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config

FLAGS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ EXAMPLES:
ARGS:
[name=<generated>] Name of the snapshot
volume-id UUID of the volume
[unified] Whether a snapshot is unified or not.
[tags.{index}] The tags of the snapshot
[project-id] Project ID to use. If none is passed the default project ID will be used
[volume-type] The volume type of the snapshot (unknown_volume_type | l_ssd | b_ssd | unified)
[organization-id] Organization ID to use. If none is passed the default organization ID will be used
[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)

Expand Down
3 changes: 2 additions & 1 deletion docs/commands/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,7 @@ scw instance server backup <server-id ...> [arg=value ...]
|------|---|-------------|
| server-id | Required | ID of the server to backup. |
| name | Default: `<generated>` | Name of your backup. |
| unified | | Whether or not the type of the snapshot is unified. |
| zone | Default: `fr-par-1` | Zone to target. If none is passed will use default zone from the config |


Expand Down Expand Up @@ -1927,9 +1928,9 @@ scw instance snapshot create [arg=value ...]
|------|---|-------------|
| name | Default: `<generated>` | Name of the snapshot |
| volume-id | Required | UUID of the volume |
| unified | | Whether a snapshot is unified or not. |
| tags.{index} | | The tags of the snapshot |
| project-id | | Project ID to use. If none is passed the default project ID will be used |
| volume-type | One of: `unknown_volume_type`, `l_ssd`, `b_ssd`, `unified` | The volume type of the snapshot |
| organization-id | | Organization ID to use. If none is passed the default organization ID will be used |
| 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 |

Expand Down
32 changes: 30 additions & 2 deletions internal/namespaces/instance/v1/custom_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ func serverBackupCommand() *core.Command {
Zone scw.Zone
ServerID string
Name string
Unified bool
}

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

client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
res, err := api.ServerAction(&instance.ServerActionRequest{
server, err := api.GetServer(&instance.GetServerRequest{
ServerID: args.ServerID,
Zone: args.Zone,
})
if err != nil {
return nil, err
}

req := &instance.ServerActionRequest{
Zone: args.Zone,
ServerID: args.ServerID,
Action: instance.ServerActionBackup,
Name: &args.Name,
})
Volumes: map[string]*instance.ServerActionRequestVolumeBackupTemplate{},
}
for _, v := range server.Server.Volumes {
var template *instance.ServerActionRequestVolumeBackupTemplate
if args.Unified {
template = &instance.ServerActionRequestVolumeBackupTemplate{
VolumeType: instance.SnapshotVolumeTypeUnified,
}
} else {
template = &instance.ServerActionRequestVolumeBackupTemplate{
VolumeType: instance.SnapshotVolumeType(v.VolumeType),
}
}
req.Volumes[v.ID] = template
}
res, err := api.ServerAction(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -792,6 +816,10 @@ Once your image is ready you will be able to create a new server based on this i
Short: `Name of your backup.`,
Default: core.RandomValueGenerator("backup"),
},
{
Name: "unified",
Short: "Whether or not the type of the snapshot is unified.",
},
core.ZoneArgSpec(),
},
Examples: []*core.Example{
Expand Down
23 changes: 23 additions & 0 deletions internal/namespaces/instance/v1/custom_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ func snapshotCreateBuilder(c *core.Command) *core.Command {
*instance.CreateSnapshotRequest
OrganizationID *string
ProjectID *string
Unified bool
}

renameOrganizationIDArgSpec(c.ArgSpecs)
renameProjectIDArgSpec(c.ArgSpecs)
c.ArgSpecs.DeleteByName("volume-type")
c.ArgSpecs.AddBefore("tags.{index}", &core.ArgSpec{
Name: "unified",
Short: "Whether a snapshot is unified or not.",
})

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

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

client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
if args.Unified {
request.VolumeType = instance.SnapshotVolumeTypeUnified
} else {
// If the snapshot is not unified, we need to set the snapshot volume type to the same type as the volume we target.
volume, err := api.GetVolume(&instance.GetVolumeRequest{
VolumeID: args.VolumeID,
Zone: args.Zone,
})
if err != nil {
return nil, err
}

request.VolumeType = instance.SnapshotVolumeType(volume.Volume.VolumeType)
}

return runner(ctx, request)
})

Expand Down
Loading

0 comments on commit 1f6dcdc

Please sign in to comment.