Skip to content

Commit 25d044b

Browse files
modular-magicianmegan07
authored andcommitted
fix crash in serviceAccountDiffSuppress (#4748) (#3208)
Signed-off-by: Modular Magician <[email protected]>
1 parent 5f29ecc commit 25d044b

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

.changelog/4748.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
compute: fixed bug where terraform would crash if updating from no `service_account.scopes` to more.
3+
```

google-beta/resource_compute_instance.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,11 @@ func hash256(raw string) (string, error) {
22422242
}
22432243

22442244
func serviceAccountDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
2245-
o, n := d.GetChange(strings.TrimSuffix(k, ".#"))
2245+
if k != "service_account.#" {
2246+
return false
2247+
}
2248+
2249+
o, n := d.GetChange("service_account")
22462250
var l []interface{}
22472251
if old == "0" && new == "1" {
22482252
l = n.([]interface{})

google-beta/resource_compute_instance_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,48 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) {
851851
})
852852
}
853853

854+
func TestAccComputeInstance_serviceAccount_updated0to1to0scopes(t *testing.T) {
855+
t.Parallel()
856+
857+
var instance compute.Instance
858+
var instanceName = fmt.Sprintf("tf-test-%s", randString(t, 10))
859+
860+
vcrTest(t, resource.TestCase{
861+
PreCheck: func() { testAccPreCheck(t) },
862+
Providers: testAccProviders,
863+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
864+
Steps: []resource.TestStep{
865+
{
866+
Config: testAccComputeInstance_serviceAccount_update01(instanceName),
867+
Check: resource.ComposeTestCheckFunc(
868+
testAccCheckComputeInstanceExists(
869+
t, "google_compute_instance.foobar", &instance),
870+
testAccCheckComputeInstanceScopes(&instance, 0),
871+
),
872+
},
873+
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
874+
{
875+
Config: testAccComputeInstance_serviceAccount_update4(instanceName),
876+
Check: resource.ComposeTestCheckFunc(
877+
testAccCheckComputeInstanceExists(
878+
t, "google_compute_instance.foobar", &instance),
879+
testAccCheckComputeInstanceScopes(&instance, 1),
880+
),
881+
},
882+
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
883+
{
884+
Config: testAccComputeInstance_serviceAccount_update01(instanceName),
885+
Check: resource.ComposeTestCheckFunc(
886+
testAccCheckComputeInstanceExists(
887+
t, "google_compute_instance.foobar", &instance),
888+
testAccCheckComputeInstanceScopes(&instance, 0),
889+
),
890+
},
891+
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
892+
},
893+
})
894+
}
895+
854896
func TestAccComputeInstance_scheduling(t *testing.T) {
855897
t.Parallel()
856898

@@ -4009,6 +4051,34 @@ resource "google_compute_instance" "foobar" {
40094051
`, instance)
40104052
}
40114053

4054+
func testAccComputeInstance_serviceAccount_update4(instance string) string {
4055+
return fmt.Sprintf(`
4056+
data "google_compute_image" "my_image" {
4057+
family = "debian-9"
4058+
project = "debian-cloud"
4059+
}
4060+
resource "google_compute_instance" "foobar" {
4061+
name = "%s"
4062+
machine_type = "e2-medium"
4063+
zone = "us-central1-a"
4064+
boot_disk {
4065+
initialize_params {
4066+
image = data.google_compute_image.my_image.self_link
4067+
}
4068+
}
4069+
network_interface {
4070+
network = "default"
4071+
}
4072+
service_account {
4073+
scopes = [
4074+
"userinfo-email",
4075+
]
4076+
}
4077+
allow_stopping_for_update = true
4078+
}
4079+
`, instance)
4080+
}
4081+
40124082
func testAccComputeInstance_scheduling(instance string) string {
40134083
return fmt.Sprintf(`
40144084
data "google_compute_image" "my_image" {

0 commit comments

Comments
 (0)