Skip to content

Commit 385ac49

Browse files
modular-magicianrileykarson
authored andcommitted
Add service_account_email to google_cloudfunctions_function (#2947)
Signed-off-by: Modular Magician <[email protected]>
1 parent 08c73f2 commit 385ac49

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

google/resource_cloudfunctions_function.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ func resourceCloudFunctionsFunction() *schema.Resource {
190190
Computed: true,
191191
},
192192

193+
"service_account_email": {
194+
Type: schema.TypeString,
195+
Optional: true,
196+
Computed: true,
197+
ForceNew: true,
198+
},
199+
193200
"environment_variables": {
194201
Type: schema.TypeMap,
195202
Optional: true,
@@ -312,9 +319,10 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro
312319
}
313320

314321
function := &cloudfunctions.CloudFunction{
315-
Name: cloudFuncId.cloudFunctionId(),
316-
Runtime: d.Get("runtime").(string),
317-
ForceSendFields: []string{},
322+
Name: cloudFuncId.cloudFunctionId(),
323+
Runtime: d.Get("runtime").(string),
324+
ServiceAccountEmail: d.Get("service_account_email").(string),
325+
ForceSendFields: []string{},
318326
}
319327

320328
sourceRepos := d.Get("source_repository").([]interface{})
@@ -406,6 +414,7 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error
406414
d.Set("timeout", timeout)
407415
d.Set("labels", function.Labels)
408416
d.Set("runtime", function.Runtime)
417+
d.Set("service_account_email", function.ServiceAccountEmail)
409418
d.Set("environment_variables", function.EnvironmentVariables)
410419
if function.SourceArchiveUrl != "" {
411420
// sourceArchiveUrl should always be a Google Cloud Storage URL (e.g. gs://bucket/object)

google/resource_cloudfunctions_function_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,35 @@ func TestAccCloudFunctionsFunction_sourceRepo(t *testing.T) {
258258
})
259259
}
260260

261+
func TestAccCloudFunctionsFunction_serviceAccountEmail(t *testing.T) {
262+
t.Parallel()
263+
264+
funcResourceName := "google_cloudfunctions_function.function"
265+
functionName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
266+
bucketName := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())
267+
zipFilePath, err := createZIPArchiveForIndexJs(testHTTPTriggerPath)
268+
if err != nil {
269+
t.Fatal(err.Error())
270+
}
271+
defer os.Remove(zipFilePath) // clean up
272+
273+
resource.Test(t, resource.TestCase{
274+
PreCheck: func() { testAccPreCheck(t) },
275+
Providers: testAccProviders,
276+
CheckDestroy: testAccCheckCloudFunctionsFunctionDestroy,
277+
Steps: []resource.TestStep{
278+
{
279+
Config: testAccCloudFunctionsFunction_serviceAccountEmail(functionName, bucketName, zipFilePath),
280+
},
281+
{
282+
ResourceName: funcResourceName,
283+
ImportState: true,
284+
ImportStateVerify: true,
285+
},
286+
},
287+
})
288+
}
289+
261290
func testAccCheckCloudFunctionsFunctionDestroy(s *terraform.State) error {
262291
config := testAccProvider.Meta().(*Config)
263292

@@ -607,3 +636,30 @@ resource "google_cloudfunctions_function" "function" {
607636
}
608637
`, functionName, project)
609638
}
639+
640+
func testAccCloudFunctionsFunction_serviceAccountEmail(functionName, bucketName, zipFilePath string) string {
641+
return fmt.Sprintf(`
642+
resource "google_storage_bucket" "bucket" {
643+
name = "%s"
644+
}
645+
646+
resource "google_storage_bucket_object" "archive" {
647+
name = "index.zip"
648+
bucket = "${google_storage_bucket.bucket.name}"
649+
source = "%s"
650+
}
651+
652+
data "google_compute_default_service_account" "default" { }
653+
654+
resource "google_cloudfunctions_function" "function" {
655+
name = "%s"
656+
657+
source_archive_bucket = "${google_storage_bucket.bucket.name}"
658+
source_archive_object = "${google_storage_bucket_object.archive.name}"
659+
660+
service_account_email = "${data.google_compute_default_service_account.default.email}"
661+
662+
trigger_http = true
663+
entry_point = "helloGET"
664+
}`, bucketName, zipFilePath, functionName)
665+
}

website/docs/r/cloudfunctions_function.html.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ The following arguments are supported:
6969

7070
* `runtime` - (Optional) The runtime in which the function is going to run. If empty, defaults to `"nodejs6"`.
7171

72+
* `service_account_email` - (Optional) If provided, the self-provided service account to run the function with.
73+
7274
* `environment_variables` - (Optional) A set of key/value environment variable pairs to assign to the function.
7375

7476
* `source_archive_bucket` - (Optional) The GCS bucket containing the zip archive which contains the function.

0 commit comments

Comments
 (0)