From 313f04c961e612369fbe7401aeb5e6d0048b74ee Mon Sep 17 00:00:00 2001 From: Megan Bang Date: Thu, 29 Apr 2021 09:29:54 -0500 Subject: [PATCH] fix crash in serviceAccountDiffSuppress --- .../resources/resource_compute_instance.go | 6 +- .../tests/resource_compute_instance_test.go | 70 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/resources/resource_compute_instance.go b/mmv1/third_party/terraform/resources/resource_compute_instance.go index 9504832d39e1..39a2ce19f002 100644 --- a/mmv1/third_party/terraform/resources/resource_compute_instance.go +++ b/mmv1/third_party/terraform/resources/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/mmv1/third_party/terraform/tests/resource_compute_instance_test.go b/mmv1/third_party/terraform/tests/resource_compute_instance_test.go index 8395b906698a..4fef38157016 100644 --- a/mmv1/third_party/terraform/tests/resource_compute_instance_test.go +++ b/mmv1/third_party/terraform/tests/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" {