Skip to content

Commit 02763c6

Browse files
authored
feat(baremetal): add user attributes to baremetal server (#1582)
1 parent dabf785 commit 02763c6

7 files changed

+453
-16
lines changed

docs/resources/baremetal_server.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ 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 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.
41+
- `user` - (Optional) User used for the installation.
42+
- `password` - (Optional) Password used for the installation. May be required depending on used os.
43+
- `service_user` - (Optional) User used for the service to install.
44+
- `service_password` - (Optional) Password used for the service to install. May be required depending on used os.
45+
- `reinstall_on_config_changes` - (Optional) If True, this boolean allows to reinstall the server on install config changes.
46+
~> **Important:** Updates to `ssh_key_ids`, `user`, `password`, `service_user` or `service_password` will not take effect on the server, it requires to reinstall it. To do so please set 'reinstall_on_config_changes' argument to true.
4347
- `name` - (Optional) The name of the server.
4448
- `hostname` - (Optional) The hostname of the server.
4549
- `description` - (Optional) A description for the server.

scaleway/resource_baremetal_server.go

Lines changed: 118 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package scaleway
22

33
import (
44
"context"
5+
"fmt"
56

7+
"github.com/hashicorp/go-cty/cty"
68
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -73,11 +75,35 @@ func resourceScalewayBaremetalServer() *schema.Resource {
7375
**NOTE** : If you are attempting to update your SSH key IDs, it will induce the reinstall of your server.
7476
If this behaviour is wanted, please set 'reinstall_on_ssh_key_changes' argument to true.`,
7577
},
76-
"reinstall_on_ssh_key_changes": {
78+
"user": {
79+
Type: schema.TypeString,
80+
Optional: true,
81+
Computed: true,
82+
Description: "User used for the installation.",
83+
},
84+
"password": {
85+
Type: schema.TypeString,
86+
Optional: true,
87+
Sensitive: true,
88+
Description: "Password used for the installation.",
89+
},
90+
"service_user": {
91+
Type: schema.TypeString,
92+
Optional: true,
93+
Computed: true,
94+
Description: "User used for the service to install.",
95+
},
96+
"service_password": {
97+
Type: schema.TypeString,
98+
Optional: true,
99+
Sensitive: true,
100+
Description: "Password used for the service to install.",
101+
},
102+
"reinstall_on_config_changes": {
77103
Type: schema.TypeBool,
78104
Optional: true,
79105
Default: false,
80-
Description: "If True, this boolean allows to reinstall the server on SSH key IDs changes",
106+
Description: "If True, this boolean allows to reinstall the server on SSH key IDs, user or password changes",
81107
},
82108
"description": {
83109
Type: schema.TypeString,
@@ -149,6 +175,9 @@ func resourceScalewayBaremetalServerCreate(ctx context.Context, d *schema.Resour
149175
}
150176
offerID = newZonedID(zone, o.ID)
151177
}
178+
if diags := validateInstallConfig(ctx, d, meta); len(diags) > 0 {
179+
return diags
180+
}
152181

153182
server, err := baremetalAPI.CreateServer(&baremetal.CreateServerRequest{
154183
Zone: zone,
@@ -170,11 +199,15 @@ func resourceScalewayBaremetalServerCreate(ctx context.Context, d *schema.Resour
170199
}
171200

172201
_, err = baremetalAPI.InstallServer(&baremetal.InstallServerRequest{
173-
Zone: server.Zone,
174-
ServerID: server.ID,
175-
OsID: expandZonedID(d.Get("os")).ID,
176-
Hostname: expandStringWithDefault(d.Get("hostname"), server.Name),
177-
SSHKeyIDs: expandStrings(d.Get("ssh_key_ids")),
202+
Zone: server.Zone,
203+
ServerID: server.ID,
204+
OsID: expandZonedID(d.Get("os")).ID,
205+
Hostname: expandStringWithDefault(d.Get("hostname"), server.Name),
206+
SSHKeyIDs: expandStrings(d.Get("ssh_key_ids")),
207+
User: expandStringPtr(d.Get("user")),
208+
Password: expandStringPtr(d.Get("password")),
209+
ServiceUser: expandStringPtr(d.Get("service_user")),
210+
ServicePassword: expandStringPtr(d.Get("service_password")),
178211
}, scw.WithContext(ctx))
179212
if err != nil {
180213
return diag.FromErr(err)
@@ -225,6 +258,7 @@ func resourceScalewayBaremetalServerRead(ctx context.Context, d *schema.Resource
225258
if server.Install != nil {
226259
_ = d.Set("os_id", newZonedID(server.Zone, server.Install.OsID).String())
227260
_ = d.Set("ssh_key_ids", server.Install.SSHKeyIDs)
261+
_ = d.Set("user", server.Install.User)
228262
}
229263
_ = d.Set("description", server.Description)
230264

@@ -267,12 +301,20 @@ func resourceScalewayBaremetalServerUpdate(ctx context.Context, d *schema.Resour
267301
}
268302

269303
installReq := &baremetal.InstallServerRequest{
270-
Zone: zonedID.Zone,
271-
ServerID: zonedID.ID,
272-
Hostname: expandStringWithDefault(d.Get("hostname"), d.Get("name").(string)),
304+
Zone: zonedID.Zone,
305+
ServerID: zonedID.ID,
306+
Hostname: expandStringWithDefault(d.Get("hostname"), d.Get("name").(string)),
307+
SSHKeyIDs: expandStrings(d.Get("ssh_key_ids")),
308+
User: expandStringPtr(d.Get("user")),
309+
Password: expandStringPtr(d.Get("password")),
310+
ServiceUser: expandStringPtr(d.Get("service_user")),
311+
ServicePassword: expandStringPtr(d.Get("service_password")),
273312
}
274313

275314
if d.HasChange("os") {
315+
if diags := validateInstallConfig(ctx, d, meta); len(diags) > 0 {
316+
return diags
317+
}
276318
err = baremetalInstallServer(ctx, d, baremetalAPI, installReq)
277319
if err != nil {
278320
return diag.FromErr(err)
@@ -286,15 +328,18 @@ func resourceScalewayBaremetalServerUpdate(ctx context.Context, d *schema.Resour
286328

287329
var diags diag.Diagnostics
288330

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") {
331+
if d.HasChanges("ssh_key_ids", "user", "password", "reinstall_on_config_changes") {
332+
if !d.Get("reinstall_on_config_changes").(bool) && !d.HasChange("os") {
291333
diags = append(diags, diag.Diagnostic{
292334
Severity: diag.Warning,
293-
Summary: "Changes have been made on your SSH key ID(s)",
335+
Summary: "Changes have been made on your config",
294336
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",
337+
"If this behaviour is wanted, please set 'reinstall_on_config_changes' argument to true",
296338
})
297339
} else {
340+
if diags := validateInstallConfig(ctx, d, meta); len(diags) > 0 {
341+
return diags
342+
}
298343
err = baremetalInstallServer(ctx, d, baremetalAPI, installReq)
299344
if err != nil {
300345
return diag.FromErr(err)
@@ -334,3 +379,62 @@ func resourceScalewayBaremetalServerDelete(ctx context.Context, d *schema.Resour
334379

335380
return nil
336381
}
382+
383+
func baremetalInstallAttributeMissing(field *baremetal.OSOSField, d *schema.ResourceData, attribute string) bool {
384+
if field != nil && field.Required && field.DefaultValue == nil {
385+
if _, attributeExists := d.GetOk(attribute); !attributeExists {
386+
return true
387+
}
388+
}
389+
return false
390+
}
391+
392+
// validateInstallConfig validates that schema contains attribute required for OS install
393+
func validateInstallConfig(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
394+
baremetalAPI, zone, err := baremetalAPIWithZone(d, meta)
395+
if err != nil {
396+
return diag.FromErr(err)
397+
}
398+
399+
os, err := baremetalAPI.GetOS(&baremetal.GetOSRequest{
400+
Zone: zone,
401+
OsID: expandID(d.Get("os")),
402+
}, scw.WithContext(ctx))
403+
if err != nil {
404+
return diag.FromErr(err)
405+
}
406+
407+
diags := diag.Diagnostics(nil)
408+
installAttributes := []struct {
409+
Attribute string
410+
Field *baremetal.OSOSField
411+
}{
412+
{
413+
"user",
414+
os.User,
415+
},
416+
{
417+
"password",
418+
os.Password,
419+
},
420+
{
421+
"service_user",
422+
os.ServiceUser,
423+
},
424+
{
425+
"service_password",
426+
os.ServicePassword,
427+
},
428+
}
429+
for _, installAttr := range installAttributes {
430+
if baremetalInstallAttributeMissing(installAttr.Field, d, installAttr.Attribute) {
431+
diags = append(diags, diag.Diagnostic{
432+
Severity: diag.Error,
433+
Summary: fmt.Sprintf("%s attribute is required", installAttr.Attribute),
434+
Detail: fmt.Sprintf("%s is required for this os", installAttr.Attribute),
435+
AttributePath: cty.GetAttrPath(installAttr.Attribute),
436+
})
437+
}
438+
}
439+
return diags
440+
}

scaleway/resource_baremetal_server_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scaleway
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -122,6 +123,31 @@ func TestAccScalewayBaremetalServer_Basic(t *testing.T) {
122123
})
123124
}
124125

126+
func TestAccScalewayBaremetalServer_RequiredInstallConfig(t *testing.T) {
127+
tt := NewTestTools(t)
128+
defer tt.Cleanup()
129+
130+
resource.ParallelTest(t, resource.TestCase{
131+
PreCheck: func() { testAccPreCheck(t) },
132+
ProviderFactories: tt.ProviderFactories,
133+
CheckDestroy: testAccCheckScalewayBaremetalServerDestroy(tt),
134+
Steps: []resource.TestStep{
135+
{
136+
Config: `
137+
resource "scaleway_baremetal_server" "base" {
138+
name = "TestAccScalewayBaremetalServer_RequiredInstallConfig"
139+
zone = "fr-par-2"
140+
offer = "EM-B112X-SSD"
141+
os = "7e865c16-1a63-4dc7-8181-dabc020fc21b" // Proxmox
142+
143+
ssh_key_ids = []
144+
}`,
145+
ExpectError: regexp.MustCompile("attribute is required"),
146+
},
147+
},
148+
})
149+
}
150+
125151
func testAccCheckScalewayBaremetalServerExists(tt *TestTools, n string) resource.TestCheckFunc {
126152
return func(s *terraform.State) error {
127153
rs, ok := s.RootModule().Resources[n]

0 commit comments

Comments
 (0)