Skip to content

Commit

Permalink
fix metadata_startup_script (GoogleCloudPlatform#6822)
Browse files Browse the repository at this point in the history
Co-authored-by: Edward Sun <[email protected]>
  • Loading branch information
2 people authored and kimihrr committed Dec 6, 2022
1 parent 9351db2 commit 3e3df06
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,35 @@ func TestComputeInstance_networkIPCustomizedDiff(t *testing.T) {
}
}

func TestAccComputeInstance_metadataStartupScript_update(t *testing.T) {
t.Parallel()

var instance compute.Instance
var instanceName = fmt.Sprintf("tf-test-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_metadataStartupScript(instanceName, "e2-medium", "abc"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance),
),
},
{
Config: testAccComputeInstance_metadataStartupScript(instanceName, "e2-standard-4", "xyz"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance),
),
},
},
})
}

func testAccCheckComputeInstanceUpdateMachineType(t *testing.T, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -6319,3 +6348,35 @@ resource "google_compute_instance" "foobar" {
`, instance)
}

func testAccComputeInstance_metadataStartupScript(instance, machineType, metadata string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "%s"
zone = "us-central1-a"
can_ip_forward = false
tags = ["foo", "bar"]

boot_disk {
initialize_params {
image = data.google_compute_image.my_image.self_link
}
}

network_interface {
network = "default"
}

metadata = {
foo = "%s"
}
metadata_startup_script = "echo hi > /test.txt"
allow_stopping_for_update = true
}
`, instance, machineType, metadata)
}
7 changes: 5 additions & 2 deletions mmv1/third_party/terraform/utils/metadata.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ func resourceInstanceMetadata(d TerraformResourceData) (*compute.Metadata, error
m := &compute.Metadata{}
mdMap := d.Get("metadata").(map[string]interface{})
if v, ok := d.GetOk("metadata_startup_script"); ok && v.(string) != "" {
if _, ok := mdMap["startup-script"]; ok {
return nil, errors.New("Cannot provide both metadata_startup_script and metadata.startup-script.")
if w, ok := mdMap["startup-script"]; ok {
// metadata.startup-script could be from metadata_startup_script in the first place
if v != w {
return nil, errors.New("Cannot provide both metadata_startup_script and metadata.startup-script.")
}
}
mdMap["startup-script"] = v
}
Expand Down

0 comments on commit 3e3df06

Please sign in to comment.