diff --git a/internal/settings.go b/internal/settings.go index 84f9302dcfa..3356fa97b8f 100644 --- a/internal/settings.go +++ b/internal/settings.go @@ -55,6 +55,8 @@ type DialSettings struct { EnableDirectPathXds bool EnableNewAuthLibrary bool AllowNonDefaultServiceAccount bool + UniverseDomain string + DefaultUniverseDomain string // Google API system parameters. For more information please read: // https://cloud.google.com/apis/docs/system-parameters diff --git a/option/internaloption/internaloption.go b/option/internaloption/internaloption.go index b2b249eec68..3fdee095c1b 100644 --- a/option/internaloption/internaloption.go +++ b/option/internaloption/internaloption.go @@ -126,6 +126,22 @@ func (w withDefaultScopes) Apply(o *internal.DialSettings) { copy(o.DefaultScopes, w) } +// WithDefaultUniverseDomain returns a ClientOption that sets the default universe domain. +// +// It should only be used internally by generated clients. +// +// This is similar to the public WithUniverse, but allows us to determine whether the user has +// overridden the default universe. +func WithDefaultUniverseDomain(ud string) option.ClientOption { + return withDefaultUniverseDomain(ud) +} + +type withDefaultUniverseDomain string + +func (w withDefaultUniverseDomain) Apply(o *internal.DialSettings) { + o.DefaultUniverseDomain = string(w) +} + // EnableJwtWithScope returns a ClientOption that specifies if scope can be used // with self-signed JWT. func EnableJwtWithScope() option.ClientOption { diff --git a/option/internaloption/internaloption_test.go b/option/internaloption/internaloption_test.go index 0ad09f8c236..8f322f9815c 100644 --- a/option/internaloption/internaloption_test.go +++ b/option/internaloption/internaloption_test.go @@ -7,9 +7,13 @@ package internaloption import ( "testing" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "golang.org/x/oauth2" "golang.org/x/oauth2/google" "google.golang.org/api/internal" + "google.golang.org/api/option" + "google.golang.org/grpc" ) func TestWithCredentials(t *testing.T) { @@ -27,3 +31,27 @@ func TestWithCredentials(t *testing.T) { t.Errorf("ts.Token() = %q, want %q", tok.AccessToken, "") } } + +func TestDefaultApply(t *testing.T) { + opts := []option.ClientOption{ + WithDefaultEndpoint("https://example.com:443"), + WithDefaultMTLSEndpoint("http://mtls.example.com:445"), + WithDefaultScopes("a"), + WithDefaultUniverseDomain("foo.com"), + WithDefaultAudience("audience"), + } + var got internal.DialSettings + for _, opt := range opts { + opt.Apply(&got) + } + want := internal.DialSettings{ + DefaultScopes: []string{"a"}, + DefaultEndpoint: "https://example.com:443", + DefaultUniverseDomain: "foo.com", + DefaultAudience: "audience", + DefaultMTLSEndpoint: "http://mtls.example.com:445", + } + if !cmp.Equal(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain")) { + t.Errorf(cmp.Diff(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain"))) + } +} diff --git a/option/option.go b/option/option.go index b2085a1949a..c882c1eb482 100644 --- a/option/option.go +++ b/option/option.go @@ -343,3 +343,16 @@ func (w *withCreds) Apply(o *internal.DialSettings) { func WithCredentials(creds *google.Credentials) ClientOption { return (*withCreds)(creds) } + +// WithUniverseDomain returns a ClientOption that sets the universe domain. +// +// This is an EXPERIMENTAL API and may be changed or removed in the future. +func WithUniverseDomain(ud string) ClientOption { + return withUniverseDomain(ud) +} + +type withUniverseDomain string + +func (w withUniverseDomain) Apply(o *internal.DialSettings) { + o.UniverseDomain = string(w) +} diff --git a/option/option_test.go b/option/option_test.go index bbd673eb0af..93129307c27 100644 --- a/option/option_test.go +++ b/option/option_test.go @@ -73,6 +73,7 @@ func TestApply(t *testing.T) { WithQuotaProject("user-project"), WithRequestReason("Request Reason"), WithTelemetryDisabled(), + WithUniverseDomain("universe.com"), } var got internal.DialSettings for _, opt := range opts { @@ -91,6 +92,7 @@ func TestApply(t *testing.T) { QuotaProject: "user-project", RequestReason: "Request Reason", TelemetryDisabled: true, + UniverseDomain: "universe.com", } if !cmp.Equal(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain")) { t.Errorf(cmp.Diff(got, want, cmpopts.IgnoreUnexported(grpc.ClientConn{}), cmpopts.IgnoreFields(google.Credentials{}, "universeDomain")))