From 19cc098a6bd14ca9d0e55ac11b9b0e808776fcb2 Mon Sep 17 00:00:00 2001 From: Riley Karson Date: Mon, 28 Jan 2019 21:26:21 +0000 Subject: [PATCH] Add service_account_email to google_cloudfunctions_function Signed-off-by: Modular Magician --- google/resource_cloudfunctions_function.go | 15 ++++- .../resource_cloudfunctions_function_test.go | 56 +++++++++++++++++++ .../r/cloudfunctions_function.html.markdown | 2 + 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/google/resource_cloudfunctions_function.go b/google/resource_cloudfunctions_function.go index 3e4b2806c33..980f2e6faf8 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 a2403de1884..b7cdcb0e136 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 4d302ffa415..888185651dd 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.