From cf6be71afa5ee803798b64654bab1a57d165c36d Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 29 Apr 2021 15:57:49 +0000 Subject: [PATCH] fix crash in serviceAccountDiffSuppress (#4748) Signed-off-by: Modular Magician --- .changelog/4748.txt | 3 + google/resource_compute_instance.go | 6 +- google/resource_compute_instance_test.go | 70 ++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 .changelog/4748.txt diff --git a/.changelog/4748.txt b/.changelog/4748.txt new file mode 100644 index 00000000000..b1efc6a8391 --- /dev/null +++ b/.changelog/4748.txt @@ -0,0 +1,3 @@ +```release-note:bug +compute: fixed bug where terraform would crash if updating from no `service_account.scopes` to more. +``` diff --git a/google/resource_compute_instance.go b/google/resource_compute_instance.go index 9504832d39e..39a2ce19f00 100644 --- a/google/resource_compute_instance.go +++ b/google/resource_compute_instance.go @@ -2242,7 +2242,11 @@ func hash256(raw string) (string, error) { } func serviceAccountDiffSuppress(k, old, new string, d *schema.ResourceData) bool { - o, n := d.GetChange(strings.TrimSuffix(k, ".#")) + if k != "service_account.#" { + return false + } + + o, n := d.GetChange("service_account") var l []interface{} if old == "0" && new == "1" { l = n.([]interface{}) diff --git a/google/resource_compute_instance_test.go b/google/resource_compute_instance_test.go index 8395b906698..4fef3815701 100644 --- a/google/resource_compute_instance_test.go +++ b/google/resource_compute_instance_test.go @@ -851,6 +851,48 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) { }) } +func TestAccComputeInstance_serviceAccount_updated0to1to0scopes(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_serviceAccount_update01(instanceName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists( + t, "google_compute_instance.foobar", &instance), + testAccCheckComputeInstanceScopes(&instance, 0), + ), + }, + computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}), + { + Config: testAccComputeInstance_serviceAccount_update4(instanceName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists( + t, "google_compute_instance.foobar", &instance), + testAccCheckComputeInstanceScopes(&instance, 1), + ), + }, + computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}), + { + Config: testAccComputeInstance_serviceAccount_update01(instanceName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists( + t, "google_compute_instance.foobar", &instance), + testAccCheckComputeInstanceScopes(&instance, 0), + ), + }, + computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}), + }, + }) +} + func TestAccComputeInstance_scheduling(t *testing.T) { t.Parallel() @@ -4009,6 +4051,34 @@ resource "google_compute_instance" "foobar" { `, instance) } +func testAccComputeInstance_serviceAccount_update4(instance string) string { + return fmt.Sprintf(` +data "google_compute_image" "my_image" { + family = "debian-9" + project = "debian-cloud" +} +resource "google_compute_instance" "foobar" { + name = "%s" + machine_type = "e2-medium" + zone = "us-central1-a" + boot_disk { + initialize_params { + image = data.google_compute_image.my_image.self_link + } + } + network_interface { + network = "default" + } + service_account { + scopes = [ + "userinfo-email", + ] + } + allow_stopping_for_update = true +} +`, instance) +} + func testAccComputeInstance_scheduling(instance string) string { return fmt.Sprintf(` data "google_compute_image" "my_image" {