diff --git a/google/resource_cloudfunctions_function.go b/google/resource_cloudfunctions_function.go index 3e4b2806..980f2e6f 100644 --- a/google/resource_cloudfunctions_function.go +++ b/google/resource_cloudfunctions_function.go @@ -190,6 +190,13 @@ func resourceCloudFunctionsFunction() *schema.Resource { Computed: true, }, + "service_account_email": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "environment_variables": { Type: schema.TypeMap, Optional: true, @@ -312,9 +319,10 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro } function := &cloudfunctions.CloudFunction{ - Name: cloudFuncId.cloudFunctionId(), - Runtime: d.Get("runtime").(string), - ForceSendFields: []string{}, + Name: cloudFuncId.cloudFunctionId(), + Runtime: d.Get("runtime").(string), + ServiceAccountEmail: d.Get("service_account_email").(string), + ForceSendFields: []string{}, } sourceRepos := d.Get("source_repository").([]interface{}) @@ -406,6 +414,7 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error d.Set("timeout", timeout) d.Set("labels", function.Labels) d.Set("runtime", function.Runtime) + d.Set("service_account_email", function.ServiceAccountEmail) d.Set("environment_variables", function.EnvironmentVariables) if function.SourceArchiveUrl != "" { // sourceArchiveUrl should always be a Google Cloud Storage URL (e.g. gs://bucket/object) diff --git a/google/resource_cloudfunctions_function_test.go b/google/resource_cloudfunctions_function_test.go index a2403de1..b7cdcb0e 100644 --- a/google/resource_cloudfunctions_function_test.go +++ b/google/resource_cloudfunctions_function_test.go @@ -258,6 +258,35 @@ func TestAccCloudFunctionsFunction_sourceRepo(t *testing.T) { }) } +func TestAccCloudFunctionsFunction_serviceAccountEmail(t *testing.T) { + t.Parallel() + + funcResourceName := "google_cloudfunctions_function.function" + functionName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + bucketName := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt()) + zipFilePath, err := createZIPArchiveForIndexJs(testHTTPTriggerPath) + if err != nil { + t.Fatal(err.Error()) + } + defer os.Remove(zipFilePath) // clean up + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFunctionsFunctionDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCloudFunctionsFunction_serviceAccountEmail(functionName, bucketName, zipFilePath), + }, + { + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckCloudFunctionsFunctionDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -607,3 +636,30 @@ resource "google_cloudfunctions_function" "function" { } `, functionName, project) } + +func testAccCloudFunctionsFunction_serviceAccountEmail(functionName, bucketName, zipFilePath string) string { + return fmt.Sprintf(` +resource "google_storage_bucket" "bucket" { + name = "%s" +} + +resource "google_storage_bucket_object" "archive" { + name = "index.zip" + bucket = "${google_storage_bucket.bucket.name}" + source = "%s" +} + +data "google_compute_default_service_account" "default" { } + +resource "google_cloudfunctions_function" "function" { + name = "%s" + + source_archive_bucket = "${google_storage_bucket.bucket.name}" + source_archive_object = "${google_storage_bucket_object.archive.name}" + + service_account_email = "${data.google_compute_default_service_account.default.email}" + + trigger_http = true + entry_point = "helloGET" +}`, bucketName, zipFilePath, functionName) +} diff --git a/website/docs/r/cloudfunctions_function.html.markdown b/website/docs/r/cloudfunctions_function.html.markdown index 4d302ffa..88818565 100644 --- a/website/docs/r/cloudfunctions_function.html.markdown +++ b/website/docs/r/cloudfunctions_function.html.markdown @@ -69,6 +69,8 @@ The following arguments are supported: * `runtime` - (Optional) The runtime in which the function is going to run. If empty, defaults to `"nodejs6"`. +* `service_account_email` - (Optional) If provided, the self-provided service account to run the function with. + * `environment_variables` - (Optional) A set of key/value environment variable pairs to assign to the function. * `source_archive_bucket` - (Optional) The GCS bucket containing the zip archive which contains the function.