Skip to content

Commit 5df9ee0

Browse files
authored
Add tags field to Pubsub Topic and Subscription (#15174)
1 parent 86a14c3 commit 5df9ee0

File tree

6 files changed

+363
-190
lines changed

6 files changed

+363
-190
lines changed

mmv1/products/pubsub/Subscription.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ examples:
131131
topic_name: 'example-topic'
132132
subscription_name: 'example-subscription'
133133
tgc_skip_test: 'The dynamic block is in test configuration. The test takes time to fix.'
134+
- name: 'pubsub_subscription_tags'
135+
primary_resource_id: 'example'
136+
vars:
137+
topic_name: 'example-topic'
138+
subscription_name: 'example-subscription'
139+
tag_key: 'tag_key'
140+
tag_value: 'tag_value'
134141
parameters:
135142
properties:
136143
- name: 'name'
@@ -583,3 +590,16 @@ properties:
583590
description: |
584591
Controls whether or not to use this transform. If not set or `false`,
585592
the transform will be applied to messages. Default: `true`.
593+
- name: 'tags'
594+
type: KeyValuePairs
595+
description: |
596+
Input only. Resource manager tags to be bound to the subscription. Tag
597+
keys and values have the same definition as resource manager tags. Keys
598+
must be in the format tagKeys/{tag_key_id}, and values are in the format
599+
tagValues/456. The field is ignored when empty. The field is immutable and
600+
causes resource replacement when mutated. This field is only set at create
601+
time and modifying this field after creation will trigger recreation. To
602+
apply tags to an existing resource, see the `google_tags_tag_value`
603+
resource.
604+
immutable: true
605+
ignore_read: true

mmv1/products/pubsub/Topic.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ examples:
109109
vars:
110110
topic_name: 'example-topic'
111111
tgc_skip_test: 'The dynamic block is in test configuration. The test takes time to fix.'
112+
- name: 'pubsub_topic_tags'
113+
primary_resource_id: 'example'
114+
vars:
115+
topic_name: 'example-topic'
116+
tag_key: 'tag_key'
117+
tag_value: 'tag_value'
112118
parameters:
113119
properties:
114120
- name: 'name'
@@ -508,3 +514,16 @@ properties:
508514
description: |
509515
Controls whether or not to use this transform. If not set or `false`,
510516
the transform will be applied to messages. Default: `true`.
517+
- name: 'tags'
518+
type: KeyValuePairs
519+
description: |
520+
Input only. Resource manager tags to be bound to the topic. Tag keys and
521+
values have the same definition as resource manager tags. Keys must be in
522+
the format tagKeys/{tag_key_id}, and values are in the format
523+
tagValues/456. The field is ignored when empty. The field is immutable and
524+
causes resource replacement when mutated. This field is only set at create
525+
time and modifying this field after creation will trigger recreation. To
526+
apply tags to an existing resource, see the `google_tags_tag_value`
527+
resource.
528+
immutable: true
529+
ignore_read: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "google_pubsub_topic" "{{$.PrimaryResourceId}}" {
2+
name = "{{index $.Vars "topic_name"}}"
3+
}
4+
5+
resource "google_pubsub_subscription" "{{$.PrimaryResourceId}}" {
6+
name = "{{index $.Vars "subscription_name"}}"
7+
topic = google_pubsub_topic.{{$.PrimaryResourceId}}.id
8+
tags = {
9+
(google_tags_tag_key.tag_key.namespaced_name) = google_tags_tag_value.tag_value.short_name
10+
}
11+
}
12+
13+
data "google_project" "project" {}
14+
15+
resource "google_tags_tag_key" "tag_key" {
16+
parent = data.google_project.project.id
17+
short_name = "{{index $.Vars "tag_key"}}"
18+
}
19+
20+
resource "google_tags_tag_value" "tag_value" {
21+
parent = google_tags_tag_key.tag_key.id
22+
short_name = "{{index $.Vars "tag_value"}}"
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resource "google_pubsub_topic" "{{$.PrimaryResourceId}}" {
2+
name = "{{index $.Vars "topic_name"}}"
3+
tags = {
4+
(google_tags_tag_key.tag_key.namespaced_name) = google_tags_tag_value.tag_value.short_name
5+
}
6+
}
7+
8+
data "google_project" "project" {}
9+
10+
resource "google_tags_tag_key" "tag_key" {
11+
parent = data.google_project.project.id
12+
short_name = "{{index $.Vars "tag_key"}}"
13+
}
14+
15+
resource "google_tags_tag_value" "tag_value" {
16+
parent = google_tags_tag_key.tag_key.id
17+
short_name = "{{index $.Vars "tag_value"}}"
18+
}

mmv1/third_party/terraform/services/pubsub/resource_pubsub_subscription_test.go

Lines changed: 110 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -583,43 +583,64 @@ func TestAccPubsubSubscription_javascriptUdfUpdate(t *testing.T) {
583583
})
584584
}
585585

586-
func testAccPubsubSubscription_javascriptUdfSettings(topic, subscription, functionName, code string) string {
587-
return fmt.Sprintf(`
588-
resource "google_pubsub_topic" "foo" {
589-
name = "%s"
590-
}
586+
func TestGetComputedTopicName(t *testing.T) {
587+
type testData struct {
588+
project string
589+
topic string
590+
expected string
591+
}
591592

592-
resource "google_pubsub_subscription" "foo" {
593-
name = "%s"
594-
topic = google_pubsub_topic.foo.id
595-
message_transforms {
596-
disabled = true
597-
javascript_udf {
598-
function_name = "%s"
599-
code = "%s"
600-
}
601-
}
602-
}
603-
`, topic, subscription, functionName, code)
604-
}
593+
var testCases = []testData{
594+
{
595+
project: "my-project",
596+
topic: "my-topic",
597+
expected: "projects/my-project/topics/my-topic",
598+
},
599+
{
600+
project: "my-project",
601+
topic: "projects/another-project/topics/my-topic",
602+
expected: "projects/another-project/topics/my-topic",
603+
},
604+
}
605605

606-
func testAccPubsubSubscription_javascriptUdfSettings_noEnabled(topic, subscription, functionName, code string) string {
607-
return fmt.Sprintf(`
608-
resource "google_pubsub_topic" "foo" {
609-
name = "%s"
606+
for _, testCase := range testCases {
607+
computedTopicName := pubsub.GetComputedTopicName(testCase.project, testCase.topic)
608+
if computedTopicName != testCase.expected {
609+
t.Fatalf("bad computed topic name: %s' => expected %s", computedTopicName, testCase.expected)
610+
}
611+
}
610612
}
611613

612-
resource "google_pubsub_subscription" "foo" {
613-
name = "%s"
614-
topic = google_pubsub_topic.foo.id
615-
message_transforms {
616-
javascript_udf {
617-
function_name = "%s"
618-
code = "%s"
619-
}
620-
}
621-
}
622-
`, topic, subscription, functionName, code)
614+
func TestAccPubsubSubscription_tags(t *testing.T) {
615+
t.Parallel()
616+
617+
subscription := fmt.Sprintf("tf-test-sub-%s", acctest.RandString(t, 10))
618+
tagKey := acctest.BootstrapSharedTestOrganizationTagKey(t, "pubsub-subscription-tagkey", nil)
619+
context := map[string]interface{}{
620+
"topic": fmt.Sprintf("tf-test-topic-%s", acctest.RandString(t, 10)),
621+
"subscription": subscription,
622+
"org": envvar.GetTestOrgFromEnv(t),
623+
"tagKey": tagKey,
624+
"tagValue": acctest.BootstrapSharedTestOrganizationTagValue(t, "pubsub-subscription-tagvalue", tagKey),
625+
}
626+
627+
acctest.VcrTest(t, resource.TestCase{
628+
PreCheck: func() { acctest.AccTestPreCheck(t) },
629+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
630+
CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t),
631+
Steps: []resource.TestStep{
632+
{
633+
Config: testAccPubsubSubscription_tags(context),
634+
},
635+
{
636+
ResourceName: "google_pubsub_subscription.foo",
637+
ImportStateId: subscription,
638+
ImportState: true,
639+
ImportStateVerify: true,
640+
ImportStateVerifyIgnore: []string{"tags"},
641+
},
642+
},
643+
})
623644
}
624645

625646
func testAccPubsubSubscription_emptyTTL(topic, subscription string) string {
@@ -1000,34 +1021,6 @@ resource "google_pubsub_subscription" "foo" {
10001021
`, topic, subscription)
10011022
}
10021023

1003-
func TestGetComputedTopicName(t *testing.T) {
1004-
type testData struct {
1005-
project string
1006-
topic string
1007-
expected string
1008-
}
1009-
1010-
var testCases = []testData{
1011-
{
1012-
project: "my-project",
1013-
topic: "my-topic",
1014-
expected: "projects/my-project/topics/my-topic",
1015-
},
1016-
{
1017-
project: "my-project",
1018-
topic: "projects/another-project/topics/my-topic",
1019-
expected: "projects/another-project/topics/my-topic",
1020-
},
1021-
}
1022-
1023-
for _, testCase := range testCases {
1024-
computedTopicName := pubsub.GetComputedTopicName(testCase.project, testCase.topic)
1025-
if computedTopicName != testCase.expected {
1026-
t.Fatalf("bad computed topic name: %s' => expected %s", computedTopicName, testCase.expected)
1027-
}
1028-
}
1029-
}
1030-
10311024
func testAccCheckPubsubSubscriptionCache404(t *testing.T, subName string) resource.TestCheckFunc {
10321025
return func(s *terraform.State) error {
10331026
config := acctest.GoogleProviderConfig(t)
@@ -1061,3 +1054,59 @@ resource "google_pubsub_subscription" "foo" {
10611054
}
10621055
`, topic, subscription, filter)
10631056
}
1057+
1058+
func testAccPubsubSubscription_javascriptUdfSettings(topic, subscription, functionName, code string) string {
1059+
return fmt.Sprintf(`
1060+
resource "google_pubsub_topic" "foo" {
1061+
name = "%s"
1062+
}
1063+
1064+
resource "google_pubsub_subscription" "foo" {
1065+
name = "%s"
1066+
topic = google_pubsub_topic.foo.id
1067+
message_transforms {
1068+
disabled = true
1069+
javascript_udf {
1070+
function_name = "%s"
1071+
code = "%s"
1072+
}
1073+
}
1074+
}
1075+
`, topic, subscription, functionName, code)
1076+
}
1077+
1078+
func testAccPubsubSubscription_javascriptUdfSettings_noEnabled(topic, subscription, functionName, code string) string {
1079+
return fmt.Sprintf(`
1080+
resource "google_pubsub_topic" "foo" {
1081+
name = "%s"
1082+
}
1083+
1084+
resource "google_pubsub_subscription" "foo" {
1085+
name = "%s"
1086+
topic = google_pubsub_topic.foo.id
1087+
message_transforms {
1088+
javascript_udf {
1089+
function_name = "%s"
1090+
code = "%s"
1091+
}
1092+
}
1093+
}
1094+
`, topic, subscription, functionName, code)
1095+
}
1096+
1097+
func testAccPubsubSubscription_tags(context map[string]interface{}) string {
1098+
return acctest.Nprintf(`
1099+
resource "google_pubsub_topic" "foo" {
1100+
name = "%{topic}"
1101+
}
1102+
1103+
resource "google_pubsub_subscription" "foo" {
1104+
name = "%{subscription}"
1105+
topic = google_pubsub_topic.foo.id
1106+
1107+
tags = {
1108+
"%{org}/%{tagKey}" = "%{tagValue}"
1109+
}
1110+
}
1111+
`, context)
1112+
}

0 commit comments

Comments
 (0)