Skip to content

Commit

Permalink
chore: revert post_install_script in dedicated server installation to…
Browse files Browse the repository at this point in the history
… accept bash script
  • Loading branch information
Arun Varghese committed Jan 23, 2025
1 parent 331eddc commit cc1f9ab
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/resources/dedicated_server_installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ resource "leaseweb_dedicated_server_installation" "example" {
- `hostname` (String) Hostname to be used in your installation
- `partitions` (Attributes List) (see [below for nested schema](#nestedatt--partitions))
- `password` (String) Server root password. If not provided, it would be automatically generated
- `post_install_script` (String) Base64 Encoded string containing a valid bash script to be run right after the installation
- `post_install_script` (String) A valid bash script to run right after the installation.
- `power_cycle` (Boolean) If true, allows system reboots to happen automatically within the process. Otherwise, you should do them manually
- `raid` (Attributes) (see [below for nested schema](#nestedatt--raid))
- `ssh_keys` (Set of String) List of public sshKeys to be setup in your installation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ resource "leaseweb_dedicated_server_installation" "example" {
dedicated_server_id = "12345"
operating_system_id = "UBUNTU_22_04_64BIT"
}

# Example install operating system on dedicated server with post_install_script
resource "leaseweb_dedicated_server_installation" "example" {
dedicated_server_id = "12345"
operating_system_id = "UBUNTU_22_04_64BIT"
post_install_script = <<-EOS
#!/bin/sh
apt install nginx -y -qq
EOS
}
5 changes: 3 additions & 2 deletions internal/provider/dedicatedserver/installation_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dedicatedserver

import (
"context"
"encoding/base64"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
Expand Down Expand Up @@ -203,7 +204,7 @@ func (i *installationResource) Schema(
},
},
"post_install_script": schema.StringAttribute{
Description: "Base64 Encoded string containing a valid bash script to be run right after the installation",
Description: "A valid bash script to run right after the installation.",
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
Expand Down Expand Up @@ -315,7 +316,7 @@ func (i *installationResource) Create(
opts.Hostname = utils.AdaptStringPointerValueToNullableString(plan.Hostname)
opts.Partitions = partitions
opts.Password = utils.AdaptStringPointerValueToNullableString(plan.Password)
opts.PostInstallScript = utils.AdaptStringPointerValueToNullableString(plan.PostInstallScript)
opts.PostInstallScript = utils.AdaptStringValueToNullableString(base64.StdEncoding.EncodeToString([]byte(strings.TrimSpace(plan.PostInstallScript.ValueString()))))
opts.PowerCycle = utils.AdaptBoolPointerValueToNullableBool(plan.PowerCycle)
opts.Raid = raid
opts.Timezone = utils.AdaptStringPointerValueToNullableString(plan.Timezone)
Expand Down
10 changes: 9 additions & 1 deletion internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,10 @@ func TestAccDedicatedServerInstallationResource(t *testing.T) {
device = "SATA2TB"
hostname = "example.com"
password = "password"
post_install_script = "c2NyaXB0LnNo"
post_install_script = <<-EOS
#!/bin/sh
apt install nginx -y -qq
EOS
power_cycle = true
ssh_keys = ["tralala"]
timezone = "UTC"
Expand Down Expand Up @@ -1455,6 +1458,11 @@ func TestAccDedicatedServerInstallationResource(t *testing.T) {
"id",
"bcf2bedf-8450-4b22-86a8-f30aeb3a38f9",
),
resource.TestCheckResourceAttr(
"leaseweb_dedicated_server_installation.test",
"post_install_script",
"#!/bin/sh\napt install nginx -y -qq\n",
),
),
},
},
Expand Down
8 changes: 8 additions & 0 deletions internal/utils/value_adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ func AdaptStringPointerValueToNullableString(value types.String) *string {
return value.ValueStringPointer()
}

func AdaptStringValueToNullableString(value string) *string {
if value == "" {
return nil
}

return &value
}

func AdaptStringTypeArrayToStringArray[T ~string](types []T) []string {
var convertedTypes []string

Expand Down
12 changes: 12 additions & 0 deletions internal/utils/value_adapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,15 @@ func ExampleAdaptNullableSdkModelToResourceObject_second() {
fmt.Println(resourceModel)
// Output: <null>
}

func TestAdaptStringToNullableString(t *testing.T) {
t.Run("returns nil when value is empty", func(t *testing.T) {
assert.Nil(t, AdaptStringValueToNullableString(""))
})

t.Run("returns pointer when value is set", func(t *testing.T) {
value := "tralala"

assert.Equal(t, &value, AdaptStringValueToNullableString(value))
})
}

0 comments on commit cc1f9ab

Please sign in to comment.