@@ -16,13 +16,15 @@ package pubsub // import "cloud.google.com/go/pubsub"
16
16
17
17
import (
18
18
"context"
19
+ "errors"
19
20
"fmt"
20
21
"os"
21
22
"reflect"
22
23
"runtime"
23
24
"strings"
24
25
"time"
25
26
27
+ "cloud.google.com/go/internal/detect"
26
28
vkit "cloud.google.com/go/pubsub/apiv1"
27
29
"cloud.google.com/go/pubsub/internal"
28
30
gax "github.com/googleapis/gax-go/v2"
@@ -113,13 +115,30 @@ func mergeSubscriberCallOptions(a *vkit.SubscriberCallOptions, b *vkit.Subscribe
113
115
return res
114
116
}
115
117
118
+ // DetectProjectID is a sentinel value that instructs NewClient to detect the
119
+ // project ID. It is given in place of the projectID argument. NewClient will
120
+ // use the project ID from the given credentials or the default credentials
121
+ // (https://developers.google.com/accounts/docs/application-default-credentials)
122
+ // if no credentials were provided. When providing credentials, not all
123
+ // options will allow NewClient to extract the project ID. Specifically a JWT
124
+ // does not have the project ID encoded.
125
+ const DetectProjectID = "*detect-project-id*"
126
+
127
+ // ErrEmptyProjectID denotes that the project string passed into NewClient was empty.
128
+ // Please provide a valid project ID or use the DetectProjectID sentinel value to detect
129
+ // project ID from well defined sources.
130
+ var ErrEmptyProjectID = errors .New ("pubsub: projectID string is empty" )
131
+
116
132
// NewClient creates a new PubSub client. It uses a default configuration.
117
133
func NewClient (ctx context.Context , projectID string , opts ... option.ClientOption ) (c * Client , err error ) {
118
134
return NewClientWithConfig (ctx , projectID , nil , opts ... )
119
135
}
120
136
121
137
// NewClientWithConfig creates a new PubSub client.
122
138
func NewClientWithConfig (ctx context.Context , projectID string , config * ClientConfig , opts ... option.ClientOption ) (c * Client , err error ) {
139
+ if projectID == "" {
140
+ return nil , ErrEmptyProjectID
141
+ }
123
142
var o []option.ClientOption
124
143
// Environment variables for gcloud emulator:
125
144
// https://cloud.google.com/sdk/gcloud/reference/beta/emulators/pubsub/
@@ -157,13 +176,26 @@ func NewClientWithConfig(ctx context.Context, projectID string, config *ClientCo
157
176
subc .CallOptions = mergeSubscriberCallOptions (subc .CallOptions , config .SubscriberCallOptions )
158
177
}
159
178
pubc .SetGoogleClientInfo ("gccl" , internal .Version )
179
+
180
+ // Handle project autodetection.
181
+ projectID , err = detect .ProjectID (ctx , projectID , "" , opts ... )
182
+ if err != nil {
183
+ return nil , err
184
+ }
185
+
160
186
return & Client {
161
187
projectID : projectID ,
162
188
pubc : pubc ,
163
189
subc : subc ,
164
190
}, nil
165
191
}
166
192
193
+ // Project returns the project ID or number for this instance of the client, which may have
194
+ // either been explicitly specified or autodetected.
195
+ func (c * Client ) Project () string {
196
+ return c .projectID
197
+ }
198
+
167
199
// Close releases any resources held by the client,
168
200
// such as memory and goroutines.
169
201
//
0 commit comments