-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: google_compute_instance with user-managed service account and em…
…pty scopes results in no service account assignment (#10358) (#18521) [upstream:a00db19f538bab3f8f8d42b35d033a7703e19e18] Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
1e7fb46
commit 3463a5b
Showing
3 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:bug | ||
compute: fixed google_compute_instance with `service_account.email` but no `service_account.scopes` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1101,6 +1101,54 @@ func TestAccComputeInstance_serviceAccount(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccComputeInstance_noServiceAccount(t *testing.T) { | ||
t.Parallel() | ||
|
||
var instance compute.Instance | ||
var instanceName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)) | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccComputeInstance_noServiceAccount(instanceName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeInstanceExists( | ||
t, "google_compute_instance.foobar", &instance), | ||
testAccCheckComputeInstanceNoServiceAccount(&instance), | ||
), | ||
}, | ||
computeInstanceImportStep("us-central1-a", instanceName, []string{}), | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccComputeInstance_serviceAccountEmail_0scopes(t *testing.T) { | ||
t.Parallel() | ||
|
||
var instance compute.Instance | ||
var instanceName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)) | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccComputeInstance_serviceAccountEmail_0scopes(instanceName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeInstanceExists( | ||
t, "google_compute_instance.foobar", &instance), | ||
testAccCheckComputeInstanceMatchServiceAccount(&instance, "\\[email protected]"), | ||
), | ||
}, | ||
computeInstanceImportStep("us-central1-a", instanceName, []string{}), | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccComputeInstance_serviceAccount_updated(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
@@ -1117,6 +1165,7 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) { | |
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeInstanceExists( | ||
t, "google_compute_instance.foobar", &instance), | ||
testAccCheckComputeInstanceNoServiceAccount(&instance), | ||
testAccCheckComputeInstanceScopes(&instance, 0), | ||
), | ||
}, | ||
|
@@ -1126,6 +1175,7 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) { | |
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeInstanceExists( | ||
t, "google_compute_instance.foobar", &instance), | ||
testAccCheckComputeInstanceNoServiceAccount(&instance), | ||
testAccCheckComputeInstanceScopes(&instance, 0), | ||
), | ||
}, | ||
|
@@ -1135,6 +1185,7 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) { | |
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeInstanceExists( | ||
t, "google_compute_instance.foobar", &instance), | ||
testAccCheckComputeInstanceMatchServiceAccount(&instance, "\\[email protected]"), | ||
testAccCheckComputeInstanceScopes(&instance, 0), | ||
), | ||
}, | ||
|
@@ -1144,6 +1195,7 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) { | |
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeInstanceExists( | ||
t, "google_compute_instance.foobar", &instance), | ||
testAccCheckComputeInstanceMatchServiceAccount(&instance, "\\[email protected]"), | ||
testAccCheckComputeInstanceScopes(&instance, 3), | ||
), | ||
}, | ||
|
@@ -1168,6 +1220,7 @@ func TestAccComputeInstance_serviceAccount_updated0to1to0scopes(t *testing.T) { | |
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeInstanceExists( | ||
t, "google_compute_instance.foobar", &instance), | ||
testAccCheckComputeInstanceNoServiceAccount(&instance), | ||
testAccCheckComputeInstanceScopes(&instance, 0), | ||
), | ||
}, | ||
|
@@ -1177,6 +1230,7 @@ func TestAccComputeInstance_serviceAccount_updated0to1to0scopes(t *testing.T) { | |
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeInstanceExists( | ||
t, "google_compute_instance.foobar", &instance), | ||
testAccCheckComputeInstanceMatchServiceAccount(&instance, "\\[email protected]"), | ||
testAccCheckComputeInstanceScopes(&instance, 1), | ||
), | ||
}, | ||
|
@@ -1186,6 +1240,7 @@ func TestAccComputeInstance_serviceAccount_updated0to1to0scopes(t *testing.T) { | |
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckComputeInstanceExists( | ||
t, "google_compute_instance.foobar", &instance), | ||
testAccCheckComputeInstanceNoServiceAccount(&instance), | ||
testAccCheckComputeInstanceScopes(&instance, 0), | ||
), | ||
}, | ||
|
@@ -3306,6 +3361,30 @@ func testAccCheckComputeInstanceServiceAccount(instance *compute.Instance, scope | |
} | ||
} | ||
|
||
func testAccCheckComputeInstanceNoServiceAccount(instance *compute.Instance) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if count := len(instance.ServiceAccounts); count != 0 { | ||
return fmt.Errorf("Wrong number of ServiceAccounts: expected 0, got %d", count) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckComputeInstanceMatchServiceAccount(instance *compute.Instance, serviceAcctRegexp string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if count := len(instance.ServiceAccounts); count != 1 { | ||
return fmt.Errorf("Wrong number of ServiceAccounts: expected 1, got %d", count) | ||
} | ||
|
||
email := instance.ServiceAccounts[0].Email | ||
if !regexp.MustCompile(serviceAcctRegexp).MatchString(email) { | ||
return fmt.Errorf("ServiceAccount email didn't match:\"%s\", got \"%s\"", serviceAcctRegexp, email) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckComputeInstanceScopes(instance *compute.Instance, scopeCount int) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
|
||
|
@@ -5277,6 +5356,70 @@ resource "google_compute_instance" "foobar" { | |
`, instance) | ||
} | ||
|
||
func testAccComputeInstance_noServiceAccount(instance string) string { | ||
return fmt.Sprintf(` | ||
data "google_compute_image" "my_image" { | ||
family = "debian-11" | ||
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 = [] | ||
} | ||
} | ||
`, instance) | ||
} | ||
|
||
func testAccComputeInstance_serviceAccountEmail_0scopes(instance string) string { | ||
return fmt.Sprintf(` | ||
data "google_project" "project" {} | ||
data "google_compute_image" "my_image" { | ||
family = "debian-11" | ||
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 { | ||
email = data.google_compute_default_service_account.default.email | ||
scopes = [] | ||
} | ||
} | ||
data "google_compute_default_service_account" "default" { | ||
} | ||
`, instance) | ||
} | ||
|
||
func testAccComputeInstance_serviceAccount_update0(instance string) string { | ||
return fmt.Sprintf(` | ||
data "google_compute_image" "my_image" { | ||
|