Skip to content
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

feat(pubsub): add v2 topic and publishing samples #5107

Draft
wants to merge 2 commits into
base: pubsub-v2-samples-trunk
Choose a base branch
from

Conversation

hongalex
Copy link
Contributor

Go Pub/Sub is moving to a version 2 of the library. In a previous PR, we moved the existing v1 samples to a new directory. This is an edit in place of the existing samples to work with the v2.

This PR primarily focuses on topic admin operations and publishing operations. Subscriptions and schemas will come in separate PRs.

The tests currently can't be run since there is an error with duplicate protos being registered in the same space:

panic: proto: file "google/pubsub/v1/schema.proto" is already registered
        previously from: "cloud.google.com/go/pubsub/apiv1/pubsubpb"
        currently from:  "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"

We'll merge these into a trunk branch until a later point when tests can be properly run.

@product-auto-label product-auto-label bot added api: pubsub Issues related to the Pub/Sub API. samples Issues that are directly related to samples. labels Jan 24, 2025
Copy link

@code-review-assist code-review-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hongalex and reviewers,

I'm currently reviewing this pull request and will post my feedback in a few minutes. In the meantime, here's a summary of the changes for you and other reviewers to quickly get up to speed:

This pull request, feat(pubsub): add v2 topic and publishing samples, aims to update the existing Google Cloud Pub/Sub samples to use the v2 version of the library. The author mentions that a previous PR moved the v1 samples, and this PR updates them in place. The description notes that subscription and schema handling will be addressed in separate PRs.

Here's a breakdown of the changes:

  • pubsub/go.mod (lines 6-22, 48-66): Updated dependencies to include cloud.google.com/go/pubsub/v2 v2.0.0-00010101000000-000000000000 and other related libraries to their latest versions. A replace directive was added to point to a local copy of the v2 Pub/Sub library.
  • pubsub/go.sum (lines 2-32, 95-226): Updated checksums for the updated dependencies.
  • pubsub/topics/add_users.go (lines 18-67): Refactored the addUsers function to use the v2 Pub/Sub and IAM libraries. The IAM interaction is now explicitly using GetIamPolicy and SetIamPolicy calls, rather than the higher-level methods previously used.
  • pubsub/topics/create.go (lines 20-43): Updated the create function to use the v2 Pub/Sub library and the new CreateTopic method.
  • pubsub/topics/create_topic_gcs_ingestion.go (lines 21-81): Updated the createTopicWithCloudStorageIngestion function to use the v2 Pub/Sub library and the new CreateTopic method. The minimumObjectCreateTime parameter is now a timestamppb.Timestamp type, and a delimiter parameter was added.
  • pubsub/topics/create_topic_kinesis_ingestion.go (lines 20-61): Updated the createTopicWithKinesisIngestion function to use the v2 Pub/Sub library and the new CreateTopic method.
  • pubsub/topics/delete.go (lines 20-47): Updated the delete function to use the v2 Pub/Sub library and the new DeleteTopic method.
  • pubsub/topics/list_subs.go (lines 18-55): Updated the listSubscriptions function to use the v2 Pub/Sub library and the new ListTopicSubscriptions method.
  • pubsub/topics/list_topics.go (lines 18-55): Updated the listTopics function to use the v2 Pub/Sub library and the new ListTopics method.
  • pubsub/topics/policy.go (lines 20-52): Updated the getIAMPolicy function to use the v2 Pub/Sub library and the new GetIamPolicy method. The output formatting was also changed.
  • pubsub/topics/publish.go (lines 20-46): Updated the publish function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/publish_custom.go (lines 20-46): Updated the publishCustomAttributes function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/publish_flow_control.go (lines 23-63): Updated the publishWithFlowControlSettings function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/publish_ordering.go (lines 22-77): Updated the publishWithOrderingKey function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/publish_otel_tracing.go (lines 20-86): Updated the publishOpenTelemetryTracing function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/publish_resume_ordering.go (lines 20-63): Updated the resumePublishWithOrderingKey function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/publish_retry.go (lines 21-70): Updated the publishWithRetrySettings function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes. The PublisherCallOptions type was changed to TopicAdminCallOptions.
  • pubsub/topics/publish_scale.go (lines 23-50): Updated the publishThatScales function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/publish_settings.go (lines 22-57): Updated the publishWithSettings function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/publish_single.go (lines 20-46): Updated the publishSingleGoroutine function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/publish_with_compression.go (lines 20-48): Updated the publishWithCompression function to use the v2 Pub/Sub library and explicitly create a publisher. The publisher is reused across publishes.
  • pubsub/topics/test_permissions.go (lines 20-53): Updated the testPermissions function to use the v2 Pub/Sub library and the new TestIamPermissions method.
  • pubsub/topics/topics_test.go (lines 20-320): Updated several test functions to use the v2 Pub/Sub library and the new methods. The list function is now listTopics, and the policy function is now getIAMPolicy.
  • pubsub/topics/update_topic_type.go (lines 20-63): Updated the updateTopicType function to use the v2 Pub/Sub library and the new UpdateTopic method. The updateCfg parameter is now updateReq which includes a fieldmaskpb.FieldMask.

The tests currently fail due to duplicate proto registration. The author plans to merge this into trunk and address the test failures later.

Happy reviewing!

Code flows like a stream,
Version two, a smoother ride,
Tests will fix the bugs.

Copy link

@code-review-assist code-review-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request updates the Pub/Sub samples to use the v2 library. The changes are necessary and well-implemented. However, there are a few minor suggestions to improve code readability and maintainability. I have referenced the Google Go Style Guide when making these suggestions.

)

func addUsers(projectID, topicID string) error {
func addUsers(w io.Writer, projectID, topicID string) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Per the Google Go style guide, function names should be descriptive and in camelCase. Consider renaming this function to addUsersToTopic.

Suggested change
func addUsers(w io.Writer, projectID, topicID string) error {
func addUsersToTopic(w io.Writer, projectID, topicID string) error {

"google.golang.org/api/iterator"
)

func listSubscriptions(projectID, topicID string) ([]*pubsub.Subscription, error) {
func listSubscriptions(w io.Writer, projectID, topicID string) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Per the Google Go style guide, function names should be descriptive and in camelCase. Consider renaming this function to listTopicSubscriptions.

Suggested change
func listSubscriptions(w io.Writer, projectID, topicID string) error {
func listTopicSubscriptions(w io.Writer, projectID, topicID string) error {

)

func policy(w io.Writer, projectID, topicID string) (*iam.Policy, error) {
func getIAMPolicy(w io.Writer, projectID, topicID string) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Per the Google Go style guide, function names should be descriptive and in camelCase. Consider renaming this function to getTopicIAMPolicy.

Suggested change
func getIAMPolicy(w io.Writer, projectID, topicID string) error {
func getTopicIAMPolicy(w io.Writer, projectID, topicID string) error {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the Pub/Sub API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant