Skip to content

Commit d998d65

Browse files
authored
feat(instance): allows setting root_volume id (#1373)
1 parent cbc451c commit d998d65

5 files changed

+2299
-3
lines changed

docs/resources/instance_server.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ To retrieve more information by label please use: ```scw marketplace image get l
156156
~> **Important:** When updating `placement_group_id` the `state` must be set to `stopped`, otherwise it will fail.
157157

158158
- `root_volume` - (Optional) Root [volume](https://developers.scaleway.com/en/products/instance/api/#volumes-7e8a39) attached to the server on creation.
159+
- `volume_id` - (Optional) The volume ID of the root volume of the server, allows you to create server with an existing volume. If empty, will be computed to a created volume ID
159160
- `size_in_gb` - (Required) Size of the root volume in gigabytes.
160-
To find the right size use [this endpoint](https://api.scaleway.com/instance/v1/zones/fr-par-1/products/servers) and
161-
check the `volumes_constraint.{min|max}_size` (in bytes) for your `commercial_type`.
162-
Updates to this field will recreate a new resource.
161+
To find the right size use [this endpoint](https://api.scaleway.com/instance/v1/zones/fr-par-1/products/servers) and
162+
check the `volumes_constraint.{min|max}_size` (in bytes) for your `commercial_type`.
163+
Updates to this field will recreate a new resource.
163164
- `volume_type` - (Optional) Volume type of root volume, can be `b_ssd` or `l_ssd`, default value depends on server type
164165
- `delete_on_termination` - (Defaults to `true`) Forces deletion of the root volume on instance termination.
165166

scaleway/helpers_instance.go

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ func sanitizeVolumeMap(serverName string, volumes map[string]*instance.VolumeSer
254254
v = &instance.VolumeServerTemplate{
255255
ID: v.ID,
256256
Name: v.Name,
257+
Boot: v.Boot,
257258
}
258259
// For the root volume (index 0) if the size is 0, it is considered as a volume created from an image.
259260
// The size is not passed to the API, so it's computed by the API

scaleway/resource_instance_server.go

+4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func resourceScalewayInstanceServer() *schema.Resource {
125125
"volume_id": {
126126
Type: schema.TypeString,
127127
Computed: true,
128+
Optional: true,
128129
Description: "Volume ID of the root volume",
129130
},
130131
},
@@ -333,6 +334,7 @@ func resourceScalewayInstanceServerCreate(ctx context.Context, d *schema.Resourc
333334
isBoot := expandBoolPtr(d.Get("root_volume.0.boot"))
334335
volumeType := d.Get("root_volume.0.volume_type").(string)
335336
sizeInput := d.Get("root_volume.0.size_in_gb").(int)
337+
rootVolumeID := expandZonedID(d.Get("root_volume.0.volume_id").(string)).ID
336338

337339
// If the volumeType is not defined, define it depending of the offer
338340
if volumeType == "" {
@@ -355,6 +357,8 @@ func resourceScalewayInstanceServerCreate(ctx context.Context, d *schema.Resourc
355357
}
356358

357359
req.Volumes["0"] = &instance.VolumeServerTemplate{
360+
Name: newRandomName("vol"), // name is ignored by the API, any name will work here
361+
ID: rootVolumeID,
358362
VolumeType: instance.VolumeVolumeType(volumeType),
359363
Size: size,
360364
Boot: *isBoot,

scaleway/resource_instance_server_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,43 @@ func TestAccScalewayInstanceServer_RootVolume_Boot(t *testing.T) {
250250
})
251251
}
252252

253+
func TestAccScalewayInstanceServer_RootVolume_ID(t *testing.T) {
254+
tt := NewTestTools(t)
255+
defer tt.Cleanup()
256+
resource.ParallelTest(t, resource.TestCase{
257+
PreCheck: func() { testAccPreCheck(t) },
258+
ProviderFactories: tt.ProviderFactories,
259+
CheckDestroy: testAccCheckScalewayInstanceServerDestroy(tt),
260+
Steps: []resource.TestStep{
261+
{
262+
Config: `
263+
resource "scaleway_instance_volume" "server_volume" {
264+
type = "b_ssd"
265+
name = "tf_tests_rootvolume"
266+
size_in_gb = 10
267+
}
268+
269+
resource "scaleway_instance_server" "base" {
270+
image = "ubuntu_focal"
271+
type = "DEV1-S"
272+
state = "stopped"
273+
root_volume {
274+
volume_id = scaleway_instance_volume.server_volume.id
275+
boot = true
276+
delete_on_termination = false
277+
}
278+
tags = [ "terraform-test", "scaleway_instance_server", "root_volume" ]
279+
}`,
280+
Check: resource.ComposeTestCheckFunc(
281+
testAccCheckScalewayInstanceServerExists(tt, "scaleway_instance_server.base"),
282+
resource.TestCheckResourceAttrPair("scaleway_instance_server.base", "root_volume.0.volume_id", "scaleway_instance_volume.server_volume", "id"),
283+
resource.TestCheckResourceAttrPair("scaleway_instance_server.base", "root_volume.0.size_in_gb", "scaleway_instance_volume.server_volume", "size_in_gb"),
284+
),
285+
},
286+
},
287+
})
288+
}
289+
253290
func TestAccScalewayInstanceServer_Basic(t *testing.T) {
254291
tt := NewTestTools(t)
255292
defer tt.Cleanup()

0 commit comments

Comments
 (0)