Skip to content

Commit e648a9d

Browse files
authored
feat: expose experimental universe-related options (#2264)
This PR adds the first step in plumbing a universe option into clients by first exposing it as part of the option package. It exposes a `WithUniverse(string)` as a public option, and `WithDefaultUniverse(string)` as an internal option that can be used by generated clients. It also augments existing tests. It does not yet include any behavioral changes.
1 parent e8e2895 commit e648a9d

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

internal/settings.go

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type DialSettings struct {
5555
EnableDirectPathXds bool
5656
EnableNewAuthLibrary bool
5757
AllowNonDefaultServiceAccount bool
58+
UniverseDomain string
59+
DefaultUniverseDomain string
5860

5961
// Google API system parameters. For more information please read:
6062
// https://cloud.google.com/apis/docs/system-parameters

option/internaloption/internaloption.go

+16
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ func (w withDefaultScopes) Apply(o *internal.DialSettings) {
126126
copy(o.DefaultScopes, w)
127127
}
128128

129+
// WithDefaultUniverseDomain returns a ClientOption that sets the default universe domain.
130+
//
131+
// It should only be used internally by generated clients.
132+
//
133+
// This is similar to the public WithUniverse, but allows us to determine whether the user has
134+
// overridden the default universe.
135+
func WithDefaultUniverseDomain(ud string) option.ClientOption {
136+
return withDefaultUniverseDomain(ud)
137+
}
138+
139+
type withDefaultUniverseDomain string
140+
141+
func (w withDefaultUniverseDomain) Apply(o *internal.DialSettings) {
142+
o.DefaultUniverseDomain = string(w)
143+
}
144+
129145
// EnableJwtWithScope returns a ClientOption that specifies if scope can be used
130146
// with self-signed JWT.
131147
func EnableJwtWithScope() option.ClientOption {

option/internaloption/internaloption_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ package internaloption
77
import (
88
"testing"
99

10+
"github.com/google/go-cmp/cmp"
11+
"github.com/google/go-cmp/cmp/cmpopts"
1012
"golang.org/x/oauth2"
1113
"golang.org/x/oauth2/google"
1214
"google.golang.org/api/internal"
15+
"google.golang.org/api/option"
16+
"google.golang.org/grpc"
1317
)
1418

1519
func TestWithCredentials(t *testing.T) {
@@ -27,3 +31,27 @@ func TestWithCredentials(t *testing.T) {
2731
t.Errorf("ts.Token() = %q, want %q", tok.AccessToken, "")
2832
}
2933
}
34+
35+
func TestDefaultApply(t *testing.T) {
36+
opts := []option.ClientOption{
37+
WithDefaultEndpoint("https://example.com:443"),
38+
WithDefaultMTLSEndpoint("http://mtls.example.com:445"),
39+
WithDefaultScopes("a"),
40+
WithDefaultUniverseDomain("foo.com"),
41+
WithDefaultAudience("audience"),
42+
}
43+
var got internal.DialSettings
44+
for _, opt := range opts {
45+
opt.Apply(&got)
46+
}
47+
want := internal.DialSettings{
48+
DefaultScopes: []string{"a"},
49+
DefaultEndpoint: "https://example.com:443",
50+
DefaultUniverseDomain: "foo.com",
51+
DefaultAudience: "audience",
52+
DefaultMTLSEndpoint: "http://mtls.example.com:445",
53+
}
54+
if !cmp.Equal(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain")) {
55+
t.Errorf(cmp.Diff(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain")))
56+
}
57+
}

option/option.go

+13
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,16 @@ func (w *withCreds) Apply(o *internal.DialSettings) {
343343
func WithCredentials(creds *google.Credentials) ClientOption {
344344
return (*withCreds)(creds)
345345
}
346+
347+
// WithUniverseDomain returns a ClientOption that sets the universe domain.
348+
//
349+
// This is an EXPERIMENTAL API and may be changed or removed in the future.
350+
func WithUniverseDomain(ud string) ClientOption {
351+
return withUniverseDomain(ud)
352+
}
353+
354+
type withUniverseDomain string
355+
356+
func (w withUniverseDomain) Apply(o *internal.DialSettings) {
357+
o.UniverseDomain = string(w)
358+
}

option/option_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func TestApply(t *testing.T) {
7373
WithQuotaProject("user-project"),
7474
WithRequestReason("Request Reason"),
7575
WithTelemetryDisabled(),
76+
WithUniverseDomain("universe.com"),
7677
}
7778
var got internal.DialSettings
7879
for _, opt := range opts {
@@ -91,6 +92,7 @@ func TestApply(t *testing.T) {
9192
QuotaProject: "user-project",
9293
RequestReason: "Request Reason",
9394
TelemetryDisabled: true,
95+
UniverseDomain: "universe.com",
9496
}
9597
if !cmp.Equal(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain")) {
9698
t.Errorf(cmp.Diff(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain")))

0 commit comments

Comments
 (0)