Skip to content

Commit 4c9676f

Browse files
authored
chore(pubsub): Add samples for topic and subscription SMTs (#32791)
1 parent 56f30b3 commit 4c9676f

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

google-cloud-pubsub/samples/acceptance/subscriptions_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
require_relative "../pubsub_test_subscription_permissions.rb"
3434
require_relative "../pubsub_detach_subscription.rb"
3535
require_relative "../pubsub_delete_subscription.rb"
36+
require_relative "../pubsub_create_subscription_with_smt.rb"
3637
require "google/cloud/bigquery"
3738

3839
describe "subscriptions" do
@@ -266,6 +267,18 @@
266267
end
267268
end
268269

270+
it "supports creating subscription with SMT" do
271+
project_id = pubsub.project
272+
topic_id = @topic.name
273+
subscription_id = random_subscription_id
274+
@created_subscriptions << subscription_id
275+
276+
assert_output "Created subscription with SMT: projects/#{pubsub.project}/subscriptions/#{subscription_id}\n" do
277+
create_subscription_with_smt topic_id: topic_id,
278+
subscription_id: subscription_id
279+
end
280+
end
281+
269282
it "supports creating bigquery subscription" do
270283
project_id = pubsub.project
271284
topic_id = @topic.name

google-cloud-pubsub/samples/acceptance/topics_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
require_relative "../pubsub_test_topic_permissions.rb"
4646
require_relative "../pubsub_update_topic_type.rb"
4747
require_relative "../pubsub_create_topic_with_schema_revisions.rb"
48+
require_relative "../pubsub_create_topic_with_smt.rb"
4849

4950
describe "emulator" do
5051
let(:pubsub) { Google::Cloud::Pubsub.new }
@@ -141,6 +142,13 @@
141142
end
142143
end
143144

145+
it "supports pubsub_create_topic_with_smt" do
146+
# pubsub_create_topic_with_smt
147+
assert_output "Created topic with SMT: projects/#{pubsub.project}/topics/#{topic_id}\n" do
148+
create_topic_with_smt topic_id: topic_id
149+
end
150+
end
151+
144152
it "supports pubsub_create_topic, pubsub_create_cloud_storage_subscription" do
145153
# pubsub_create_topic
146154
assert_output "Topic projects/#{pubsub.project}/topics/#{topic_id} created.\n" do
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "google/cloud/pubsub"
16+
17+
def create_subscription_with_smt topic_id:, subscription_id:
18+
# [START pubsub_create_subscription_with_smt]
19+
# topic_id = "your-topic-id"
20+
# subscription_id = "your-subscription-id"
21+
22+
pubsub = Google::Cloud::PubSub.new
23+
subscription_admin = pubsub.subscription_admin
24+
25+
# UDF that removes the 'ssn' field, if present
26+
code = "function redactSSN(message, metadata) {\n" \
27+
" const data = JSON.parse(message.data);\n" \
28+
" delete data['ssn'];\n" \
29+
" message.data = JSON.stringify(data);\n" \
30+
" return message;\n" \
31+
"}"
32+
33+
subscription = subscription_admin.create_subscription(
34+
name: pubsub.subscription_path(subscription_id),
35+
topic: pubsub.topic_path(topic_id),
36+
message_transforms: [
37+
{
38+
javascript_udf: {
39+
function_name: "redactSSN",
40+
code: code
41+
}
42+
}
43+
]
44+
)
45+
46+
puts "Created subscription with SMT: #{subscription.name}"
47+
# [END pubsub_create_subscription_with_smt]
48+
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "google/cloud/pubsub"
16+
17+
def create_topic_with_smt topic_id:
18+
# [START pubsub_create_topic_with_smt]
19+
# topic_id = "your-topic-id"
20+
21+
pubsub = Google::Cloud::PubSub.new
22+
topic_admin = pubsub.topic_admin
23+
24+
# UDF that removes the 'ssn' field, if present
25+
code = "function redactSSN(message, metadata) {\n" \
26+
" const data = JSON.parse(message.data);\n" \
27+
" delete data['ssn'];\n" \
28+
" message.data = JSON.stringify(data);\n" \
29+
" return message;\n" \
30+
"}"
31+
32+
topic = topic_admin.create_topic(
33+
name: pubsub.topic_path(topic_id),
34+
message_transforms: [
35+
{
36+
javascript_udf: {
37+
function_name: "redactSSN",
38+
code: code
39+
}
40+
}
41+
]
42+
)
43+
44+
puts "Created topic with SMT: #{topic.name}"
45+
# [END pubsub_create_topic_with_smt]
46+
end

0 commit comments

Comments
 (0)