Skip to content

Commit 3d9d4ff

Browse files
authored
fix(instance_server): allow updating root_volume size for non l_ssd (#2842)
* fix(instance_server): allow updating root_volume.0.size_in_gb for non l_ssd volumes * update cassettes * update doc
1 parent 08ed204 commit 3d9d4ff

File tree

5 files changed

+1237
-2147
lines changed

5 files changed

+1237
-2147
lines changed

docs/resources/instance_server.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ To retrieve more information by label please use: ```scw marketplace image get l
213213
- `size_in_gb` - (Required) Size of the root volume in gigabytes.
214214
To find the right size use [this endpoint](https://www.scaleway.com/en/developers/api/instance/#path-instances-list-all-instances) and
215215
check the `volumes_constraint.{min|max}_size` (in bytes) for your `commercial_type`.
216-
Updates to this field will recreate a new resource.
216+
Depending on `volume_type`, updates to this field may recreate a new resource.
217217
- `volume_type` - (Optional) Volume type of root volume, can be `b_ssd`, `l_ssd` or `sbs_volume`, default value depends on server type
218218
- `delete_on_termination` - (Defaults to `true`) Forces deletion of the root volume on instance termination.
219219
- `sbs_iops` - (Optional) Choose IOPS of your sbs volume, has to be used with `sbs_volume` for root volume type.

internal/services/instance/server.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func ResourceServer() *schema.Resource {
121121
Type: schema.TypeInt,
122122
Optional: true,
123123
Computed: true,
124-
ForceNew: true,
125124
Description: "Size of the root volume in gigabytes",
126125
},
127126
"volume_type": {
@@ -374,6 +373,7 @@ func ResourceServer() *schema.Resource {
374373
),
375374
customDiffInstanceServerType,
376375
customDiffInstanceServerImage,
376+
customDiffInstanceRootVolumeSize,
377377
),
378378
}
379379
}
@@ -1169,6 +1169,33 @@ func instanceServerCanMigrate(api *instanceSDK.API, server *instanceSDK.Server,
11691169
return nil
11701170
}
11711171

1172+
func customDiffInstanceRootVolumeSize(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
1173+
if !diff.HasChange("root_volume.0.size_in_gb") || diff.Id() == "" {
1174+
return nil
1175+
}
1176+
1177+
instanceAPI, zone, id, err := NewAPIWithZoneAndID(meta, diff.Id())
1178+
if err != nil {
1179+
return err
1180+
}
1181+
1182+
resp, err := instanceAPI.GetServer(&instanceSDK.GetServerRequest{
1183+
Zone: zone,
1184+
ServerID: id,
1185+
})
1186+
if err != nil {
1187+
return fmt.Errorf("failed to check server root volume type: %w", err)
1188+
}
1189+
1190+
if rootVolume, hasRootVolume := resp.Server.Volumes["0"]; hasRootVolume {
1191+
if rootVolume.VolumeType == instanceSDK.VolumeServerVolumeTypeLSSD {
1192+
return diff.ForceNew("root_volume.0.size_in_gb")
1193+
}
1194+
}
1195+
1196+
return nil
1197+
}
1198+
11721199
func customDiffInstanceServerType(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
11731200
if !diff.HasChange("type") || diff.Id() == "" {
11741201
return nil

internal/services/instance/server_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ func TestAccServer_Minimal2(t *testing.T) {
146146
func TestAccServer_RootVolume1(t *testing.T) {
147147
tt := acctest.NewTestTools(t)
148148
defer tt.Cleanup()
149+
150+
serverID := ""
151+
149152
resource.ParallelTest(t, resource.TestCase{
150153
PreCheck: func() { acctest.PreCheck(t) },
151154
ProviderFactories: tt.ProviderFactories,
@@ -166,6 +169,7 @@ func TestAccServer_RootVolume1(t *testing.T) {
166169
isServerPresent(tt, "scaleway_instance_server.base"),
167170
serverHasNewVolume(tt, "scaleway_instance_server.base"),
168171
resource.TestCheckResourceAttr("scaleway_instance_server.base", "root_volume.0.size_in_gb", "10"),
172+
acctest.CheckResourceIDPersisted("scaleway_instance_server.base", &serverID),
169173
),
170174
},
171175
{
@@ -183,6 +187,7 @@ func TestAccServer_RootVolume1(t *testing.T) {
183187
isServerPresent(tt, "scaleway_instance_server.base"),
184188
serverHasNewVolume(tt, "scaleway_instance_server.base"),
185189
resource.TestCheckResourceAttr("scaleway_instance_server.base", "root_volume.0.size_in_gb", "20"),
190+
acctest.CheckResourceIDChanged("scaleway_instance_server.base", &serverID), // Server should have been re-created as l_ssd cannot be resized.
186191
),
187192
},
188193
},
@@ -1702,6 +1707,9 @@ func TestAccServer_BlockExternal(t *testing.T) {
17021707
func TestAccServer_BlockExternalRootVolume(t *testing.T) {
17031708
tt := acctest.NewTestTools(t)
17041709
defer tt.Cleanup()
1710+
1711+
serverID := ""
1712+
17051713
resource.ParallelTest(t, resource.TestCase{
17061714
PreCheck: func() { acctest.PreCheck(t) },
17071715
ProviderFactories: tt.ProviderFactories,
@@ -1725,6 +1733,7 @@ func TestAccServer_BlockExternalRootVolume(t *testing.T) {
17251733
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.volume_type", string(instanceSDK.VolumeVolumeTypeSbsVolume)),
17261734
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.sbs_iops", "15000"),
17271735
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.size_in_gb", "50"),
1736+
acctest.CheckResourceIDPersisted("scaleway_instance_server.main", &serverID),
17281737
),
17291738
},
17301739
{
@@ -1745,6 +1754,7 @@ func TestAccServer_BlockExternalRootVolume(t *testing.T) {
17451754
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.volume_type", string(instanceSDK.VolumeVolumeTypeSbsVolume)),
17461755
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.sbs_iops", "15000"),
17471756
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.size_in_gb", "60"),
1757+
acctest.CheckResourceIDPersisted("scaleway_instance_server.main", &serverID),
17481758
),
17491759
},
17501760
},

0 commit comments

Comments
 (0)