Skip to content

Commit 5a71eff

Browse files
authored
feat(instance): add support for bootscript (#652)
1 parent fb6acc9 commit 5a71eff

4 files changed

+4170
-1
lines changed

docs/resources/instance_server.md

+4
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ attached to the server. Updates to this field will trigger a stop/start of the s
175175

176176
- `value` - (Required) The user data content. It could be a string or a file content using [file](https://www.terraform.io/docs/configuration/functions/file.html) or [filebase64](https://www.terraform.io/docs/configuration/functions/filebase64.html) for example.
177177

178+
- `boot_type` - The boot Type of the server. Possible values are: `local`, `bootscript` or `rescue`.
179+
180+
- `bootscript_id` - The ID of the bootscript to use (set boot_type to `bootscript`).
181+
178182
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the server should be created.
179183

180184
- `organization_id` - (Defaults to [provider](../index.md#organization_id) `organization_id`) The ID of the organization the server is associated with.

scaleway/resource_instance_server.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,20 @@ func resourceScalewayInstanceServer() *schema.Resource {
174174
},
175175
"boot_type": {
176176
Type: schema.TypeString,
177-
Computed: true,
177+
Optional: true,
178178
Description: "The boot type of the server",
179+
Default: instance.BootTypeLocal,
180+
ValidateFunc: validation.StringInSlice([]string{
181+
instance.BootTypeLocal.String(),
182+
instance.BootTypeRescue.String(),
183+
instance.BootTypeBootscript.String(),
184+
}, false),
185+
},
186+
"bootscript_id": {
187+
Type: schema.TypeString,
188+
Optional: true,
189+
Description: "ID of the target bootscript (set boot_type to bootscript)",
190+
ValidateFunc: validationUUID(),
179191
},
180192
"cloud_init": {
181193
Type: schema.TypeString,
@@ -259,6 +271,15 @@ func resourceScalewayInstanceServerCreate(ctx context.Context, d *schema.Resourc
259271
Tags: expandStrings(d.Get("tags")),
260272
}
261273

274+
if bootScriptID, ok := d.GetOk("bootscript_id"); ok {
275+
req.Bootscript = expandStringPtr(bootScriptID)
276+
}
277+
278+
if bootType, ok := d.GetOk("boot_type"); ok {
279+
bootType := instance.BootType(bootType.(string))
280+
req.BootType = &bootType
281+
}
282+
262283
if ipID, ok := d.GetOk("ip_id"); ok {
263284
req.PublicIP = expandStringPtr(expandZonedID(ipID).ID)
264285
}
@@ -360,6 +381,7 @@ func resourceScalewayInstanceServerRead(ctx context.Context, d *schema.ResourceD
360381
_ = d.Set("zone", string(zone))
361382
_ = d.Set("name", response.Server.Name)
362383
_ = d.Set("boot_type", response.Server.BootType)
384+
_ = d.Set("bootscript_id", response.Server.Bootscript.ID)
363385
_ = d.Set("type", response.Server.CommercialType)
364386
_ = d.Set("tags", response.Server.Tags)
365387
_ = d.Set("security_group_id", newZonedID(zone, response.Server.SecurityGroup.ID).String())
@@ -579,6 +601,17 @@ func resourceScalewayInstanceServerUpdate(ctx context.Context, d *schema.Resourc
579601
}
580602
}
581603

604+
if d.HasChanges("boot_type") {
605+
bootType := instance.BootType(d.Get("boot_type").(string))
606+
updateRequest.BootType = &bootType
607+
forceReboot = true
608+
}
609+
610+
if d.HasChanges("bootscript_id") {
611+
updateRequest.Bootscript = expandStringPtr(d.Get("bootscript_id").(string))
612+
forceReboot = true
613+
}
614+
582615
////
583616
// Update server user data
584617
////

scaleway/resource_instance_server_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -903,3 +903,36 @@ resource "scaleway_instance_server" "base" {
903903
additional_volume_ids = [ %s ]
904904
}`, additionalVolumeResources, baseVolume, strings.Join(additionalVolumeIDs, ","))
905905
}
906+
907+
func TestAccScalewayInstanceServer_Bootscript(t *testing.T) {
908+
tt := NewTestTools(t)
909+
defer tt.Cleanup()
910+
// Quick tip to get all the different bootscript:
911+
// curl -sH "X-Auth-Token: $(scw config get secret-key)" https://api.scaleway.com/instance/v1/zones/fr-par-1/bootscripts | jq -r '.bootscripts[] | [.id, .architecture, .title] | @tsv'
912+
bootscript := "7decf961-d3e9-4711-93c7-b16c254e99b9"
913+
resource.ParallelTest(t, resource.TestCase{
914+
PreCheck: func() { testAccPreCheck(t) },
915+
ProviderFactories: tt.ProviderFactories,
916+
CheckDestroy: testAccCheckScalewayInstanceServerDestroy(tt),
917+
Steps: []resource.TestStep{
918+
{
919+
Config: fmt.Sprintf(`
920+
data "scaleway_instance_image" "ubuntu_focal" {
921+
name = "Ubuntu 20.04 Focal Fossa"
922+
}
923+
924+
resource "scaleway_instance_server" "base" {
925+
type = "DEV1-S"
926+
image = data.scaleway_instance_image.ubuntu_focal.id
927+
boot_type = "bootscript"
928+
bootscript_id = "%s"
929+
}
930+
`, bootscript),
931+
Check: resource.ComposeTestCheckFunc(
932+
testAccCheckScalewayInstanceServerExists(tt, "scaleway_instance_server.base"),
933+
resource.TestCheckResourceAttr("scaleway_instance_server.base", "bootscript_id", bootscript),
934+
),
935+
},
936+
},
937+
})
938+
}

0 commit comments

Comments
 (0)