Skip to content

Commit

Permalink
feat(instance): allows setting root_volume id (#1373)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax committed Jul 13, 2022
1 parent cbc451c commit d998d65
Show file tree
Hide file tree
Showing 5 changed files with 2,299 additions and 3 deletions.
7 changes: 4 additions & 3 deletions docs/resources/instance_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ To retrieve more information by label please use: ```scw marketplace image get l
~> **Important:** When updating `placement_group_id` the `state` must be set to `stopped`, otherwise it will fail.

- `root_volume` - (Optional) Root [volume](https://developers.scaleway.com/en/products/instance/api/#volumes-7e8a39) attached to the server on creation.
- `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
- `size_in_gb` - (Required) Size of the root volume in gigabytes.
To find the right size use [this endpoint](https://api.scaleway.com/instance/v1/zones/fr-par-1/products/servers) and
check the `volumes_constraint.{min|max}_size` (in bytes) for your `commercial_type`.
Updates to this field will recreate a new resource.
To find the right size use [this endpoint](https://api.scaleway.com/instance/v1/zones/fr-par-1/products/servers) and
check the `volumes_constraint.{min|max}_size` (in bytes) for your `commercial_type`.
Updates to this field will recreate a new resource.
- `volume_type` - (Optional) Volume type of root volume, can be `b_ssd` or `l_ssd`, default value depends on server type
- `delete_on_termination` - (Defaults to `true`) Forces deletion of the root volume on instance termination.

Expand Down
1 change: 1 addition & 0 deletions scaleway/helpers_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func sanitizeVolumeMap(serverName string, volumes map[string]*instance.VolumeSer
v = &instance.VolumeServerTemplate{
ID: v.ID,
Name: v.Name,
Boot: v.Boot,
}
// For the root volume (index 0) if the size is 0, it is considered as a volume created from an image.
// The size is not passed to the API, so it's computed by the API
Expand Down
4 changes: 4 additions & 0 deletions scaleway/resource_instance_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func resourceScalewayInstanceServer() *schema.Resource {
"volume_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Volume ID of the root volume",
},
},
Expand Down Expand Up @@ -333,6 +334,7 @@ func resourceScalewayInstanceServerCreate(ctx context.Context, d *schema.Resourc
isBoot := expandBoolPtr(d.Get("root_volume.0.boot"))
volumeType := d.Get("root_volume.0.volume_type").(string)
sizeInput := d.Get("root_volume.0.size_in_gb").(int)
rootVolumeID := expandZonedID(d.Get("root_volume.0.volume_id").(string)).ID

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

req.Volumes["0"] = &instance.VolumeServerTemplate{
Name: newRandomName("vol"), // name is ignored by the API, any name will work here
ID: rootVolumeID,
VolumeType: instance.VolumeVolumeType(volumeType),
Size: size,
Boot: *isBoot,
Expand Down
37 changes: 37 additions & 0 deletions scaleway/resource_instance_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,43 @@ func TestAccScalewayInstanceServer_RootVolume_Boot(t *testing.T) {
})
}

func TestAccScalewayInstanceServer_RootVolume_ID(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayInstanceServerDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_instance_volume" "server_volume" {
type = "b_ssd"
name = "tf_tests_rootvolume"
size_in_gb = 10
}
resource "scaleway_instance_server" "base" {
image = "ubuntu_focal"
type = "DEV1-S"
state = "stopped"
root_volume {
volume_id = scaleway_instance_volume.server_volume.id
boot = true
delete_on_termination = false
}
tags = [ "terraform-test", "scaleway_instance_server", "root_volume" ]
}`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstanceServerExists(tt, "scaleway_instance_server.base"),
resource.TestCheckResourceAttrPair("scaleway_instance_server.base", "root_volume.0.volume_id", "scaleway_instance_volume.server_volume", "id"),
resource.TestCheckResourceAttrPair("scaleway_instance_server.base", "root_volume.0.size_in_gb", "scaleway_instance_volume.server_volume", "size_in_gb"),
),
},
},
})
}

func TestAccScalewayInstanceServer_Basic(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()
Expand Down

0 comments on commit d998d65

Please sign in to comment.