Skip to content

Commit 0d8b3ed

Browse files
feat: Add Pub/Sub Topic support for changing schema settings (#8078) (#14819)
* Add support for Pub/Sub schema evolution * Add example for Pub/Sub schema evolution * Undo changes to Schema * Remove unneeded Topic changes. * Remove accidentally added newline * Added test for schema update Signed-off-by: Modular Magician <[email protected]>
1 parent cd672c9 commit 0d8b3ed

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

.changelog/8078.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
pubsub: Allowed `schema_settings` of `google_pubsub_topic` to change without deleting and recreating the resource
3+
```

google/resource_pubsub_topic.go

-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ and is not a valid configuration.`,
119119
"schema": {
120120
Type: schema.TypeString,
121121
Required: true,
122-
ForceNew: true,
123122
Description: `The name of the schema that messages published should be
124123
validated against. Format is projects/{project}/schemas/{schema}.
125124
The value of this field will be _deleted-schema_
@@ -128,7 +127,6 @@ if the schema has been deleted.`,
128127
"encoding": {
129128
Type: schema.TypeString,
130129
Optional: true,
131-
ForceNew: true,
132130
ValidateFunc: verify.ValidateEnum([]string{"ENCODING_UNSPECIFIED", "JSON", "BINARY", ""}),
133131
Description: `The encoding of messages validated against schema. Default value: "ENCODING_UNSPECIFIED" Possible values: ["ENCODING_UNSPECIFIED", "JSON", "BINARY"]`,
134132
Default: "ENCODING_UNSPECIFIED",

google/resource_pubsub_topic_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,40 @@ func TestAccPubsubTopic_cmek(t *testing.T) {
6969
})
7070
}
7171

72+
func TestAccPubsubTopic_schema(t *testing.T) {
73+
t.Parallel()
74+
75+
schema1 := fmt.Sprintf("tf-test-schema-%s", RandString(t, 10))
76+
schema2 := fmt.Sprintf("tf-test-schema-%s", RandString(t, 10))
77+
topic := fmt.Sprintf("tf-test-topic-%s", RandString(t, 10))
78+
79+
VcrTest(t, resource.TestCase{
80+
PreCheck: func() { acctest.AccTestPreCheck(t) },
81+
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
82+
CheckDestroy: testAccCheckPubsubTopicDestroyProducer(t),
83+
Steps: []resource.TestStep{
84+
{
85+
Config: testAccPubsubTopic_updateWithSchema(topic, schema1),
86+
},
87+
{
88+
ResourceName: "google_pubsub_topic.bar",
89+
ImportStateId: topic,
90+
ImportState: true,
91+
ImportStateVerify: true,
92+
},
93+
{
94+
Config: testAccPubsubTopic_updateWithNewSchema(topic, schema2),
95+
},
96+
{
97+
ResourceName: "google_pubsub_topic.bar",
98+
ImportStateId: topic,
99+
ImportState: true,
100+
ImportStateVerify: true,
101+
},
102+
},
103+
})
104+
}
105+
72106
func testAccPubsubTopic_update(topic, key, value string) string {
73107
return fmt.Sprintf(`
74108
resource "google_pubsub_topic" "foo" {
@@ -105,3 +139,39 @@ resource "google_pubsub_topic" "topic" {
105139
}
106140
`, topicName, kmsKey)
107141
}
142+
143+
func testAccPubsubTopic_updateWithSchema(topic, schema string) string {
144+
return fmt.Sprintf(`
145+
resource "google_pubsub_schema" "foo" {
146+
name = "%s"
147+
type = "PROTOCOL_BUFFER"
148+
definition = "syntax = \"proto3\";\nmessage Results {\nstring f1 = 1;\n}"
149+
}
150+
151+
resource "google_pubsub_topic" "bar" {
152+
name = "%s"
153+
schema_settings {
154+
schema = google_pubsub_schema.foo.id
155+
encoding = "BINARY"
156+
}
157+
}
158+
`, schema, topic)
159+
}
160+
161+
func testAccPubsubTopic_updateWithNewSchema(topic, schema string) string {
162+
return fmt.Sprintf(`
163+
resource "google_pubsub_schema" "foo" {
164+
name = "%s"
165+
type = "PROTOCOL_BUFFER"
166+
definition = "syntax = \"proto3\";\nmessage Results {\nstring f1 = 1;\n}"
167+
}
168+
169+
resource "google_pubsub_topic" "bar" {
170+
name = "%s"
171+
schema_settings {
172+
schema = google_pubsub_schema.foo.id
173+
encoding = "JSON"
174+
}
175+
}
176+
`, schema, topic)
177+
}

0 commit comments

Comments
 (0)