Skip to content

Commit

Permalink
Workflows: Add user defined environment variables feature to ga and u…
Browse files Browse the repository at this point in the history
…pdate tests (#9406) (#6618)

[upstream:8533038b4b46983ca5d7dc55ca62df76ba06c710]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Nov 8, 2023
1 parent 177b9fb commit d86a71c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 256 deletions.
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`
```
Original file line number Diff line number Diff line change
Expand Up @@ -64,76 +64,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
}
`, context)
}

func TestAccWorkflowsWorkflow_workflowBetaExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
CheckDestroy: testAccCheckWorkflowsWorkflowDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccWorkflowsWorkflow_workflowBetaExample(context),
},
},
})
}

func testAccWorkflowsWorkflow_workflowBetaExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_service_account" "test_account" {
provider = google-beta
account_id = "tf-test-my-account%{random_suffix}"
display_name = "Test Service Account"
}
resource "google_workflows_workflow" "example_beta" {
provider = google-beta
name = "tf_test_workflow_beta%{random_suffix}"
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 @@ -150,7 +82,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
137 changes: 8 additions & 129 deletions google-beta/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 Expand Up @@ -213,130 +219,3 @@ EOF
}
`, workflowName, kmsKeyName)
}

func TestAccWorkflowsWorkflowBeta_update(t *testing.T) {
// custom test to test updating
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
CheckDestroy: testAccCheckWorkflowsWorkflowDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccWorkflowsWorkflowBeta_full(context),
},
{
Config: testAccWorkflowsWorkflowBeta_update(context),
},
},
})
}

func testAccWorkflowsWorkflowBeta_full(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_service_account" "test_account" {
provider = google-beta
account_id = "tf-test-my-account%{random_suffix}"
display_name = "Test Service Account"
}
resource "google_workflows_workflow" "example_beta" {
provider = google-beta
name = "tf_test_workflow_beta%{random_suffix}"
region = "us-central1"
description = "Magic"
service_account = google_service_account.test_account.id
labels = {
env = "test"
}
user_env_vars = {
foo = "BAR"
}
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
}
`, context)
}

func testAccWorkflowsWorkflowBeta_update(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_service_account" "test_account" {
provider = google-beta
account_id = "tf-test-my-account%{random_suffix}"
display_name = "Test Service Account"
}
resource "google_workflows_workflow" "example_beta" {
provider = google-beta
name = "tf_test_workflow_beta%{random_suffix}"
region = "us-central1"
description = "Magic"
service_account = google_service_account.test_account.id
labels = {
env = "dev"
}
user_env_vars = {
bar = "FOO"
}
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
}
`, context)
}
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%2Fgithub.com%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

0 comments on commit d86a71c

Please sign in to comment.