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

Workflows: Add user defined environment variables feature to ga and update tests #16477

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
3 changes: 3 additions & 0 deletions .changelog/9406.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note: new-resource
workflows: promoted `user_env_vars` to GA on resource `google_workflows_workflow`
```
40 changes: 40 additions & 0 deletions google/services/workflows/resource_workflows_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ Modifying this field for an existing workflow results in a new workflow revision
Optional: true,
Description: `Workflow code to be executed. The size limit is 32KB.`,
},
"user_env_vars": {
Type: schema.TypeMap,
Optional: true,
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".`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -204,6 +210,12 @@ func resourceWorkflowsWorkflowCreate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("crypto_key_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(cryptoKeyNameProp)) && (ok || !reflect.DeepEqual(v, cryptoKeyNameProp)) {
obj["cryptoKeyName"] = cryptoKeyNameProp
}
userEnvVarsProp, err := expandWorkflowsWorkflowUserEnvVars(d.Get("user_env_vars"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("user_env_vars"); !tpgresource.IsEmptyValue(reflect.ValueOf(userEnvVarsProp)) && (ok || !reflect.DeepEqual(v, userEnvVarsProp)) {
obj["userEnvVars"] = userEnvVarsProp
}
labelsProp, err := expandWorkflowsWorkflowEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -354,6 +366,9 @@ func resourceWorkflowsWorkflowRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("crypto_key_name", flattenWorkflowsWorkflowCryptoKeyName(res["cryptoKeyName"], d, config)); err != nil {
return fmt.Errorf("Error reading Workflow: %s", err)
}
if err := d.Set("user_env_vars", flattenWorkflowsWorkflowUserEnvVars(res["userEnvVars"], d, config)); err != nil {
return fmt.Errorf("Error reading Workflow: %s", err)
}
if err := d.Set("terraform_labels", flattenWorkflowsWorkflowTerraformLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Workflow: %s", err)
}
Expand Down Expand Up @@ -404,6 +419,12 @@ func resourceWorkflowsWorkflowUpdate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("crypto_key_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, cryptoKeyNameProp)) {
obj["cryptoKeyName"] = cryptoKeyNameProp
}
userEnvVarsProp, err := expandWorkflowsWorkflowUserEnvVars(d.Get("user_env_vars"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("user_env_vars"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, userEnvVarsProp)) {
obj["userEnvVars"] = userEnvVarsProp
}
labelsProp, err := expandWorkflowsWorkflowEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -440,6 +461,10 @@ func resourceWorkflowsWorkflowUpdate(d *schema.ResourceData, meta interface{}) e
updateMask = append(updateMask, "cryptoKeyName")
}

if d.HasChange("user_env_vars") {
updateMask = append(updateMask, "userEnvVars")
}

if d.HasChange("effective_labels") {
updateMask = append(updateMask, "labels")
}
Expand Down Expand Up @@ -592,6 +617,10 @@ func flattenWorkflowsWorkflowCryptoKeyName(v interface{}, d *schema.ResourceData
return v
}

func flattenWorkflowsWorkflowUserEnvVars(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenWorkflowsWorkflowTerraformLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -631,6 +660,17 @@ func expandWorkflowsWorkflowCryptoKeyName(v interface{}, d tpgresource.Terraform
return v, nil
}

func expandWorkflowsWorkflowUserEnvVars(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}

func expandWorkflowsWorkflowEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ resource "google_workflows_workflow" "example" {
labels = {
env = "test"
}
user_env_vars = {
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
}
source_contents = <<-EOF
# This is a sample workflow. You can replace it with your source code.
#
Expand All @@ -79,7 +82,7 @@ resource "google_workflows_workflow" "example" {
- getCurrentTime:
call: http.get
args:
url: https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam
url: $${sys.get_env("url")}
result: currentTime
- readWikipedia:
call: http.get
Expand Down
10 changes: 8 additions & 2 deletions google/services/workflows/resource_workflows_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ resource "google_workflows_workflow" "example" {
name = "%s"
region = "us-central1"
description = "Magic"
user_env_vars = {
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
}
source_contents = <<-EOF
# This is a sample workflow, feel free to replace it with your source code
#
Expand All @@ -55,7 +58,7 @@ resource "google_workflows_workflow" "example" {
- getCurrentTime:
call: http.get
args:
url: https://us-central1-workflowsample.cloudfunctions.net/datetime
url: $${sys.get_env("url")}
result: CurrentDateTime
- readWikipedia:
call: http.get
Expand All @@ -78,6 +81,9 @@ resource "google_workflows_workflow" "example" {
name = "%s"
region = "us-central1"
description = "Magic"
user_env_vars = {
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
}
source_contents = <<-EOF
# This is a sample workflow, feel free to replace it with your source code
#
Expand All @@ -92,7 +98,7 @@ resource "google_workflows_workflow" "example" {
- getCurrentTime:
call: http.get
args:
url: https://us-central1-workflowsample.cloudfunctions.net/datetime
url: $${sys.get_env("url")}
result: CurrentDateTime
- readWikipedia:
call: http.get
Expand Down
60 changes: 3 additions & 57 deletions website/docs/r/workflows_workflow.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -50,62 +50,8 @@ resource "google_workflows_workflow" "example" {
labels = {
env = "test"
}
source_contents = <<-EOF
# This is a sample workflow. You can replace it with your source code.
#
# This workflow does the following:
# - reads current time and date information from an external API and stores
# the response in currentTime variable
# - retrieves a list of Wikipedia articles related to the day of the week
# from currentTime
# - returns the list of articles as an output of the workflow
#
# Note: In Terraform you need to escape the $$ or it will cause errors.

- getCurrentTime:
call: http.get
args:
url: https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam
result: currentTime
- readWikipedia:
call: http.get
args:
url: https://en.wikipedia.org/w/api.php
query:
action: opensearch
search: $${currentTime.body.dayOfWeek}
result: wikiResult
- returnOutput:
return: $${wikiResult.body[1]}
EOF
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgit.luolix.top%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">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Workflow Beta


```hcl
resource "google_service_account" "test_account" {
provider = google-beta
account_id = "my-account"
display_name = "Test Service Account"
}

resource "google_workflows_workflow" "example_beta" {
provider = google-beta
name = "workflow_beta"
region = "us-central1"
description = "Magic"
service_account = google_service_account.test_account.id
labels = {
env = "test"
}
user_env_vars = {
foo = "BAR"
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
}
source_contents = <<-EOF
# This is a sample workflow. You can replace it with your source code.
Expand All @@ -122,7 +68,7 @@ resource "google_workflows_workflow" "example_beta" {
- getCurrentTime:
call: http.get
args:
url: https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam
url: $${sys.get_env("url")}
result: currentTime
- readWikipedia:
call: http.get
Expand Down Expand Up @@ -182,7 +128,7 @@ The following arguments are supported:
Format: projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}

* `user_env_vars` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
(Optional)
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".

* `region` -
Expand Down