Skip to content

Commit

Permalink
Add Build Enviroment Vars to Cloud Functions (#4133) (#7596)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Oct 21, 2020
1 parent cd066c1 commit 3281560
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .changelog/4133.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
functions: added `build_environment_variables` field to `google_cloudfunction_function`
```
15 changes: 15 additions & 0 deletions google/resource_cloudfunctions_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down
102 changes: 60 additions & 42 deletions google/resource_cloudfunctions_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
})
Expand Down Expand Up @@ -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),
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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"
Expand 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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/cloudfunctions_function.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3281560

Please sign in to comment.