From cc5ebb321f1baed39bde488b1952df7e98950a3a Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Wed, 21 Oct 2020 18:43:38 +0000 Subject: [PATCH] Add Build Enviroment Vars to Cloud Functions (#4133) Signed-off-by: Modular Magician --- .changelog/4133.txt | 3 + google/resource_cloudfunctions_function.go | 15 +++ .../resource_cloudfunctions_function_test.go | 102 ++++++++++-------- google/utils.go | 5 + .../r/cloudfunctions_function.html.markdown | 2 + 5 files changed, 85 insertions(+), 42 deletions(-) create mode 100644 .changelog/4133.txt diff --git a/.changelog/4133.txt b/.changelog/4133.txt new file mode 100644 index 00000000000..76be0074ec0 --- /dev/null +++ b/.changelog/4133.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +functions: added `build_environment_variables` field to `google_cloudfunction_function` +``` diff --git a/google/resource_cloudfunctions_function.go b/google/resource_cloudfunctions_function.go index 27e4d7a5abb..b0881faf394 100644 --- a/google/resource_cloudfunctions_function.go +++ b/google/resource_cloudfunctions_function.go @@ -249,6 +249,12 @@ func resourceCloudFunctionsFunction() *schema.Resource { Description: `A set of key/value environment variable pairs to assign to the function.`, }, + "build_environment_variables": { + Type: schema.TypeMap, + Optional: true, + Description: ` A set of key/value environment variable pairs available during build time.`, + }, + "trigger_http": { Type: schema.TypeBool, Optional: true, @@ -411,6 +417,10 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro function.EnvironmentVariables = expandEnvironmentVariables(d) } + if _, ok := d.GetOk("build_environment_variables"); ok { + function.BuildEnvironmentVariables = expandBuildEnvironmentVariables(d) + } + if v, ok := d.GetOk("vpc_connector"); ok { function.VpcConnector = v.(string) } @@ -624,6 +634,11 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro updateMaskArr = append(updateMaskArr, "environmentVariables") } + if d.HasChange("build_environment_variables") { + function.EnvironmentVariables = expandEnvironmentVariables(d) + updateMaskArr = append(updateMaskArr, "buildEnvironmentVariables") + } + if d.HasChange("vpc_connector") { function.VpcConnector = d.Get("vpc_connector").(string) updateMaskArr = append(updateMaskArr, "vpcConnector") diff --git a/google/resource_cloudfunctions_function_test.go b/google/resource_cloudfunctions_function_test.go index f7bae6cd1b6..7daae1233a5 100644 --- a/google/resource_cloudfunctions_function_test.go +++ b/google/resource_cloudfunctions_function_test.go @@ -161,9 +161,10 @@ func TestAccCloudFunctionsFunction_basic(t *testing.T) { ), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -196,9 +197,10 @@ func TestAccCloudFunctionsFunction_update(t *testing.T) { ), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, { Config: testAccCloudFunctionsFunction_updated(functionName, bucketName, zipFileUpdatePath), @@ -224,9 +226,10 @@ func TestAccCloudFunctionsFunction_update(t *testing.T) { ), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -252,9 +255,10 @@ func TestAccCloudFunctionsFunction_pubsub(t *testing.T) { topicName, zipFilePath), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -277,17 +281,19 @@ func TestAccCloudFunctionsFunction_bucket(t *testing.T) { Config: testAccCloudFunctionsFunction_bucket(functionName, bucketName, zipFilePath), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, { Config: testAccCloudFunctionsFunction_bucketNoRetry(functionName, bucketName, zipFilePath), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -310,9 +316,10 @@ func TestAccCloudFunctionsFunction_firestore(t *testing.T) { Config: testAccCloudFunctionsFunction_firestore(functionName, bucketName, zipFilePath), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -334,9 +341,10 @@ func TestAccCloudFunctionsFunction_sourceRepo(t *testing.T) { Config: testAccCloudFunctionsFunction_sourceRepo(functionName, proj), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -360,9 +368,10 @@ func TestAccCloudFunctionsFunction_serviceAccountEmail(t *testing.T) { Config: testAccCloudFunctionsFunction_serviceAccountEmail(functionName, bucketName, zipFilePath), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -389,17 +398,19 @@ func TestAccCloudFunctionsFunction_vpcConnector(t *testing.T) { Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.10.0.0/28", vpcConnectorName), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, { Config: testAccCloudFunctionsFunction_vpcConnector(projectNumber, networkName, functionName, bucketName, zipFilePath, "10.20.0.0/28", vpcConnectorName+"-update"), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -592,7 +603,7 @@ resource "google_storage_bucket_object" "archive" { resource "google_cloudfunctions_function" "function" { name = "%s" - runtime = "nodejs8" + runtime = "nodejs10" description = "test function" available_memory_mb = 128 source_archive_bucket = google_storage_bucket.bucket.name @@ -607,6 +618,9 @@ resource "google_cloudfunctions_function" "function" { environment_variables = { TEST_ENV_VARIABLE = "test-env-variable-value" } + build_environment_variables = { + TEST_ENV_VARIABLE = "test-env-variable-value" + } max_instances = 10 } `, bucketName, zipFilePath, functionName) @@ -631,7 +645,7 @@ resource "google_cloudfunctions_function" "function" { source_archive_bucket = google_storage_bucket.bucket.name source_archive_object = google_storage_bucket_object.archive.name trigger_http = true - runtime = "nodejs8" + runtime = "nodejs10" timeout = 91 entry_point = "helloGET" ingress_settings = "ALLOW_ALL" @@ -643,6 +657,10 @@ resource "google_cloudfunctions_function" "function" { TEST_ENV_VARIABLE = "test-env-variable-value" NEW_ENV_VARIABLE = "new-env-variable-value" } + build_environment_variables = { + TEST_ENV_VARIABLE = "test-env-variable-value" + NEW_ENV_VARIABLE = "new-env-variable-value" + } max_instances = 15 } `, bucketName, zipFilePath, functionName) @@ -667,7 +685,7 @@ resource "google_pubsub_topic" "sub" { resource "google_cloudfunctions_function" "function" { name = "%s" - runtime = "nodejs8" + runtime = "nodejs10" available_memory_mb = 128 source_archive_bucket = google_storage_bucket.bucket.name source_archive_object = google_storage_bucket_object.archive.name @@ -702,7 +720,7 @@ resource "google_storage_bucket_object" "archive" { resource "google_cloudfunctions_function" "function" { name = "%s" - runtime = "nodejs8" + runtime = "nodejs10" available_memory_mb = 128 source_archive_bucket = google_storage_bucket.bucket.name source_archive_object = google_storage_bucket_object.archive.name @@ -734,7 +752,7 @@ resource "google_storage_bucket_object" "archive" { resource "google_cloudfunctions_function" "function" { name = "%s" - runtime = "nodejs8" + runtime = "nodejs10" available_memory_mb = 128 source_archive_bucket = google_storage_bucket.bucket.name source_archive_object = google_storage_bucket_object.archive.name @@ -763,7 +781,7 @@ resource "google_storage_bucket_object" "archive" { resource "google_cloudfunctions_function" "function" { name = "%s" - runtime = "nodejs8" + runtime = "nodejs10" available_memory_mb = 128 source_archive_bucket = google_storage_bucket.bucket.name source_archive_object = google_storage_bucket_object.archive.name @@ -781,7 +799,7 @@ func testAccCloudFunctionsFunction_sourceRepo(functionName, project string) stri return fmt.Sprintf(` resource "google_cloudfunctions_function" "function" { name = "%s" - runtime = "nodejs8" + runtime = "nodejs10" source_repository { // There isn't yet an API that'll allow us to create a source repository and @@ -814,7 +832,7 @@ data "google_compute_default_service_account" "default" { resource "google_cloudfunctions_function" "function" { name = "%s" - runtime = "nodejs8" + runtime = "nodejs10" source_archive_bucket = google_storage_bucket.bucket.name source_archive_object = google_storage_bucket_object.archive.name @@ -859,7 +877,7 @@ resource "google_storage_bucket_object" "archive" { resource "google_cloudfunctions_function" "function" { name = "%s" - runtime = "nodejs8" + runtime = "nodejs10" description = "test function" available_memory_mb = 128 diff --git a/google/utils.go b/google/utils.go index 034a65daec7..ce25414ab24 100644 --- a/google/utils.go +++ b/google/utils.go @@ -159,6 +159,11 @@ func expandEnvironmentVariables(d *schema.ResourceData) map[string]string { return expandStringMap(d, "environment_variables") } +// expandBuildEnvironmentVariables pulls the value of "build_environment_variables" out of a schema.ResourceData as a map[string]string. +func expandBuildEnvironmentVariables(d *schema.ResourceData) map[string]string { + return expandStringMap(d, "build_environment_variables") +} + // expandStringMap pulls the value of key out of a TerraformResourceData as a map[string]string. func expandStringMap(d TerraformResourceData, key string) map[string]string { v, ok := d.GetOk(key) diff --git a/website/docs/r/cloudfunctions_function.html.markdown b/website/docs/r/cloudfunctions_function.html.markdown index 18df933e787..18f3b85098a 100644 --- a/website/docs/r/cloudfunctions_function.html.markdown +++ b/website/docs/r/cloudfunctions_function.html.markdown @@ -131,6 +131,8 @@ Eg. `"nodejs8"`, `"nodejs10"`, `"python37"`, `"go111"`, `"go113"`. * `environment_variables` - (Optional) A set of key/value environment variable pairs to assign to the function. +* `build_environment_variables` - (Optional) A set of key/value environment variable pairs available during build time. + * `vpc_connector` - (Optional) The VPC Network Connector that this cloud function can connect to. It should be set up as fully-qualified URI. The format of this field is `projects/*/locations/*/connectors/*`. * `vpc_connector_egress_settings` - (Optional) The egress settings for the connector, controlling what traffic is diverted through it. Allowed values are `ALL_TRAFFIC` and `PRIVATE_RANGES_ONLY`. Defaults to `PRIVATE_RANGES_ONLY`. If unset, this field preserves the previously set value.