Skip to content

Commit 22864aa

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 22864aa

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

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

+19-26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ 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+
}
36+
2837
func TestAccCloudFunctionsFunction_basic(t *testing.T) {
2938
t.Parallel()
3039

@@ -419,16 +428,16 @@ func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) {
419428
networkName := fmt.Sprintf("tf-test-net-%d", acctest.RandInt(t))
420429
vpcConnectorName := fmt.Sprintf("tf-test-conn-%s", acctest.RandString(t, 5))
421430
zipFilePath := acctest.CreateZIPArchiveForCloudFunctionSource(t, testHTTPTriggerPath)
422-
projectNumber := os.Getenv("GOOGLE_PROJECT_NUMBER")
423431
defer os.Remove(zipFilePath) // clean up
424432

433+
bootstrapGcfAdminAgents(t)
425434
acctest.VcrTest(t, resource.TestCase{
426435
PreCheck: func() { acctest.AccTestPreCheck(t) },
427436
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
428437
CheckDestroy: testAccCheckCloudFunctionsFunctionDestroyProducer(t),
429438
Steps: []resource.TestStep{
430439
{
431-
Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.10.0.0/28", vpcConnectorName),
440+
Config: testAccCloudFunctionsFunction_vpcConnector(networkName, functionName, bucketName, zipFilePath, "10.10.0.0/28", vpcConnectorName),
432441
},
433442
{
434443
ResourceName: funcResourceName,
@@ -437,7 +446,7 @@ func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) {
437446
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels", "terraform_labels"},
438447
},
439448
{
440-
Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update"),
449+
Config: testAccCloudFunctionsFunction_vpcConnector(networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update"),
441450
},
442451
{
443452
ResourceName: funcResourceName,
@@ -458,16 +467,16 @@ func TestAccCloudFunctionsFunction_vpcConnectorEgressSettings(t *testing.T) {
458467
networkName := fmt.Sprintf("tf-test-net-%d", acctest.RandInt(t))
459468
vpcConnectorName := fmt.Sprintf("tf-test-conn-%s", acctest.RandString(t, 5))
460469
zipFilePath := acctest.CreateZIPArchiveForCloudFunctionSource(t, testHTTPTriggerPath)
461-
projectNumber := os.Getenv("GOOGLE_PROJECT_NUMBER")
462470
defer os.Remove(zipFilePath) // clean up
463471

472+
bootstrapGcfAdminAgents(t)
464473
acctest.VcrTest(t, resource.TestCase{
465474
PreCheck: func() { acctest.AccTestPreCheck(t) },
466475
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
467476
CheckDestroy: testAccCheckCloudFunctionsFunctionDestroyProducer(t),
468477
Steps: []resource.TestStep{
469478
{
470-
Config: testAccCloudFunctionsFunction_vpcConnectorEgressSettings(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.10.0.0/28", vpcConnectorName, "PRIVATE_RANGES_ONLY"),
479+
Config: testAccCloudFunctionsFunction_vpcConnectorEgressSettings(networkName, functionName, bucketName, zipFilePath, "10.10.0.0/28", vpcConnectorName, "PRIVATE_RANGES_ONLY"),
471480
},
472481
{
473482
ResourceName: funcResourceName,
@@ -476,7 +485,7 @@ func TestAccCloudFunctionsFunction_vpcConnectorEgressSettings(t *testing.T) {
476485
ImportStateVerifyIgnore: []string{"build_environment_variables", "labels", "terraform_labels"},
477486
},
478487
{
479-
Config: testAccCloudFunctionsFunction_vpcConnectorEgressSettings(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update", "ALL_TRAFFIC"),
488+
Config: testAccCloudFunctionsFunction_vpcConnectorEgressSettings(networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update", "ALL_TRAFFIC"),
480489
},
481490
{
482491
ResourceName: funcResourceName,
@@ -1053,16 +1062,10 @@ resource "google_cloudfunctions_function" "function" {
10531062
`, bucketName, zipFilePath, functionName)
10541063
}
10551064

1056-
func testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, vpcIp, vpcConnectorName string) string {
1065+
func testAccCloudFunctionsFunction_vpcConnector(networkName, functionName, bucketName, zipFilePath, vpcIp, vpcConnectorName string) string {
10571066
return fmt.Sprintf(`
10581067
data "google_project" "project" {}
10591068

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-
10661069
resource "google_compute_network" "vpc" {
10671070
name = "%s"
10681071
auto_create_subnetworks = false
@@ -1110,23 +1113,15 @@ resource "google_cloudfunctions_function" "function" {
11101113
min_instances = 3
11111114
vpc_connector = google_vpc_access_connector.%s.self_link
11121115
vpc_connector_egress_settings = "PRIVATE_RANGES_ONLY"
1113-
1114-
depends_on = [google_project_iam_member.gcfadmin]
11151116
}
1116-
`, projectNumber, networkName, vpcConnectorName, vpcConnectorName, vpcIp, bucketName, zipFilePath, functionName, vpcConnectorName)
1117+
`, networkName, vpcConnectorName, vpcConnectorName, vpcIp, bucketName, zipFilePath, functionName, vpcConnectorName)
11171118
}
11181119

11191120

1120-
func testAccCloudFunctionsFunction_vpcConnectorEgressSettings(projectNumber, networkName, functionName, bucketName, zipFilePath, vpcIp, vpcConnectorName, vpcConnectorEgressSettings string) string {
1121+
func testAccCloudFunctionsFunction_vpcConnectorEgressSettings(networkName, functionName, bucketName, zipFilePath, vpcIp, vpcConnectorName, vpcConnectorEgressSettings string) string {
11211122
return fmt.Sprintf(`
11221123
data "google_project" "project" {}
11231124

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-
11301125
resource "google_compute_network" "vpc" {
11311126
name = "%s"
11321127
auto_create_subnetworks = false
@@ -1174,10 +1169,8 @@ resource "google_cloudfunctions_function" "function" {
11741169
min_instances = 3
11751170
vpc_connector = google_vpc_access_connector.%s.self_link
11761171
vpc_connector_egress_settings = "%s"
1177-
1178-
depends_on = [google_project_iam_member.gcfadmin]
11791172
}
1180-
`, projectNumber, networkName, vpcConnectorName, vpcConnectorName, vpcIp, bucketName, zipFilePath, functionName, vpcConnectorName, vpcConnectorEgressSettings)
1173+
`, networkName, vpcConnectorName, vpcConnectorName, vpcIp, bucketName, zipFilePath, functionName, vpcConnectorName, vpcConnectorEgressSettings)
11811174
}
11821175

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

0 commit comments

Comments
 (0)