Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove user-data conditional size #121

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions cloudstack/resource_cloudstack_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
}

if userData, ok := d.GetOk("user_data"); ok {
ud, err := getUserData(userData.(string), cs.HTTPGETOnly)
ud, err := getUserData(userData.(string))
if err != nil {
return err
}
Expand Down Expand Up @@ -656,7 +656,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
if d.HasChange("user_data") {
log.Printf("[DEBUG] user_data changed for %s, starting update", name)

ud, err := getUserData(d.Get("user_data").(string), cs.HTTPGETOnly)
ud, err := getUserData(d.Get("user_data").(string))
if err != nil {
return err
}
Expand Down Expand Up @@ -733,25 +733,11 @@ func resourceCloudStackInstanceImport(d *schema.ResourceData, meta interface{})
}

// getUserData returns the user data as a base64 encoded string
func getUserData(userData string, httpGetOnly bool) (string, error) {
func getUserData(userData string) (string, error) {
ud := userData
if _, err := base64.StdEncoding.DecodeString(ud); err != nil {
ud = base64.StdEncoding.EncodeToString([]byte(userData))
}

// deployVirtualMachine uses POST by default, so max userdata is 32K
maxUD := 32768

if httpGetOnly {
// deployVirtualMachine using GET instead, so max userdata is 2K
maxUD = 2048
}

if len(ud) > maxUD {
return "", fmt.Errorf(
"The supplied user_data contains %d bytes after encoding, "+
"this exceeds the limit of %d bytes", len(ud), maxUD)
}

return ud, nil
}
51 changes: 51 additions & 0 deletions cloudstack/resource_cloudstack_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package cloudstack

import (
"fmt"
"regexp"
"testing"

"github.com/apache/cloudstack-go/v2/cloudstack"
Expand Down Expand Up @@ -212,6 +213,26 @@ func TestAccCloudStackInstance_importProject(t *testing.T) {
})
}

func TestAccCloudStackInstance_userData(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackInstanceDestroy,
ExternalProviders: map[string]resource.ExternalProvider{
"random": {
VersionConstraint: ">= 3.6.0",
Source: "hashicorp/random",
},
},
Steps: []resource.TestStep{
{
Config: testAccCloudStackInstance_userData,
ExpectError: regexp.MustCompile("User data has exceeded configurable max length"),
},
},
})
}

func testAccCheckCloudStackInstanceExists(
n string, instance *cloudstack.VirtualMachine) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -436,3 +457,33 @@ resource "cloudstack_instance" "foobar" {
zone = cloudstack_network.foo.zone
expunge = true
}`

const testAccCloudStackInstance_userData = `
resource "random_bytes" "string" {
length = 32768
}

resource "cloudstack_network" "foo" {
name = "terraform-network"
display_text = "terraform-network"
cidr = "10.1.1.0/24"
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
zone = "Sandbox-simulator"
}

resource "cloudstack_instance" "foobar" {
name = "terraform-test"
display_name = "terraform-test"
service_offering= "Small Instance"
network_id = cloudstack_network.foo.id
template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
zone = cloudstack_network.foo.zone
expunge = true
user_data = <<-EOFTF
#!/bin/bash

echo <<EOF
${random_bytes.string.base64}
EOF
EOFTF
}`
Loading