Skip to content

Commit 65669fc

Browse files
hpidcocknat-henderson
authored andcommitted
Added Environment Variables configuration to Cloud Functions (#1830)
1 parent 37de431 commit 65669fc

File tree

7 files changed

+775
-648
lines changed

7 files changed

+775
-648
lines changed

google/resource_cloudfunctions_function.go

+15
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ func resourceCloudFunctionsFunction() *schema.Resource {
169169
Optional: true,
170170
},
171171

172+
"environment_variables": {
173+
Type: schema.TypeMap,
174+
Optional: true,
175+
},
176+
172177
"trigger_bucket": {
173178
Type: schema.TypeString,
174179
Optional: true,
@@ -319,6 +324,10 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro
319324
function.Labels = expandLabels(d)
320325
}
321326

327+
if _, ok := d.GetOk("environment_variables"); ok {
328+
function.EnvironmentVariables = expandEnvironmentVariables(d)
329+
}
330+
322331
log.Printf("[DEBUG] Creating cloud function: %s", function.Name)
323332
op, err := config.clientCloudFunctions.Projects.Locations.Functions.Create(
324333
cloudFuncId.locationId(), function).Do()
@@ -361,6 +370,7 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error
361370
}
362371
d.Set("timeout", timeout)
363372
d.Set("labels", function.Labels)
373+
d.Set("environment_variables", function.EnvironmentVariables)
364374
if function.SourceArchiveUrl != "" {
365375
// sourceArchiveUrl should always be a Google Cloud Storage URL (e.g. gs://bucket/object)
366376
// https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions
@@ -440,6 +450,11 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro
440450
updateMaskArr = append(updateMaskArr, "labels")
441451
}
442452

453+
if d.HasChange("environment_variables") {
454+
function.EnvironmentVariables = expandEnvironmentVariables(d)
455+
updateMaskArr = append(updateMaskArr, "environment_variables")
456+
}
457+
443458
if d.HasChange("retry_on_failure") {
444459
if d.Get("retry_on_failure").(bool) {
445460
if function.EventTrigger == nil {

google/resource_cloudfunctions_function_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ func TestAccCloudFunctionsFunction_basic(t *testing.T) {
6666
resource.TestCheckResourceAttr(funcResourceName,
6767
"trigger_http", "true"),
6868
testAccCloudFunctionsFunctionHasLabel("my-label", "my-label-value", &function),
69+
testAccCloudFunctionsFunctionHasEnvironmentVariable("TEST_ENV_VARIABLE",
70+
"test-env-variable-value", &function),
6971
),
7072
},
7173
{
@@ -119,6 +121,10 @@ func TestAccCloudFunctionsFunction_update(t *testing.T) {
119121
"timeout", "91"),
120122
testAccCloudFunctionsFunctionHasLabel("my-label", "my-updated-label-value", &function),
121123
testAccCloudFunctionsFunctionHasLabel("a-new-label", "a-new-label-value", &function),
124+
testAccCloudFunctionsFunctionHasEnvironmentVariable("TEST_ENV_VARIABLE",
125+
"test-env-variable-value", &function),
126+
testAccCloudFunctionsFunctionHasEnvironmentVariable("NEW_ENV_VARIABLE",
127+
"new-env-variable-value", &function),
122128
),
123129
},
124130
},
@@ -347,6 +353,21 @@ func testAccCloudFunctionsFunctionHasLabel(key, value string,
347353
}
348354
}
349355

356+
func testAccCloudFunctionsFunctionHasEnvironmentVariable(key, value string,
357+
function *cloudfunctions.CloudFunction) resource.TestCheckFunc {
358+
return func(s *terraform.State) error {
359+
if val, ok := function.EnvironmentVariables[key]; ok {
360+
if val != value {
361+
return fmt.Errorf("Environment Variable value did not match for key %s: expected %s but found %s",
362+
key, value, val)
363+
}
364+
} else {
365+
return fmt.Errorf("Environment Variable with key %s not found", key)
366+
}
367+
return nil
368+
}
369+
}
370+
350371
func createZIPArchiveForIndexJs(sourcePath string) (string, error) {
351372
source, err := ioutil.ReadFile(sourcePath)
352373
if err != nil {
@@ -411,6 +432,9 @@ resource "google_cloudfunctions_function" "function" {
411432
labels {
412433
my-label = "my-label-value"
413434
}
435+
environment_variables {
436+
TEST_ENV_VARIABLE = "test-env-variable-value"
437+
}
414438
}
415439
`, bucketName, zipFilePath, functionName)
416440
}
@@ -440,6 +464,10 @@ resource "google_cloudfunctions_function" "function" {
440464
my-label = "my-updated-label-value"
441465
a-new-label = "a-new-label-value"
442466
}
467+
environment_variables {
468+
TEST_ENV_VARIABLE = "test-env-variable-value"
469+
NEW_ENV_VARIABLE = "new-env-variable-value"
470+
}
443471
}`, bucketName, zipFilePath, functionName)
444472
}
445473

google/utils.go

+5
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ func expandLabels(d *schema.ResourceData) map[string]string {
272272
return expandStringMap(d, "labels")
273273
}
274274

275+
// expandEnvironmentVariables pulls the value of "environment_variables" out of a schema.ResourceData as a map[string]string.
276+
func expandEnvironmentVariables(d *schema.ResourceData) map[string]string {
277+
return expandStringMap(d, "environment_variables")
278+
}
279+
275280
// expandStringMap pulls the value of key out of a schema.ResourceData as a map[string]string.
276281
func expandStringMap(d *schema.ResourceData, key string) map[string]string {
277282
v, ok := d.GetOk(key)

0 commit comments

Comments
 (0)