diff --git a/docs/resources/dedicated_server_installation.md b/docs/resources/dedicated_server_installation.md index 136e1cc3..74fa54f8 100644 --- a/docs/resources/dedicated_server_installation.md +++ b/docs/resources/dedicated_server_installation.md @@ -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 diff --git a/examples/resources/leaseweb_dedicated_server_installation/resource.tf b/examples/resources/leaseweb_dedicated_server_installation/resource.tf index 42dbf3ee..867d537d 100644 --- a/examples/resources/leaseweb_dedicated_server_installation/resource.tf +++ b/examples/resources/leaseweb_dedicated_server_installation/resource.tf @@ -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 +} \ No newline at end of file diff --git a/internal/provider/dedicatedserver/installation_resource.go b/internal/provider/dedicatedserver/installation_resource.go index 0edd97e7..bc105cd7 100644 --- a/internal/provider/dedicatedserver/installation_resource.go +++ b/internal/provider/dedicatedserver/installation_resource.go @@ -2,6 +2,7 @@ package dedicatedserver import ( "context" + "encoding/base64" "strings" "github.com/hashicorp/terraform-plugin-framework-validators/int32validator" @@ -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(), @@ -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) diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 7f687c31..88fc2bf2 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -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" @@ -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", + ), ), }, }, diff --git a/internal/utils/value_adapters.go b/internal/utils/value_adapters.go index 6dfb3a7b..43c75a4c 100644 --- a/internal/utils/value_adapters.go +++ b/internal/utils/value_adapters.go @@ -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 diff --git a/internal/utils/value_adapters_test.go b/internal/utils/value_adapters_test.go index 3868bec5..1318de9a 100644 --- a/internal/utils/value_adapters_test.go +++ b/internal/utils/value_adapters_test.go @@ -425,3 +425,15 @@ func ExampleAdaptNullableSdkModelToResourceObject_second() { fmt.Println(resourceModel) // Output: } + +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)) + }) +}