Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Build Enviroment Vars to Cloud Functions #4133

Merged
merged 7 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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)
}
Expand Down Expand Up @@ -627,6 +637,11 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro
updateMaskArr = append(updateMaskArr, "environmentVariables")
}

if d.HasChange("build_environment_variables") {
function.EnvironmentVariables = expandEnvironmentVariables(d)
slevenick marked this conversation as resolved.
Show resolved Hide resolved
updateMaskArr = append(updateMaskArr, "buildEnvironmentVariables")
}

if d.HasChange("vpc_connector") {
function.VpcConnector = d.Get("vpc_connector").(string)
updateMaskArr = append(updateMaskArr, "vpcConnector")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
c2thorn marked this conversation as resolved.
Show resolved Hide resolved
},
},
})
Expand Down Expand Up @@ -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),
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand Down Expand Up @@ -591,7 +602,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 @@ -606,6 +617,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 @@ -630,7 +644,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 @@ -642,6 +656,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 @@ -666,7 +684,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 @@ -701,7 +719,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 @@ -733,7 +751,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 @@ -762,7 +780,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 @@ -780,7 +798,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 @@ -813,7 +831,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 @@ -858,7 +876,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 third_party/terraform/utils/utils.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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