-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(pubsub/v2): add single message transform samples #5352
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
Changes from 5 commits
9007519
981723a
6524e38
2eb2dd1
5697683
7a67e80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package subscriptions | ||
|
||
// [START pubsub_create_subscription_with_smt] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/pubsub/v2" | ||
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" | ||
) | ||
|
||
// createSubscriptionWithSMT creates a subscription with a single message transform function applied. | ||
func createSubscriptionWithSMT(w io.Writer, projectID, topicID, subID string) error { | ||
// projectID := "my-project-id" | ||
// topicID := "my-topic" | ||
// subID := "my-sub" | ||
ctx := context.Background() | ||
client, err := pubsub.NewClient(ctx, projectID) | ||
if err != nil { | ||
return fmt.Errorf("pubsub.NewClient: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
code := `function redactSSN(message, metadata) { | ||
const data = JSON.parse(message.data); | ||
delete data['ssn']; | ||
message.data = JSON.stringify(data); | ||
return message; | ||
}` | ||
hongalex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
transform := &pubsubpb.MessageTransform{ | ||
Transform: &pubsubpb.MessageTransform_JavascriptUdf{ | ||
JavascriptUdf: &pubsubpb.JavaScriptUDF{ | ||
FunctionName: "redactSSN", | ||
Code: code, | ||
}, | ||
}, | ||
} | ||
|
||
sub := &pubsubpb.Subscription{ | ||
Name: fmt.Sprintf("projects/%s/subscriptions/%s", projectID, subID), | ||
Topic: fmt.Sprintf("projects/%s/topics/%s", projectID, topicID), | ||
MessageTransforms: []*pubsubpb.MessageTransform{transform}, | ||
} | ||
sub, err = client.SubscriptionAdminClient.CreateSubscription(ctx, sub) | ||
if err != nil { | ||
return fmt.Errorf("CreateSubscription: %w", err) | ||
} | ||
fmt.Fprintf(w, "Created subscription with message transform: %v\n", sub) | ||
return nil | ||
} | ||
|
||
// [END pubsub_create_subscription_with_smt] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,8 @@ import ( | |
trace "cloud.google.com/go/trace/apiv1" | ||
"cloud.google.com/go/trace/apiv1/tracepb" | ||
"google.golang.org/api/iterator" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil" | ||
) | ||
|
@@ -731,3 +733,34 @@ func createOrGetStorageBucket(projectID, bucketID string) error { | |
|
||
return nil | ||
} | ||
|
||
func TestCreateSubscriptionWithSMT(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If (and only if) you decide to pass a Topic struct to createSubscriptionWithSMT, it would probably make sense to update the test to the same format I used in #5381. (Avoids a race condition when setting the topic variable.) Not needed if createSubscriptionWithSMT stays as is. |
||
ctx := context.Background() | ||
tc := testutil.SystemTest(t) | ||
client := setup(t) | ||
|
||
smtSubID := subID + "-smt" | ||
smtTopicID := topicID + "-smt" | ||
testutil.Retry(t, 10, time.Second, func(r *testutil.R) { | ||
_, err := client.TopicAdminClient.CreateTopic(ctx, &pb.Topic{ | ||
Name: fmt.Sprintf("projects/%s/topics/%s", tc.ProjectID, smtTopicID), | ||
}) | ||
if err != nil { | ||
if st, ok := status.FromError(err); !ok || st.Code() != codes.AlreadyExists { | ||
r.Errorf("CreateTopic: %v", err) | ||
} | ||
} | ||
}) | ||
|
||
testutil.Retry(t, 10, time.Second, func(r *testutil.R) { | ||
buf := new(bytes.Buffer) | ||
if err := createSubscriptionWithSMT(buf, tc.ProjectID, smtTopicID, smtSubID); err != nil { | ||
r.Errorf("failed to create subscription with SMT: %v", err) | ||
} | ||
got := buf.String() | ||
want := "Created subscription with message transform" | ||
if !strings.Contains(got, want) { | ||
r.Errorf("got: %s, want: %v", got, want) | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package topics | ||
|
||
// [START pubsub_create_topic_with_smt] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/pubsub/v2" | ||
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" | ||
) | ||
|
||
// createTopicWithSMT creates a topic with a single message transform function applied. | ||
func createTopicWithSMT(w io.Writer, projectID, topicID string) error { | ||
// projectID := "my-project-id" | ||
// topicID := "my-topic" | ||
ctx := context.Background() | ||
client, err := pubsub.NewClient(ctx, projectID) | ||
if err != nil { | ||
return fmt.Errorf("pubsub.NewClient: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
code := `function redactSSN(message, metadata) { | ||
const data = JSON.parse(message.data); | ||
delete data['ssn']; | ||
message.data = JSON.stringify(data); | ||
return message; | ||
}` | ||
hongalex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
transform := &pubsubpb.MessageTransform{ | ||
Transform: &pubsubpb.MessageTransform_JavascriptUdf{ | ||
JavascriptUdf: &pubsubpb.JavaScriptUDF{ | ||
FunctionName: "redactSSN", | ||
Code: code, | ||
}, | ||
}, | ||
} | ||
|
||
topic := &pubsubpb.Topic{ | ||
Name: fmt.Sprintf("projects/%s/topics/%s", projectID, topicID), | ||
MessageTransforms: []*pubsubpb.MessageTransform{transform}, | ||
} | ||
|
||
topic, err = client.TopicAdminClient.CreateTopic(ctx, topic) | ||
if err != nil { | ||
return fmt.Errorf("CreateTopic: %w", err) | ||
} | ||
|
||
fmt.Fprintf(w, "Created topic with message transform: %v\n", topic) | ||
return nil | ||
} | ||
|
||
// [END pubsub_create_topic_with_smt] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example function signature is different than the V1 version. Is that by design?