From 03c0a9e44fa265d735b9d38a4de6dabe0a090c63 Mon Sep 17 00:00:00 2001 From: upodroid Date: Wed, 21 Oct 2020 11:24:28 +0100 Subject: [PATCH 1/2] add build env variable field --- .../resource_cloudfunctions_function.go | 18 +++++++++++++ ...source_cloudfunctions_function_test.go.erb | 25 ++++++++++++------- third_party/terraform/utils/utils.go.erb | 5 ++++ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/third_party/terraform/resources/resource_cloudfunctions_function.go b/third_party/terraform/resources/resource_cloudfunctions_function.go index 3eddabaf8b19..7bd85b955f99 100644 --- a/third_party/terraform/resources/resource_cloudfunctions_function.go +++ b/third_party/terraform/resources/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 to assign to the function.`, + }, + "trigger_http": { Type: schema.TypeBool, Optional: true, @@ -412,6 +418,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) } @@ -502,6 +512,9 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error if err := d.Set("environment_variables", function.EnvironmentVariables); err != nil { return fmt.Errorf("Error setting environment_variables: %s", err) } + if err := d.Set("build_environment_variables", function.BuildEnvironmentVariables); err != nil { + return fmt.Errorf("Error setting environment_variables: %s", err) + } if err := d.Set("vpc_connector", function.VpcConnector); err != nil { return fmt.Errorf("Error setting vpc_connector: %s", err) } @@ -627,6 +640,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/third_party/terraform/tests/resource_cloudfunctions_function_test.go.erb b/third_party/terraform/tests/resource_cloudfunctions_function_test.go.erb index 6d020165378f..b244bce77d9b 100644 --- a/third_party/terraform/tests/resource_cloudfunctions_function_test.go.erb +++ b/third_party/terraform/tests/resource_cloudfunctions_function_test.go.erb @@ -591,7 +591,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 @@ -606,6 +606,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) @@ -630,7 +633,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" @@ -642,6 +645,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) @@ -666,7 +673,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 @@ -701,7 +708,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 @@ -733,7 +740,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 @@ -762,7 +769,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 @@ -780,7 +787,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 @@ -813,7 +820,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 @@ -858,7 +865,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/third_party/terraform/utils/utils.go.erb b/third_party/terraform/utils/utils.go.erb index c5bd2f6c5260..75db7a1594ac 100644 --- a/third_party/terraform/utils/utils.go.erb +++ b/third_party/terraform/utils/utils.go.erb @@ -163,6 +163,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) From 356764300c5743ec125adf5c3e805feedb3b57db Mon Sep 17 00:00:00 2001 From: upodroid Date: Wed, 21 Oct 2020 12:27:44 +0100 Subject: [PATCH 2/2] add docs and tweak import tests --- .../resource_cloudfunctions_function.go | 5 +- ...source_cloudfunctions_function_test.go.erb | 77 +++++++++++-------- .../r/cloudfunctions_function.html.markdown | 2 + 3 files changed, 47 insertions(+), 37 deletions(-) diff --git a/third_party/terraform/resources/resource_cloudfunctions_function.go b/third_party/terraform/resources/resource_cloudfunctions_function.go index 7bd85b955f99..5aaf3c22db76 100644 --- a/third_party/terraform/resources/resource_cloudfunctions_function.go +++ b/third_party/terraform/resources/resource_cloudfunctions_function.go @@ -252,7 +252,7 @@ func resourceCloudFunctionsFunction() *schema.Resource { "build_environment_variables": { Type: schema.TypeMap, Optional: true, - Description: `A set of key/value environment variable pairs to assign to the function.`, + Description: ` A set of key/value environment variable pairs available during build time.`, }, "trigger_http": { @@ -512,9 +512,6 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error if err := d.Set("environment_variables", function.EnvironmentVariables); err != nil { return fmt.Errorf("Error setting environment_variables: %s", err) } - if err := d.Set("build_environment_variables", function.BuildEnvironmentVariables); err != nil { - return fmt.Errorf("Error setting environment_variables: %s", err) - } if err := d.Set("vpc_connector", function.VpcConnector); err != nil { return fmt.Errorf("Error setting vpc_connector: %s", err) } diff --git a/third_party/terraform/tests/resource_cloudfunctions_function_test.go.erb b/third_party/terraform/tests/resource_cloudfunctions_function_test.go.erb index b244bce77d9b..452b2a1039ab 100644 --- a/third_party/terraform/tests/resource_cloudfunctions_function_test.go.erb +++ b/third_party/terraform/tests/resource_cloudfunctions_function_test.go.erb @@ -160,9 +160,10 @@ func TestAccCloudFunctionsFunction_basic(t *testing.T) { ), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -195,9 +196,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), @@ -223,9 +225,10 @@ func TestAccCloudFunctionsFunction_update(t *testing.T) { ), }, { - ResourceName: funcResourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: funcResourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"build_environment_variables"}, }, }, }) @@ -251,9 +254,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"}, }, }, }) @@ -276,17 +280,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"}, }, }, }) @@ -309,9 +315,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"}, }, }, }) @@ -333,9 +340,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"}, }, }, }) @@ -359,9 +367,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"}, }, }, }) @@ -388,17 +397,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"}, }, }, }) diff --git a/third_party/terraform/website/docs/r/cloudfunctions_function.html.markdown b/third_party/terraform/website/docs/r/cloudfunctions_function.html.markdown index 18df933e787a..18f3b85098a1 100644 --- a/third_party/terraform/website/docs/r/cloudfunctions_function.html.markdown +++ b/third_party/terraform/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.