Skip to content

Commit 82c9f80

Browse files
authored
feat(instance_server): allow root_volume size update (#2745)
* feat(instance_server): allow root_volume size update * add tests * record new test
1 parent 148e0c8 commit 82c9f80

6 files changed

+5055
-1092
lines changed

internal/services/instance/helpers_instance_block.go

+32
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ type GetUnknownVolumeRequest struct {
2222
Zone scw.Zone
2323
}
2424

25+
type ResizeUnknownVolumeRequest struct {
26+
VolumeID string
27+
Zone scw.Zone
28+
Size *scw.Size
29+
}
30+
2531
type UnknownVolume struct {
2632
Zone scw.Zone
2733
ID string
@@ -129,6 +135,32 @@ func (api *BlockAndInstanceAPI) GetUnknownVolume(req *GetUnknownVolumeRequest, o
129135
return vol, nil
130136
}
131137

138+
func (api *BlockAndInstanceAPI) ResizeUnknownVolume(req *ResizeUnknownVolumeRequest, opts ...scw.RequestOption) error {
139+
unknownVolume, err := api.GetUnknownVolume(&GetUnknownVolumeRequest{
140+
VolumeID: req.VolumeID,
141+
Zone: req.Zone,
142+
}, opts...)
143+
if err != nil {
144+
return err
145+
}
146+
147+
if unknownVolume.IsBlockVolume() {
148+
_, err = api.blockAPI.UpdateVolume(&block.UpdateVolumeRequest{
149+
Zone: req.Zone,
150+
VolumeID: req.VolumeID,
151+
Size: req.Size,
152+
}, opts...)
153+
} else {
154+
_, err = api.API.UpdateVolume(&instance.UpdateVolumeRequest{
155+
Zone: req.Zone,
156+
VolumeID: req.VolumeID,
157+
Size: req.Size,
158+
}, opts...)
159+
}
160+
161+
return err
162+
}
163+
132164
// newAPIWithZone returns a new instance API and the zone for a Create request
133165
func instanceAndBlockAPIWithZone(d *schema.ResourceData, m interface{}) (*BlockAndInstanceAPI, scw.Zone, error) {
134166
instanceAPI := instance.NewAPI(meta.ExtractScwClient(m))

internal/services/instance/server.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ func ResourceInstanceServerUpdate(ctx context.Context, d *schema.ResourceData, m
817817
}
818818

819819
if d.HasChanges("additional_volume_ids", "root_volume") {
820-
volumes, err := instanceServerVolumesTemplatesUpdate(ctx, d, api, zone, isStopped)
820+
volumes, err := instanceServerVolumesUpdate(ctx, d, api, zone, isStopped)
821821
if err != nil {
822822
return diag.FromErr(err)
823823
}
@@ -1396,12 +1396,23 @@ func ResourceInstanceServerUpdateRootVolumeIOPS(ctx context.Context, api *BlockA
13961396
return nil
13971397
}
13981398

1399-
// instanceServerVolumesTemplatesUpdate returns the list of volumes templates that should be updated for the server.
1399+
// instanceServerVolumesUpdate updates root_volume size and returns the list of volumes templates that should be updated for the server.
14001400
// It uses root_volume and additional_volume_ids to build the volumes templates.
1401-
func instanceServerVolumesTemplatesUpdate(ctx context.Context, d *schema.ResourceData, api *BlockAndInstanceAPI, zone scw.Zone, serverIsStopped bool) (map[string]*instanceSDK.VolumeServerTemplate, error) {
1401+
func instanceServerVolumesUpdate(ctx context.Context, d *schema.ResourceData, api *BlockAndInstanceAPI, zone scw.Zone, serverIsStopped bool) (map[string]*instanceSDK.VolumeServerTemplate, error) {
14021402
volumes := map[string]*instanceSDK.VolumeServerTemplate{}
14031403
raw, hasAdditionalVolumes := d.GetOk("additional_volume_ids")
14041404

1405+
if d.HasChange("root_volume.0.size_in_gb") {
1406+
err := api.ResizeUnknownVolume(&ResizeUnknownVolumeRequest{
1407+
VolumeID: zonal.ExpandID(d.Get("root_volume.0.volume_id")).ID,
1408+
Zone: zone,
1409+
Size: scw.SizePtr(scw.Size(d.Get("root_volume.0.size_in_gb").(int)) * scw.GB),
1410+
}, scw.WithContext(ctx))
1411+
if err != nil {
1412+
return nil, err
1413+
}
1414+
}
1415+
14051416
volumes["0"] = &instanceSDK.VolumeServerTemplate{
14061417
ID: scw.StringPtr(zonal.ExpandID(d.Get("root_volume.0.volume_id")).ID),
14071418
Name: scw.StringPtr(types.NewRandomName("vol")), // name is ignored by the API, any name will work here

internal/services/instance/server_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ func TestAccServer_RootVolume1(t *testing.T) {
170170
Check: resource.ComposeTestCheckFunc(
171171
isServerPresent(tt, "scaleway_instance_server.base"),
172172
serverHasNewVolume(tt, "scaleway_instance_server.base"),
173+
resource.TestCheckResourceAttr("scaleway_instance_server.base", "root_volume.0.size_in_gb", "10"),
174+
),
175+
},
176+
{
177+
Config: `
178+
resource "scaleway_instance_server" "base" {
179+
image = "ubuntu_focal"
180+
type = "DEV1-S"
181+
root_volume {
182+
size_in_gb = 20
183+
delete_on_termination = true
184+
}
185+
tags = [ "terraform-test", "scaleway_instance_server", "root_volume" ]
186+
}`,
187+
Check: resource.ComposeTestCheckFunc(
188+
isServerPresent(tt, "scaleway_instance_server.base"),
189+
serverHasNewVolume(tt, "scaleway_instance_server.base"),
190+
resource.TestCheckResourceAttr("scaleway_instance_server.base", "root_volume.0.size_in_gb", "20"),
173191
),
174192
},
175193
},
@@ -2008,6 +2026,26 @@ func TestAccServer_BlockExternalRootVolume(t *testing.T) {
20082026
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.size_in_gb", "50"),
20092027
),
20102028
},
2029+
{
2030+
Config: `
2031+
resource "scaleway_instance_server" "main" {
2032+
name = "tf-tests-instance-block-external-root-volume"
2033+
image = "ubuntu_jammy"
2034+
type = "PLAY2-PICO"
2035+
root_volume {
2036+
volume_type = "sbs_volume"
2037+
size_in_gb = 60
2038+
sbs_iops = 15000
2039+
}
2040+
}`,
2041+
Check: resource.ComposeTestCheckFunc(
2042+
resource.TestCheckResourceAttr("scaleway_instance_server.main", "type", "PLAY2-PICO"),
2043+
resource.TestCheckResourceAttr("scaleway_instance_server.main", "additional_volume_ids.#", "0"),
2044+
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.volume_type", string(instanceSDK.VolumeVolumeTypeSbsVolume)),
2045+
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.sbs_iops", "15000"),
2046+
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.size_in_gb", "60"),
2047+
),
2048+
},
20112049
},
20122050
})
20132051
}

internal/services/instance/testdata/private-nic-with-ipam.cassette.yaml

+502-551
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)