Skip to content

Commit 7bd6633

Browse files
committed
feat(instance): add support for admin_password_encryption_ssh_key_id
1 parent ba0d3a3 commit 7bd6633

File tree

4 files changed

+4261
-0
lines changed

4 files changed

+4261
-0
lines changed

docs/resources/instance_server.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ attached to the server. Updates to this field will trigger a stop/start of the s
260260

261261
- `protected` - (Optional) Set to true to activate server protection option.
262262

263+
- `admin_password_encryption_ssh_key_id` - (Optional) The ID of the SSH RSA key that will be used to encrypt the initial admin password for OS requiring it.
264+
Mandatory for Windows OS. The public_key value of this key is used to encrypt the admin password.
265+
When set to an empty string, it resets this value and admin_password_encrypted_value to an empty string so a new password may be generated.
266+
263267
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the server should be created.
264268

265269
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the server is associated with.

internal/services/instance/server.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ func ResourceServer() *schema.Resource {
372372
},
373373
},
374374
},
375+
"admin_password_encryption_ssh_key_id": {
376+
Type: schema.TypeString,
377+
Optional: true,
378+
Description: "The ID of the IAM SSH key used to encrypt the initial admin password on a Windows server",
379+
},
375380
"zone": zonal.Schema(),
376381
"organization_id": account.OrganizationIDSchema(),
377382
"project_id": account.ProjectIDSchema(),
@@ -437,6 +442,10 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
437442
req.PlacementGroup = types.ExpandStringPtr(zonal.ExpandID(placementGroupID).ID)
438443
}
439444

445+
if adminPasswordEncryptionSSHKeyID, ok := d.GetOk("admin_password_encryption_key_ssh_id"); ok {
446+
req.AdminPasswordEncryptionSSHKeyID = types.ExpandStringPtr(adminPasswordEncryptionSSHKeyID)
447+
}
448+
440449
serverType := getServerType(ctx, api.API, req.Zone, req.CommercialType)
441450
if serverType == nil {
442451
return diag.Diagnostics{{
@@ -713,6 +722,10 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m a
713722
_ = d.Set("ipv6_prefix_length", nil)
714723
}
715724

725+
if server.AdminPasswordEncryptionSSHKeyID != nil {
726+
_ = d.Set("admin_password_encryption_ssh_key_id", server.AdminPasswordEncryptionSSHKeyID)
727+
}
728+
716729
var additionalVolumesIDs []string
717730

718731
for i, serverVolume := range sortVolumeServer(server.Volumes) {
@@ -965,6 +978,10 @@ func ResourceInstanceServerUpdate(ctx context.Context, d *schema.ResourceData, m
965978
}
966979
}
967980

981+
if d.HasChange("admin_password_encryption_ssh_key_id") {
982+
updateRequest.AdminPasswordEncryptionSSHKeyID = types.ExpandUpdatedStringPtr(d.Get("admin_password_encryption_ssh_key_id").(string))
983+
}
984+
968985
////
969986
// Update reserved IP
970987
////

internal/services/instance/server_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package instance_test
33
import (
44
"errors"
55
"fmt"
6+
iamchecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/iam/testfuncs"
67
"regexp"
78
"strings"
89
"testing"
@@ -2094,3 +2095,75 @@ func TestAccServer_PrivateNetworkMissingPNIC(t *testing.T) {
20942095
},
20952096
})
20962097
}
2098+
2099+
func TestAccServer_AdminPasswordEncryptionSSHKeyID(t *testing.T) {
2100+
tt := acctest.NewTestTools(t)
2101+
defer tt.Cleanup()
2102+
2103+
sshKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX [email protected]"
2104+
2105+
resource.ParallelTest(t, resource.TestCase{
2106+
PreCheck: func() { acctest.PreCheck(t) },
2107+
ProviderFactories: tt.ProviderFactories,
2108+
CheckDestroy: resource.ComposeTestCheckFunc(
2109+
instancechecks.IsServerDestroyed(tt),
2110+
iamchecks.CheckSSHKeyDestroy(tt),
2111+
),
2112+
Steps: []resource.TestStep{
2113+
{
2114+
Config: fmt.Sprintf(`
2115+
resource "scaleway_iam_ssh_key" "main" {
2116+
name = "test-acc-admin-pwd-encryption"
2117+
public_key = %q
2118+
}
2119+
2120+
resource "scaleway_instance_server" "main" {
2121+
type = "POP2-2C-8G-WIN"
2122+
image = "windows_server_2022"
2123+
admin_password_encryption_ssh_key_id = scaleway_iam_ssh_key.main.id
2124+
}
2125+
`, sshKey),
2126+
Check: resource.ComposeTestCheckFunc(
2127+
iamchecks.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
2128+
resource.TestCheckResourceAttr("scaleway_instance_server.main", "type", "POP2-2C-8G-WIN"),
2129+
resource.TestCheckResourceAttr("scaleway_instance_server.main", "image", "windows_server_2022"),
2130+
resource.TestCheckResourceAttrPair("scaleway_instance_server.main", "admin_password_encryption_ssh_key_id", "scaleway_iam_ssh_key.main", "id"),
2131+
),
2132+
},
2133+
{
2134+
Config: fmt.Sprintf(`
2135+
resource "scaleway_iam_ssh_key" "main" {
2136+
name = "test-acc-admin-pwd-encryption"
2137+
public_key = %q
2138+
}
2139+
2140+
resource "scaleway_instance_server" "main" {
2141+
type = "POP2-2C-8G-WIN"
2142+
image = "windows_server_2022"
2143+
admin_password_encryption_ssh_key_id = ""
2144+
}
2145+
`, sshKey),
2146+
Check: resource.ComposeTestCheckFunc(
2147+
resource.TestCheckResourceAttr("scaleway_instance_server.main", "admin_password_encryption_ssh_key_id", ""),
2148+
),
2149+
},
2150+
{
2151+
Config: fmt.Sprintf(`
2152+
resource "scaleway_iam_ssh_key" "main" {
2153+
name = "test-acc-admin-pwd-encryption"
2154+
public_key = %q
2155+
}
2156+
2157+
resource "scaleway_instance_server" "main" {
2158+
type = "POP2-2C-8G-WIN"
2159+
image = "windows_server_2022"
2160+
admin_password_encryption_ssh_key_id = scaleway_iam_ssh_key.main.id
2161+
}
2162+
`, sshKey),
2163+
Check: resource.ComposeTestCheckFunc(
2164+
resource.TestCheckResourceAttrPair("scaleway_instance_server.main", "admin_password_encryption_ssh_key_id", "scaleway_iam_ssh_key.main", "id"),
2165+
),
2166+
},
2167+
},
2168+
})
2169+
}

internal/services/instance/testdata/server-admin-password-encryption-ssh-key-id.cassette.yaml

Lines changed: 4167 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)