Skip to content

Commit 959623e

Browse files
Workflows: Add user defined environment variables feature to ga and update tests (#9406) (#16477)
[upstream:8533038b4b46983ca5d7dc55ca62df76ba06c710] Signed-off-by: Modular Magician <[email protected]>
1 parent 10d504d commit 959623e

File tree

5 files changed

+58
-60
lines changed

5 files changed

+58
-60
lines changed

.changelog/9406.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note: new-resource
2+
workflows: promoted `user_env_vars` to GA on resource `google_workflows_workflow`
3+
```

google/services/workflows/resource_workflows_workflow.go

+40
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ Modifying this field for an existing workflow results in a new workflow revision
115115
Optional: true,
116116
Description: `Workflow code to be executed. The size limit is 32KB.`,
117117
},
118+
"user_env_vars": {
119+
Type: schema.TypeMap,
120+
Optional: true,
121+
Description: `User-defined environment variables associated with this workflow revision. This map has a maximum length of 20. Each string can take up to 40KiB. Keys cannot be empty strings and cannot start with “GOOGLE” or “WORKFLOWS".`,
122+
Elem: &schema.Schema{Type: schema.TypeString},
123+
},
118124
"create_time": {
119125
Type: schema.TypeString,
120126
Computed: true,
@@ -204,6 +210,12 @@ func resourceWorkflowsWorkflowCreate(d *schema.ResourceData, meta interface{}) e
204210
} else if v, ok := d.GetOkExists("crypto_key_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(cryptoKeyNameProp)) && (ok || !reflect.DeepEqual(v, cryptoKeyNameProp)) {
205211
obj["cryptoKeyName"] = cryptoKeyNameProp
206212
}
213+
userEnvVarsProp, err := expandWorkflowsWorkflowUserEnvVars(d.Get("user_env_vars"), d, config)
214+
if err != nil {
215+
return err
216+
} else if v, ok := d.GetOkExists("user_env_vars"); !tpgresource.IsEmptyValue(reflect.ValueOf(userEnvVarsProp)) && (ok || !reflect.DeepEqual(v, userEnvVarsProp)) {
217+
obj["userEnvVars"] = userEnvVarsProp
218+
}
207219
labelsProp, err := expandWorkflowsWorkflowEffectiveLabels(d.Get("effective_labels"), d, config)
208220
if err != nil {
209221
return err
@@ -354,6 +366,9 @@ func resourceWorkflowsWorkflowRead(d *schema.ResourceData, meta interface{}) err
354366
if err := d.Set("crypto_key_name", flattenWorkflowsWorkflowCryptoKeyName(res["cryptoKeyName"], d, config)); err != nil {
355367
return fmt.Errorf("Error reading Workflow: %s", err)
356368
}
369+
if err := d.Set("user_env_vars", flattenWorkflowsWorkflowUserEnvVars(res["userEnvVars"], d, config)); err != nil {
370+
return fmt.Errorf("Error reading Workflow: %s", err)
371+
}
357372
if err := d.Set("terraform_labels", flattenWorkflowsWorkflowTerraformLabels(res["labels"], d, config)); err != nil {
358373
return fmt.Errorf("Error reading Workflow: %s", err)
359374
}
@@ -404,6 +419,12 @@ func resourceWorkflowsWorkflowUpdate(d *schema.ResourceData, meta interface{}) e
404419
} else if v, ok := d.GetOkExists("crypto_key_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, cryptoKeyNameProp)) {
405420
obj["cryptoKeyName"] = cryptoKeyNameProp
406421
}
422+
userEnvVarsProp, err := expandWorkflowsWorkflowUserEnvVars(d.Get("user_env_vars"), d, config)
423+
if err != nil {
424+
return err
425+
} else if v, ok := d.GetOkExists("user_env_vars"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, userEnvVarsProp)) {
426+
obj["userEnvVars"] = userEnvVarsProp
427+
}
407428
labelsProp, err := expandWorkflowsWorkflowEffectiveLabels(d.Get("effective_labels"), d, config)
408429
if err != nil {
409430
return err
@@ -440,6 +461,10 @@ func resourceWorkflowsWorkflowUpdate(d *schema.ResourceData, meta interface{}) e
440461
updateMask = append(updateMask, "cryptoKeyName")
441462
}
442463

464+
if d.HasChange("user_env_vars") {
465+
updateMask = append(updateMask, "userEnvVars")
466+
}
467+
443468
if d.HasChange("effective_labels") {
444469
updateMask = append(updateMask, "labels")
445470
}
@@ -592,6 +617,10 @@ func flattenWorkflowsWorkflowCryptoKeyName(v interface{}, d *schema.ResourceData
592617
return v
593618
}
594619

620+
func flattenWorkflowsWorkflowUserEnvVars(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
621+
return v
622+
}
623+
595624
func flattenWorkflowsWorkflowTerraformLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
596625
if v == nil {
597626
return v
@@ -631,6 +660,17 @@ func expandWorkflowsWorkflowCryptoKeyName(v interface{}, d tpgresource.Terraform
631660
return v, nil
632661
}
633662

663+
func expandWorkflowsWorkflowUserEnvVars(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
664+
if v == nil {
665+
return map[string]string{}, nil
666+
}
667+
m := make(map[string]string)
668+
for k, val := range v.(map[string]interface{}) {
669+
m[k] = val.(string)
670+
}
671+
return m, nil
672+
}
673+
634674
func expandWorkflowsWorkflowEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
635675
if v == nil {
636676
return map[string]string{}, nil

google/services/workflows/resource_workflows_workflow_generated_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ resource "google_workflows_workflow" "example" {
6464
labels = {
6565
env = "test"
6666
}
67+
user_env_vars = {
68+
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
69+
}
6770
source_contents = <<-EOF
6871
# This is a sample workflow. You can replace it with your source code.
6972
#
@@ -79,7 +82,7 @@ resource "google_workflows_workflow" "example" {
7982
- getCurrentTime:
8083
call: http.get
8184
args:
82-
url: https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam
85+
url: $${sys.get_env("url")}
8386
result: currentTime
8487
- readWikipedia:
8588
call: http.get

google/services/workflows/resource_workflows_workflow_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ resource "google_workflows_workflow" "example" {
4141
name = "%s"
4242
region = "us-central1"
4343
description = "Magic"
44+
user_env_vars = {
45+
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
46+
}
4447
source_contents = <<-EOF
4548
# This is a sample workflow, feel free to replace it with your source code
4649
#
@@ -55,7 +58,7 @@ resource "google_workflows_workflow" "example" {
5558
- getCurrentTime:
5659
call: http.get
5760
args:
58-
url: https://us-central1-workflowsample.cloudfunctions.net/datetime
61+
url: $${sys.get_env("url")}
5962
result: CurrentDateTime
6063
- readWikipedia:
6164
call: http.get
@@ -78,6 +81,9 @@ resource "google_workflows_workflow" "example" {
7881
name = "%s"
7982
region = "us-central1"
8083
description = "Magic"
84+
user_env_vars = {
85+
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
86+
}
8187
source_contents = <<-EOF
8288
# This is a sample workflow, feel free to replace it with your source code
8389
#
@@ -92,7 +98,7 @@ resource "google_workflows_workflow" "example" {
9298
- getCurrentTime:
9399
call: http.get
94100
args:
95-
url: https://us-central1-workflowsample.cloudfunctions.net/datetime
101+
url: $${sys.get_env("url")}
96102
result: CurrentDateTime
97103
- readWikipedia:
98104
call: http.get

website/docs/r/workflows_workflow.html.markdown

+3-57
Original file line numberDiff line numberDiff line change
@@ -50,62 +50,8 @@ resource "google_workflows_workflow" "example" {
5050
labels = {
5151
env = "test"
5252
}
53-
source_contents = <<-EOF
54-
# This is a sample workflow. You can replace it with your source code.
55-
#
56-
# This workflow does the following:
57-
# - reads current time and date information from an external API and stores
58-
# the response in currentTime variable
59-
# - retrieves a list of Wikipedia articles related to the day of the week
60-
# from currentTime
61-
# - returns the list of articles as an output of the workflow
62-
#
63-
# Note: In Terraform you need to escape the $$ or it will cause errors.
64-
65-
- getCurrentTime:
66-
call: http.get
67-
args:
68-
url: https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam
69-
result: currentTime
70-
- readWikipedia:
71-
call: http.get
72-
args:
73-
url: https://en.wikipedia.org/w/api.php
74-
query:
75-
action: opensearch
76-
search: $${currentTime.body.dayOfWeek}
77-
result: wikiResult
78-
- returnOutput:
79-
return: $${wikiResult.body[1]}
80-
EOF
81-
}
82-
```
83-
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
84-
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=workflow_beta&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
85-
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
86-
</a>
87-
</div>
88-
## Example Usage - Workflow Beta
89-
90-
91-
```hcl
92-
resource "google_service_account" "test_account" {
93-
provider = google-beta
94-
account_id = "my-account"
95-
display_name = "Test Service Account"
96-
}
97-
98-
resource "google_workflows_workflow" "example_beta" {
99-
provider = google-beta
100-
name = "workflow_beta"
101-
region = "us-central1"
102-
description = "Magic"
103-
service_account = google_service_account.test_account.id
104-
labels = {
105-
env = "test"
106-
}
10753
user_env_vars = {
108-
foo = "BAR"
54+
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
10955
}
11056
source_contents = <<-EOF
11157
# This is a sample workflow. You can replace it with your source code.
@@ -122,7 +68,7 @@ resource "google_workflows_workflow" "example_beta" {
12268
- getCurrentTime:
12369
call: http.get
12470
args:
125-
url: https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam
71+
url: $${sys.get_env("url")}
12672
result: currentTime
12773
- readWikipedia:
12874
call: http.get
@@ -182,7 +128,7 @@ The following arguments are supported:
182128
Format: projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}
183129

184130
* `user_env_vars` -
185-
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
131+
(Optional)
186132
User-defined environment variables associated with this workflow revision. This map has a maximum length of 20. Each string can take up to 40KiB. Keys cannot be empty strings and cannot start with “GOOGLE” or “WORKFLOWS".
187133

188134
* `region` -

0 commit comments

Comments
 (0)