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): enable project autodetection and detect empty project #8168

Merged
merged 4 commits into from Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions pubsub/integration_test.go
Expand Up @@ -2060,3 +2060,25 @@ func TestIntegration_TopicUpdateSchema(t *testing.T) {
t.Fatalf("schema settings for update -want, +got: %v", diff)
}
}

func TestIntegration_DetectProjectID(t *testing.T) {
ctx := context.Background()
testCreds := testutil.Credentials(ctx)
if testCreds == nil {
t.Skip("test credentials not present, skipping")
}

goodClient, err := NewClient(ctx, DetectProjectID, option.WithCredentials(testCreds))
if err != nil {
t.Errorf("test pubsub.NewClient: %v", err)
}
if goodClient.Project() != testutil.ProjID() {
t.Errorf("client.Project() got %q, want %q", goodClient.Project(), testutil.ProjID())
}

badTS := testutil.ErroringTokenSource{}

if badClient, err := NewClient(ctx, DetectProjectID, option.WithTokenSource(badTS)); err == nil {
t.Errorf("expected error from bad token source, NewClient succeeded with project: %s", badClient.projectID)
}
}
32 changes: 32 additions & 0 deletions pubsub/pubsub.go
Expand Up @@ -16,13 +16,15 @@ package pubsub // import "cloud.google.com/go/pubsub"

import (
"context"
"errors"
"fmt"
"os"
"reflect"
"runtime"
"strings"
"time"

"cloud.google.com/go/internal/detect"
vkit "cloud.google.com/go/pubsub/apiv1"
"cloud.google.com/go/pubsub/internal"
gax "github.com/googleapis/gax-go/v2"
Expand Down Expand Up @@ -113,13 +115,30 @@ func mergeSubscriberCallOptions(a *vkit.SubscriberCallOptions, b *vkit.Subscribe
return res
}

// DetectProjectID is a sentinel value that instructs NewClient to detect the
// project ID. It is given in place of the projectID argument. NewClient will
// use the project ID from the given credentials or the default credentials
// (https://developers.google.com/accounts/docs/application-default-credentials)
// if no credentials were provided. When providing credentials, not all
// options will allow NewClient to extract the project ID. Specifically a JWT
// does not have the project ID encoded.
const DetectProjectID = "*detect-project-id*"

// ErrEmptyProjectID denotes that the project string passed into NewClient was empty.
// Please provide a valid project ID or use the DetectProjectID sentinel value to detect
// project ID from well defined sources.
var ErrEmptyProjectID = errors.New("pubsub: projectID string is empty")

// NewClient creates a new PubSub client. It uses a default configuration.
func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (c *Client, err error) {
return NewClientWithConfig(ctx, projectID, nil, opts...)
}

// NewClientWithConfig creates a new PubSub client.
func NewClientWithConfig(ctx context.Context, projectID string, config *ClientConfig, opts ...option.ClientOption) (c *Client, err error) {
if projectID == "" {
Copy link

Choose a reason for hiding this comment

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

@hongalex doesn't having this check early on break the opts.WithoutAuthentication case since it now requires you to pass a dummy projectID to instantiate the clients.

Copy link

Choose a reason for hiding this comment

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

Isn't this a breaking change that warrants a major version bump instead of a minor bump?

Copy link
Member Author

Choose a reason for hiding this comment

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

While this may be backwards incompatible, but falls under the realm of a bug fix and doesn't warrant a major version bump. Passing in an empty string to the client is unsupported behavior and should have resulted in an error, even when using unauthenticated. The project string is used to construct paths like "projects/my-project/topics/my-topic" and creating resources like "projects//topics/my-topic" is undefined behavior.

return nil, ErrEmptyProjectID
}
var o []option.ClientOption
// Environment variables for gcloud emulator:
// https://cloud.google.com/sdk/gcloud/reference/beta/emulators/pubsub/
Expand Down Expand Up @@ -157,13 +176,26 @@ func NewClientWithConfig(ctx context.Context, projectID string, config *ClientCo
subc.CallOptions = mergeSubscriberCallOptions(subc.CallOptions, config.SubscriberCallOptions)
}
pubc.SetGoogleClientInfo("gccl", internal.Version)

// Handle project autodetection.
projectID, err = detect.ProjectID(ctx, projectID, "", opts...)
if err != nil {
return nil, err
}

return &Client{
projectID: projectID,
pubc: pubc,
subc: subc,
}, nil
}

// Project returns the project ID or number for this instance of the client, which may have
// either been explicitly specified or autodetected.
func (c *Client) Project() string {
return c.projectID
}

// Close releases any resources held by the client,
// such as memory and goroutines.
//
Expand Down
8 changes: 8 additions & 0 deletions pubsub/pubsub_test.go
Expand Up @@ -117,3 +117,11 @@ func TestClient_ApplyClientConfig(t *testing.T) {
}
}
}

func TestClient_EmptyProjectID(t *testing.T) {
ctx := context.Background()
_, err := NewClient(ctx, "")
if err != ErrEmptyProjectID {
t.Fatalf("passing empty project ID got %v, want%v", err, ErrEmptyProjectID)
}
}