Skip to content

Commit 20f6368

Browse files
committed
cloudfunctions: bootstraped IAM roles for service agent
Bootstrap IAM role for `service-[project_number]@gcf-admin-robot.iam.gserviceaccount.com` to avoid having bindings get deleted when tests run in parallel. Also adjust role(s) assigned to service agent. Fixes hashicorp/terraform-provider-google#20570
1 parent 5b7326c commit 20f6368

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

mmv1/third_party/terraform/services/cloudfunctions/resource_cloudfunctions_function_test.go.tmpl

+23-26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ const testFirestoreTriggerPath = "./test-fixtures/firestore_trigger.js"
2525
const testSecretEnvVarFunctionPath = "./test-fixtures/secret_environment_variables.js"
2626
const testSecretVolumesMountFunctionPath = "./test-fixtures/secret_volumes_mount.js"
2727

28+
func bootstrapGcfAdminAgents(t *testing.T) {
29+
acctest.BootstrapIamMembers(t, []acctest.IamMember{
30+
{
31+
Member: "serviceAccount:service-{project_number}@gcf-admin-robot.iam.gserviceaccount.com",
32+
Role: "roles/vpcaccess.admin",
33+
},
34+
{
35+
Member: "serviceAccount:service-{project_number}@gcf-admin-robot.iam.gserviceaccount.com",
36+
Role: "roles/storage.objectViewer",
37+
},
38+
})
39+
}
40+
2841
func TestAccCloudFunctionsFunction_basic(t *testing.T) {
2942
t.Parallel()
3043

@@ -419,16 +432,16 @@ func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) {
419432
networkName := fmt.Sprintf("tf-test-net-%d", acctest.RandInt(t))
420433
vpcConnectorName := fmt.Sprintf("tf-test-conn-%s", acctest.RandString(t, 5))
421434
zipFilePath := acctest.CreateZIPArchiveForCloudFunctionSource(t, testHTTPTriggerPath)
422-
projectNumber := os.Getenv("GOOGLE_PROJECT_NUMBER")
423435
defer os.Remove(zipFilePath) // clean up
424436

437+
bootstrapGcfAdminAgents(t)
425438
acctest.VcrTest(t, resource.TestCase{
426439
PreCheck: func() { acctest.AccTestPreCheck(t) },
427440
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
428441
CheckDestroy: testAccCheckCloudFunctionsFunctionDestroyProducer(t),
429442
Steps: []resource.TestStep{
430443
{
431-
Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.10.0.0/28", vpcConnectorName),
444+
Config: testAccCloudFunctionsFunction_vpcConnector(networkName, functionName, bucketName, zipFilePath, "10.10.0.0/28", vpcConnectorName),
432445
},
433446
{
434447
ResourceName: funcResourceName,
@@ -437,7 +450,7 @@ func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) {
437450
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels", "terraform_labels"},
438451
},
439452
{
440-
Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update"),
453+
Config: testAccCloudFunctionsFunction_vpcConnector(networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update"),
441454
},
442455
{
443456
ResourceName: funcResourceName,
@@ -458,16 +471,16 @@ func TestAccCloudFunctionsFunction_vpcConnectorEgressSettings(t *testing.T) {
458471
networkName := fmt.Sprintf("tf-test-net-%d", acctest.RandInt(t))
459472
vpcConnectorName := fmt.Sprintf("tf-test-conn-%s", acctest.RandString(t, 5))
460473
zipFilePath := acctest.CreateZIPArchiveForCloudFunctionSource(t, testHTTPTriggerPath)
461-
projectNumber := os.Getenv("GOOGLE_PROJECT_NUMBER")
462474
defer os.Remove(zipFilePath) // clean up
463475

476+
bootstrapGcfAdminAgents(t)
464477
acctest.VcrTest(t, resource.TestCase{
465478
PreCheck: func() { acctest.AccTestPreCheck(t) },
466479
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
467480
CheckDestroy: testAccCheckCloudFunctionsFunctionDestroyProducer(t),
468481
Steps: []resource.TestStep{
469482
{
470-
Config: testAccCloudFunctionsFunction_vpcConnectorEgressSettings(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.10.0.0/28", vpcConnectorName, "PRIVATE_RANGES_ONLY"),
483+
Config: testAccCloudFunctionsFunction_vpcConnectorEgressSettings(networkName, functionName, bucketName, zipFilePath, "10.10.0.0/28", vpcConnectorName, "PRIVATE_RANGES_ONLY"),
471484
},
472485
{
473486
ResourceName: funcResourceName,
@@ -476,7 +489,7 @@ func TestAccCloudFunctionsFunction_vpcConnectorEgressSettings(t *testing.T) {
476489
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels", "terraform_labels"},
477490
},
478491
{
479-
Config: testAccCloudFunctionsFunction_vpcConnectorEgressSettings(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update", "ALL_TRAFFIC"),
492+
Config: testAccCloudFunctionsFunction_vpcConnectorEgressSettings(networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update", "ALL_TRAFFIC"),
480493
},
481494
{
482495
ResourceName: funcResourceName,
@@ -1053,16 +1066,10 @@ resource "google_cloudfunctions_function" "function" {
10531066
`, bucketName, zipFilePath, functionName)
10541067
}
10551068

1056-
func testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, vpcIp, vpcConnectorName string) string {
1069+
func testAccCloudFunctionsFunction_vpcConnector(networkName, functionName, bucketName, zipFilePath, vpcIp, vpcConnectorName string) string {
10571070
return fmt.Sprintf(`
10581071
data "google_project" "project" {}
10591072

1060-
resource "google_project_iam_member" "gcfadmin" {
1061-
project = data.google_project.project.project_id
1062-
role = "roles/editor"
1063-
member = "serviceAccount:service-%[email protected]"
1064-
}
1065-
10661073
resource "google_compute_network" "vpc" {
10671074
name = "%s"
10681075
auto_create_subnetworks = false
@@ -1110,23 +1117,15 @@ resource "google_cloudfunctions_function" "function" {
11101117
min_instances = 3
11111118
vpc_connector = google_vpc_access_connector.%s.self_link
11121119
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
1113-
1114-
depends_on = [google_project_iam_member.gcfadmin]
11151120
}
1116-
`, projectNumber, networkName, vpcConnectorName, vpcConnectorName, vpcIp, bucketName, zipFilePath, functionName, vpcConnectorName)
1121+
`, networkName, vpcConnectorName, vpcConnectorName, vpcIp, bucketName, zipFilePath, functionName, vpcConnectorName)
11171122
}
11181123

11191124

1120-
func testAccCloudFunctionsFunction_vpcConnectorEgressSettings(projectNumber, networkName, functionName, bucketName, zipFilePath, vpcIp, vpcConnectorName, vpcConnectorEgressSettings string) string {
1125+
func testAccCloudFunctionsFunction_vpcConnectorEgressSettings(networkName, functionName, bucketName, zipFilePath, vpcIp, vpcConnectorName, vpcConnectorEgressSettings string) string {
11211126
return fmt.Sprintf(`
11221127
data "google_project" "project" {}
11231128

1124-
resource "google_project_iam_member" "gcfadmin" {
1125-
project = data.google_project.project.project_id
1126-
role = "roles/editor"
1127-
member = "serviceAccount:service-%[email protected]"
1128-
}
1129-
11301129
resource "google_compute_network" "vpc" {
11311130
name = "%s"
11321131
auto_create_subnetworks = false
@@ -1174,10 +1173,8 @@ resource "google_cloudfunctions_function" "function" {
11741173
min_instances = 3
11751174
vpc_connector = google_vpc_access_connector.%s.self_link
11761175
vpc_connector_egress_settings = "%s"
1177-
1178-
depends_on = [google_project_iam_member.gcfadmin]
11791176
}
1180-
`, projectNumber, networkName, vpcConnectorName, vpcConnectorName, vpcIp, bucketName, zipFilePath, functionName, vpcConnectorName, vpcConnectorEgressSettings)
1177+
`, networkName, vpcConnectorName, vpcConnectorName, vpcIp, bucketName, zipFilePath, functionName, vpcConnectorName, vpcConnectorEgressSettings)
11811178
}
11821179

11831180
{{ if ne $.TargetVersionName `ga` -}}

0 commit comments

Comments
 (0)