Skip to content

Commit

Permalink
feat: expose experimental universe-related options (#2264)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shollyman committed Nov 30, 2023
1 parent e8e2895 commit e648a9d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions option/internaloption/internaloption.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions option/internaloption/internaloption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")))
}
}
13 changes: 13 additions & 0 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions option/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")))
Expand Down

0 comments on commit e648a9d

Please sign in to comment.