Skip to content

Add tags and workflow_tags fields to google_workflows_workflow resource. Fixes b/374990996 #9152

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

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/12085.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
workflows: added `tags` and `workflow_tags` fields to `google_workflows_workflow` resource
```
26 changes: 26 additions & 0 deletions google-beta/services/workflows/resource_workflows_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ 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 128KB.`,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: `A map of resource manager tags. Resource manager tag keys and values have the same definition
as resource manager tags. Keys must be in the format tagKeys/{tag_key_id}, and values are in
the format tagValues/456. The field is ignored (both PUT & PATCH) when empty.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"user_env_vars": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -243,6 +252,12 @@ func resourceWorkflowsWorkflowCreate(d *schema.ResourceData, meta interface{}) e
} else if v, ok := d.GetOkExists("user_env_vars"); !tpgresource.IsEmptyValue(reflect.ValueOf(userEnvVarsProp)) && (ok || !reflect.DeepEqual(v, userEnvVarsProp)) {
obj["userEnvVars"] = userEnvVarsProp
}
tagsProp, err := expandWorkflowsWorkflowTags(d.Get("tags"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("tags"); !tpgresource.IsEmptyValue(reflect.ValueOf(tagsProp)) && (ok || !reflect.DeepEqual(v, tagsProp)) {
obj["tags"] = tagsProp
}
labelsProp, err := expandWorkflowsWorkflowEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -737,6 +752,17 @@ func expandWorkflowsWorkflowUserEnvVars(v interface{}, d tpgresource.TerraformRe
return m, nil
}

func expandWorkflowsWorkflowTags(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 @@ -20,6 +20,7 @@ fields:
- field: 'service_account'
- field: 'source_contents'
- field: 'state'
- field: 'tags'
- field: 'terraform_labels'
provider_only: true
- field: 'update_time'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,86 @@ EOF
`, context)
}

func TestAccWorkflowsWorkflow_workflowTagsExample(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.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckWorkflowsWorkflowDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccWorkflowsWorkflow_workflowTagsExample(context),
},
},
})
}

func testAccWorkflowsWorkflow_workflowTagsExample(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_project" "project" {
}

resource "google_tags_tag_key" "tag_key" {
parent = "projects/${data.google_project.project.number}"
short_name = "tf_test_tag_key%{random_suffix}"
}

resource "google_tags_tag_value" "tag_value" {
parent = "tagKeys/${google_tags_tag_key.tag_key.name}"
short_name = "tf_test_tag_value%{random_suffix}"
}

resource "google_service_account" "test_account" {
account_id = "tf-test-my-account%{random_suffix}"
display_name = "Test Service Account"
}

resource "google_workflows_workflow" "example" {
name = "workflow%{random_suffix}"
region = "us-central1"
description = "Magic"
service_account = google_service_account.test_account.id
deletion_protection = false
tags = {
"${data.google_project.project.project_id}/${google_tags_tag_key.tag_key.short_name}" = "${google_tags_tag_value.tag_value.short_name}"
}
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: $${sys.get_env("url")}
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 testAccCheckWorkflowsWorkflowDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ resource "google_workflows_workflow" "example" {
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
}
deletion_protection = false
tags = {
env = "test"
}
source_contents = <<-EOF
# This is a sample workflow, feel free to replace it with your source code
#
Expand Down Expand Up @@ -88,6 +91,9 @@ resource "google_workflows_workflow" "example" {
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
}
deletion_protection = false
tags = {
env = "test"
}
source_contents = <<-EOF
# This is a sample workflow, feel free to replace it with your source code
#
Expand Down
72 changes: 72 additions & 0 deletions website/docs/r/workflows_workflow.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,72 @@ resource "google_workflows_workflow" "example" {
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.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=workflow_tags&open_in_editor=main.tf" 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 Tags


```hcl
data "google_project" "project" {
}

resource "google_tags_tag_key" "tag_key" {
parent = "projects/${data.google_project.project.number}"
short_name = "tag_key"
}

resource "google_tags_tag_value" "tag_value" {
parent = "tagKeys/${google_tags_tag_key.tag_key.name}"
short_name = "tag_value"
}

resource "google_service_account" "test_account" {
account_id = "my-account"
display_name = "Test Service Account"
}

resource "google_workflows_workflow" "example" {
name = "workflow"
region = "us-central1"
description = "Magic"
service_account = google_service_account.test_account.id
deletion_protection = false
tags = {
"${data.google_project.project.project_id}/${google_tags_tag_key.tag_key.short_name}" = "${google_tags_tag_value.tag_value.short_name}"
}
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: $${sys.get_env("url")}
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
}
```

## Argument Reference

Expand Down Expand Up @@ -140,6 +206,12 @@ The following arguments are supported:
(Optional)
User-defined environment variables associated with this workflow revision. This map has a maximum length of 20. Each string can take up to 4KiB. Keys cannot be empty strings and cannot start with “GOOGLE” or “WORKFLOWS".

* `tags` -
(Optional)
A map of resource manager tags. Resource manager tag keys and values have the same definition
as resource manager tags. Keys must be in the format tagKeys/{tag_key_id}, and values are in
the format tagValues/456. The field is ignored (both PUT & PATCH) when empty.

* `region` -
(Optional)
The region of the workflow.
Expand Down
Loading