Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crash in serviceAccountDiffSuppress #9032

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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