Skip to content

Commit 5c1ff34

Browse files
Add tags and workflow_tags fields to google_workflows_workflow resource. Fixes b/374990996 (#12085) (#21053)
[upstream:75a29e4c1ae8365e0d9e5930be1c6553fb810d3e] Signed-off-by: Modular Magician <[email protected]>
1 parent bb230c1 commit 5c1ff34

6 files changed

+188
-0
lines changed

.changelog/12085.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
workflows: added `tags` and `workflow_tags` fields to `google_workflows_workflow` resource
3+
```

google/services/workflows/resource_workflows_workflow.go

+26
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ Modifying this field for an existing workflow results in a new workflow revision
125125
Optional: true,
126126
Description: `Workflow code to be executed. The size limit is 128KB.`,
127127
},
128+
"tags": {
129+
Type: schema.TypeMap,
130+
Optional: true,
131+
ForceNew: true,
132+
Description: `A map of resource manager tags. Resource manager tag keys and values have the same definition
133+
as resource manager tags. Keys must be in the format tagKeys/{tag_key_id}, and values are in
134+
the format tagValues/456. The field is ignored (both PUT & PATCH) when empty.`,
135+
Elem: &schema.Schema{Type: schema.TypeString},
136+
},
128137
"user_env_vars": {
129138
Type: schema.TypeMap,
130139
Optional: true,
@@ -243,6 +252,12 @@ func resourceWorkflowsWorkflowCreate(d *schema.ResourceData, meta interface{}) e
243252
} else if v, ok := d.GetOkExists("user_env_vars"); !tpgresource.IsEmptyValue(reflect.ValueOf(userEnvVarsProp)) && (ok || !reflect.DeepEqual(v, userEnvVarsProp)) {
244253
obj["userEnvVars"] = userEnvVarsProp
245254
}
255+
tagsProp, err := expandWorkflowsWorkflowTags(d.Get("tags"), d, config)
256+
if err != nil {
257+
return err
258+
} else if v, ok := d.GetOkExists("tags"); !tpgresource.IsEmptyValue(reflect.ValueOf(tagsProp)) && (ok || !reflect.DeepEqual(v, tagsProp)) {
259+
obj["tags"] = tagsProp
260+
}
246261
labelsProp, err := expandWorkflowsWorkflowEffectiveLabels(d.Get("effective_labels"), d, config)
247262
if err != nil {
248263
return err
@@ -737,6 +752,17 @@ func expandWorkflowsWorkflowUserEnvVars(v interface{}, d tpgresource.TerraformRe
737752
return m, nil
738753
}
739754

755+
func expandWorkflowsWorkflowTags(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
756+
if v == nil {
757+
return map[string]string{}, nil
758+
}
759+
m := make(map[string]string)
760+
for k, val := range v.(map[string]interface{}) {
761+
m[k] = val.(string)
762+
}
763+
return m, nil
764+
}
765+
740766
func expandWorkflowsWorkflowEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
741767
if v == nil {
742768
return map[string]string{}, nil

google/services/workflows/resource_workflows_workflow_generated_meta.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fields:
2020
- field: 'service_account'
2121
- field: 'source_contents'
2222
- field: 'state'
23+
- field: 'tags'
2324
- field: 'terraform_labels'
2425
provider_only: true
2526
- field: 'update_time'

google/services/workflows/resource_workflows_workflow_generated_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,86 @@ EOF
101101
`, context)
102102
}
103103

104+
func TestAccWorkflowsWorkflow_workflowTagsExample(t *testing.T) {
105+
t.Parallel()
106+
107+
context := map[string]interface{}{
108+
"random_suffix": acctest.RandString(t, 10),
109+
}
110+
111+
acctest.VcrTest(t, resource.TestCase{
112+
PreCheck: func() { acctest.AccTestPreCheck(t) },
113+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
114+
CheckDestroy: testAccCheckWorkflowsWorkflowDestroyProducer(t),
115+
Steps: []resource.TestStep{
116+
{
117+
Config: testAccWorkflowsWorkflow_workflowTagsExample(context),
118+
},
119+
},
120+
})
121+
}
122+
123+
func testAccWorkflowsWorkflow_workflowTagsExample(context map[string]interface{}) string {
124+
return acctest.Nprintf(`
125+
data "google_project" "project" {
126+
}
127+
128+
resource "google_tags_tag_key" "tag_key" {
129+
parent = "projects/${data.google_project.project.number}"
130+
short_name = "tf_test_tag_key%{random_suffix}"
131+
}
132+
133+
resource "google_tags_tag_value" "tag_value" {
134+
parent = "tagKeys/${google_tags_tag_key.tag_key.name}"
135+
short_name = "tf_test_tag_value%{random_suffix}"
136+
}
137+
138+
resource "google_service_account" "test_account" {
139+
account_id = "tf-test-my-account%{random_suffix}"
140+
display_name = "Test Service Account"
141+
}
142+
143+
resource "google_workflows_workflow" "example" {
144+
name = "workflow%{random_suffix}"
145+
region = "us-central1"
146+
description = "Magic"
147+
service_account = google_service_account.test_account.id
148+
deletion_protection = false
149+
tags = {
150+
"${data.google_project.project.project_id}/${google_tags_tag_key.tag_key.short_name}" = "${google_tags_tag_value.tag_value.short_name}"
151+
}
152+
source_contents = <<-EOF
153+
# This is a sample workflow. You can replace it with your source code.
154+
#
155+
# This workflow does the following:
156+
# - reads current time and date information from an external API and stores
157+
# the response in currentTime variable
158+
# - retrieves a list of Wikipedia articles related to the day of the week
159+
# from currentTime
160+
# - returns the list of articles as an output of the workflow
161+
#
162+
# Note: In Terraform you need to escape the $$ or it will cause errors.
163+
164+
- getCurrentTime:
165+
call: http.get
166+
args:
167+
url: $${sys.get_env("url")}
168+
result: currentTime
169+
- readWikipedia:
170+
call: http.get
171+
args:
172+
url: https://en.wikipedia.org/w/api.php
173+
query:
174+
action: opensearch
175+
search: $${currentTime.body.dayOfWeek}
176+
result: wikiResult
177+
- returnOutput:
178+
return: $${wikiResult.body[1]}
179+
EOF
180+
}
181+
`, context)
182+
}
183+
104184
func testAccCheckWorkflowsWorkflowDestroyProducer(t *testing.T) func(s *terraform.State) error {
105185
return func(s *terraform.State) error {
106186
for name, rs := range s.RootModule().Resources {

google/services/workflows/resource_workflows_workflow_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ resource "google_workflows_workflow" "example" {
4646
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
4747
}
4848
deletion_protection = false
49+
tags = {
50+
env = "test"
51+
}
4952
source_contents = <<-EOF
5053
# This is a sample workflow, feel free to replace it with your source code
5154
#
@@ -88,6 +91,9 @@ resource "google_workflows_workflow" "example" {
8891
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
8992
}
9093
deletion_protection = false
94+
tags = {
95+
env = "test"
96+
}
9197
source_contents = <<-EOF
9298
# This is a sample workflow, feel free to replace it with your source code
9399
#

website/docs/r/workflows_workflow.html.markdown

+72
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,72 @@ resource "google_workflows_workflow" "example" {
8585
EOF
8686
}
8787
```
88+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
89+
<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">
90+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
91+
</a>
92+
</div>
93+
## Example Usage - Workflow Tags
94+
95+
96+
```hcl
97+
data "google_project" "project" {
98+
}
99+
100+
resource "google_tags_tag_key" "tag_key" {
101+
parent = "projects/${data.google_project.project.number}"
102+
short_name = "tag_key"
103+
}
104+
105+
resource "google_tags_tag_value" "tag_value" {
106+
parent = "tagKeys/${google_tags_tag_key.tag_key.name}"
107+
short_name = "tag_value"
108+
}
109+
110+
resource "google_service_account" "test_account" {
111+
account_id = "my-account"
112+
display_name = "Test Service Account"
113+
}
114+
115+
resource "google_workflows_workflow" "example" {
116+
name = "workflow"
117+
region = "us-central1"
118+
description = "Magic"
119+
service_account = google_service_account.test_account.id
120+
deletion_protection = false
121+
tags = {
122+
"${data.google_project.project.project_id}/${google_tags_tag_key.tag_key.short_name}" = "${google_tags_tag_value.tag_value.short_name}"
123+
}
124+
source_contents = <<-EOF
125+
# This is a sample workflow. You can replace it with your source code.
126+
#
127+
# This workflow does the following:
128+
# - reads current time and date information from an external API and stores
129+
# the response in currentTime variable
130+
# - retrieves a list of Wikipedia articles related to the day of the week
131+
# from currentTime
132+
# - returns the list of articles as an output of the workflow
133+
#
134+
# Note: In Terraform you need to escape the $$ or it will cause errors.
135+
136+
- getCurrentTime:
137+
call: http.get
138+
args:
139+
url: $${sys.get_env("url")}
140+
result: currentTime
141+
- readWikipedia:
142+
call: http.get
143+
args:
144+
url: https://en.wikipedia.org/w/api.php
145+
query:
146+
action: opensearch
147+
search: $${currentTime.body.dayOfWeek}
148+
result: wikiResult
149+
- returnOutput:
150+
return: $${wikiResult.body[1]}
151+
EOF
152+
}
153+
```
88154

89155
## Argument Reference
90156

@@ -140,6 +206,12 @@ The following arguments are supported:
140206
(Optional)
141207
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".
142208

209+
* `tags` -
210+
(Optional)
211+
A map of resource manager tags. Resource manager tag keys and values have the same definition
212+
as resource manager tags. Keys must be in the format tagKeys/{tag_key_id}, and values are in
213+
the format tagValues/456. The field is ignored (both PUT & PATCH) when empty.
214+
143215
* `region` -
144216
(Optional)
145217
The region of the workflow.

0 commit comments

Comments
 (0)