Skip to content

Commit fcd778d

Browse files
authored
fix(baremetal): unexpected reinstall on ssh key ids changes (#1556)
1 parent da17044 commit fcd778d

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

docs/resources/baremetal_server.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ The following arguments are supported:
3838
Use [this endpoint](https://developers.scaleway.com/en/products/baremetal/api/#get-87598a) to find the right OS ID.
3939
~> **Important:** Updates to `os` will reinstall the server.
4040
- `ssh_key_ids` - (Required) List of SSH keys allowed to connect to the server.
41-
~> **Important:** Updates to `ssh_key_ids` will reinstall the server.
41+
~> **Important:** Updates to `ssh_key_ids` will not take effect on the server, it requires to reinstall it. To do so please set 'reinstall_on_ssh_key_changes' argument to true.
42+
- `reinstall_on_ssh_key_changes` - (Optional) If True, this boolean allows to reinstall the server on SSH key IDs changes.
4243
- `name` - (Optional) The name of the server.
4344
- `hostname` - (Optional) The hostname of the server.
4445
- `description` - (Optional) A description for the server.

scaleway/helpers_baremetal.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,15 @@ func waitForBaremetalServerInstall(ctx context.Context, api *baremetal.API, zone
133133

134134
return server, err
135135
}
136+
137+
func baremetalInstallServer(ctx context.Context, d *schema.ResourceData, baremetalAPI *baremetal.API, installServerRequest *baremetal.InstallServerRequest) error {
138+
installServerRequest.OsID = expandID(d.Get("os"))
139+
installServerRequest.SSHKeyIDs = expandStrings(d.Get("ssh_key_ids"))
140+
141+
_, err := baremetalAPI.InstallServer(installServerRequest, scw.WithContext(ctx))
142+
if err != nil {
143+
return err
144+
}
145+
146+
return nil
147+
}

scaleway/resource_baremetal_server.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,17 @@ func resourceScalewayBaremetalServer() *schema.Resource {
6767
Type: schema.TypeString,
6868
ValidateFunc: validationUUID(),
6969
},
70-
Required: true,
71-
Description: "Array of SSH key IDs allowed to SSH to the server",
70+
Required: true,
71+
Description: `Array of SSH key IDs allowed to SSH to the server
72+
73+
**NOTE** : If you are attempting to update your SSH key IDs, it will induce the reinstall of your server.
74+
If this behaviour is wanted, please set 'reinstall_on_ssh_key_changes' argument to true.`,
75+
},
76+
"reinstall_on_ssh_key_changes": {
77+
Type: schema.TypeBool,
78+
Optional: true,
79+
Default: false,
80+
Description: "If True, this boolean allows to reinstall the server on SSH key IDs changes",
7281
},
7382
"description": {
7483
Type: schema.TypeString,
@@ -257,16 +266,14 @@ func resourceScalewayBaremetalServerUpdate(ctx context.Context, d *schema.Resour
257266
}
258267
}
259268

260-
if d.HasChanges("os", "ssh_key_ids") {
261-
installReq := &baremetal.InstallServerRequest{
262-
Zone: zonedID.Zone,
263-
ServerID: zonedID.ID,
264-
OsID: expandZonedID(d.Get("os")).ID,
265-
Hostname: expandStringWithDefault(d.Get("hostname"), d.Get("name").(string)),
266-
SSHKeyIDs: expandStrings(d.Get("ssh_key_ids")),
267-
}
269+
installReq := &baremetal.InstallServerRequest{
270+
Zone: zonedID.Zone,
271+
ServerID: zonedID.ID,
272+
Hostname: expandStringWithDefault(d.Get("hostname"), d.Get("name").(string)),
273+
}
268274

269-
_, err := baremetalAPI.InstallServer(installReq, scw.WithContext(ctx))
275+
if d.HasChange("os") {
276+
err = baremetalInstallServer(ctx, d, baremetalAPI, installReq)
270277
if err != nil {
271278
return diag.FromErr(err)
272279
}
@@ -277,7 +284,30 @@ func resourceScalewayBaremetalServerUpdate(ctx context.Context, d *schema.Resour
277284
}
278285
}
279286

280-
return resourceScalewayBaremetalServerRead(ctx, d, meta)
287+
var diags diag.Diagnostics
288+
289+
if d.HasChanges("ssh_key_ids", "reinstall_on_ssh_key_changes") {
290+
if !d.Get("reinstall_on_ssh_key_changes").(bool) && !d.HasChange("os") {
291+
diags = append(diags, diag.Diagnostic{
292+
Severity: diag.Warning,
293+
Summary: "Changes have been made on your SSH key ID(s)",
294+
Detail: "[WARN] This change induce the reinstall of your server. " +
295+
"If this behaviour is wanted, please set 'reinstall_on_ssh_key_changes' argument to true",
296+
})
297+
} else {
298+
err = baremetalInstallServer(ctx, d, baremetalAPI, installReq)
299+
if err != nil {
300+
return diag.FromErr(err)
301+
}
302+
303+
_, err = waitForBaremetalServerInstall(ctx, baremetalAPI, zonedID.Zone, zonedID.ID, d.Timeout(schema.TimeoutUpdate))
304+
if err != nil {
305+
return diag.FromErr(err)
306+
}
307+
}
308+
}
309+
310+
return append(diags, resourceScalewayBaremetalServerRead(ctx, d, meta)...)
281311
}
282312

283313
func resourceScalewayBaremetalServerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

0 commit comments

Comments
 (0)