From 02b240c2eb97146babb2bae93f36c170dddbbc2c Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Wed, 8 Nov 2023 18:56:57 +0000 Subject: [PATCH] Workflows: Add user defined environment variables feature to ga and update tests (#9406) [upstream:8533038b4b46983ca5d7dc55ca62df76ba06c710] Signed-off-by: Modular Magician --- .changelog/9406.txt | 3 + .../workflows/resource_workflows_workflow.go | 40 +++++++++++++ ...ource_workflows_workflow_generated_test.go | 5 +- .../resource_workflows_workflow_test.go | 10 +++- .../docs/r/workflows_workflow.html.markdown | 60 +------------------ 5 files changed, 58 insertions(+), 60 deletions(-) create mode 100644 .changelog/9406.txt diff --git a/.changelog/9406.txt b/.changelog/9406.txt new file mode 100644 index 00000000000..0d8fc222d51 --- /dev/null +++ b/.changelog/9406.txt @@ -0,0 +1,3 @@ +```release-note: new-resource +workflows: promoted `user_env_vars` to GA on resource `google_workflows_workflow` +``` diff --git a/google/services/workflows/resource_workflows_workflow.go b/google/services/workflows/resource_workflows_workflow.go index d7e2ad15f33..34e2203d47d 100644 --- a/google/services/workflows/resource_workflows_workflow.go +++ b/google/services/workflows/resource_workflows_workflow.go @@ -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, @@ -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 @@ -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) } @@ -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 @@ -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") } @@ -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 @@ -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 diff --git a/google/services/workflows/resource_workflows_workflow_generated_test.go b/google/services/workflows/resource_workflows_workflow_generated_test.go index 68c3dc1e38c..f56816e8fa2 100644 --- a/google/services/workflows/resource_workflows_workflow_generated_test.go +++ b/google/services/workflows/resource_workflows_workflow_generated_test.go @@ -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. # @@ -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 diff --git a/google/services/workflows/resource_workflows_workflow_test.go b/google/services/workflows/resource_workflows_workflow_test.go index fdf8368c168..05fad551e30 100644 --- a/google/services/workflows/resource_workflows_workflow_test.go +++ b/google/services/workflows/resource_workflows_workflow_test.go @@ -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 # @@ -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 @@ -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 # @@ -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 diff --git a/website/docs/r/workflows_workflow.html.markdown b/website/docs/r/workflows_workflow.html.markdown index 71d768d625c..81239660643 100644 --- a/website/docs/r/workflows_workflow.html.markdown +++ b/website/docs/r/workflows_workflow.html.markdown @@ -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 -} -``` - -## 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. @@ -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 @@ -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` -