diff --git a/.changelog/6822.txt b/.changelog/6822.txt new file mode 100644 index 0000000000..ef8b1beae5 --- /dev/null +++ b/.changelog/6822.txt @@ -0,0 +1,3 @@ +```release-note:bug +compute: fixed the error when `metadata` and `machine_type` are updated while `metadata_startup_script` was already provided on `google_compute_instance` +``` diff --git a/google/metadata.go b/google/metadata.go index dceb18e158..24c0f66152 100644 --- a/google/metadata.go +++ b/google/metadata.go @@ -142,8 +142,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 } diff --git a/google/resource_compute_instance_test.go b/google/resource_compute_instance_test.go index 449cdf6835..163cf5f7bc 100644 --- a/google/resource_compute_instance_test.go +++ b/google/resource_compute_instance_test.go @@ -2370,6 +2370,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] @@ -6205,3 +6234,36 @@ 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) +}