Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instance_server): allow updating root_volume size for non l_ssd #2842

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/resources/instance_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ To retrieve more information by label please use: ```scw marketplace image get l
- `size_in_gb` - (Required) Size of the root volume in gigabytes.
To find the right size use [this endpoint](https://www.scaleway.com/en/developers/api/instance/#path-instances-list-all-instances) and
check the `volumes_constraint.{min|max}_size` (in bytes) for your `commercial_type`.
Updates to this field will recreate a new resource.
Depending on `volume_type`, updates to this field may recreate a new resource.
- `volume_type` - (Optional) Volume type of root volume, can be `b_ssd`, `l_ssd` or `sbs_volume`, default value depends on server type
- `delete_on_termination` - (Defaults to `true`) Forces deletion of the root volume on instance termination.
- `sbs_iops` - (Optional) Choose IOPS of your sbs volume, has to be used with `sbs_volume` for root volume type.
Expand Down
29 changes: 28 additions & 1 deletion internal/services/instance/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func ResourceServer() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
Description: "Size of the root volume in gigabytes",
},
"volume_type": {
Expand Down Expand Up @@ -374,6 +373,7 @@ func ResourceServer() *schema.Resource {
),
customDiffInstanceServerType,
customDiffInstanceServerImage,
customDiffInstanceRootVolumeSize,
),
}
}
Expand Down Expand Up @@ -1169,6 +1169,33 @@ func instanceServerCanMigrate(api *instanceSDK.API, server *instanceSDK.Server,
return nil
}

func customDiffInstanceRootVolumeSize(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if !diff.HasChange("root_volume.0.size_in_gb") || diff.Id() == "" {
return nil
}

instanceAPI, zone, id, err := NewAPIWithZoneAndID(meta, diff.Id())
if err != nil {
return err
}

resp, err := instanceAPI.GetServer(&instanceSDK.GetServerRequest{
Zone: zone,
ServerID: id,
})
if err != nil {
return fmt.Errorf("failed to check server root volume type: %w", err)
}

if rootVolume, hasRootVolume := resp.Server.Volumes["0"]; hasRootVolume {
if rootVolume.VolumeType == instanceSDK.VolumeServerVolumeTypeLSSD {
return diff.ForceNew("root_volume.0.size_in_gb")
}
}

return nil
}

func customDiffInstanceServerType(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if !diff.HasChange("type") || diff.Id() == "" {
return nil
Expand Down
10 changes: 10 additions & 0 deletions internal/services/instance/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func TestAccServer_Minimal2(t *testing.T) {
func TestAccServer_RootVolume1(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

serverID := ""

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
Expand All @@ -166,6 +169,7 @@ func TestAccServer_RootVolume1(t *testing.T) {
isServerPresent(tt, "scaleway_instance_server.base"),
serverHasNewVolume(tt, "scaleway_instance_server.base"),
resource.TestCheckResourceAttr("scaleway_instance_server.base", "root_volume.0.size_in_gb", "10"),
acctest.CheckResourceIDPersisted("scaleway_instance_server.base", &serverID),
),
},
{
Expand All @@ -183,6 +187,7 @@ func TestAccServer_RootVolume1(t *testing.T) {
isServerPresent(tt, "scaleway_instance_server.base"),
serverHasNewVolume(tt, "scaleway_instance_server.base"),
resource.TestCheckResourceAttr("scaleway_instance_server.base", "root_volume.0.size_in_gb", "20"),
acctest.CheckResourceIDChanged("scaleway_instance_server.base", &serverID), // Server should have been re-created as l_ssd cannot be resized.
),
},
},
Expand Down Expand Up @@ -1702,6 +1707,9 @@ func TestAccServer_BlockExternal(t *testing.T) {
func TestAccServer_BlockExternalRootVolume(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

serverID := ""

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
Expand All @@ -1725,6 +1733,7 @@ func TestAccServer_BlockExternalRootVolume(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.volume_type", string(instanceSDK.VolumeVolumeTypeSbsVolume)),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.sbs_iops", "15000"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.size_in_gb", "50"),
acctest.CheckResourceIDPersisted("scaleway_instance_server.main", &serverID),
),
},
{
Expand All @@ -1745,6 +1754,7 @@ func TestAccServer_BlockExternalRootVolume(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.volume_type", string(instanceSDK.VolumeVolumeTypeSbsVolume)),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.sbs_iops", "15000"),
resource.TestCheckResourceAttr("scaleway_instance_server.main", "root_volume.0.size_in_gb", "60"),
acctest.CheckResourceIDPersisted("scaleway_instance_server.main", &serverID),
),
},
},
Expand Down
Loading
Loading