Skip to content

Commit

Permalink
fix crash in serviceAccountDiffSuppress (#4748) (#9032)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored and megan07 committed Apr 29, 2021
1 parent cdb3256 commit cb8dfc4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/4748.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed bug where terraform would crash if updating from no `service_account.scopes` to more.
```
6 changes: 5 additions & 1 deletion google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
70 changes: 70 additions & 0 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit cb8dfc4

Please sign in to comment.