Skip to content

Commit

Permalink
Add service_account_email to google_cloudfunctions_function
Browse files Browse the repository at this point in the history
  • Loading branch information
rileykarson committed Jan 28, 2019
1 parent 107fcae commit 996e162
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 996e162

Please sign in to comment.