Skip to content

Commit

Permalink
feat(option/internaloption): add WithDefaultEndpointTemplate
Browse files Browse the repository at this point in the history
* Add DefaultEndpointTemplate to internal/settings.go
* Deprecate internaloption.WithDefaultEndpoint

refs: googleapis#2264
  • Loading branch information
quartzmo committed Dec 18, 2023
1 parent bb522ac commit deb2e46
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/cba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func TestGetHTTPTransportConfigAndEndpoint(t *testing.T) {

func setupTest() func() {
oldDefaultMTLSEnabled := mtlsEndpointEnabledForS2A
oldHTTPGet := httpGetMetadataMTLSConfig
oldHTTPGet := httpGetMetadataMTLSConfig
oldExpiry := configExpiry
oldUseS2A := os.Getenv(googleAPIUseS2AEnv)
oldUseClientCert := os.Getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE")
Expand Down
1 change: 1 addition & 0 deletions internal/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
type DialSettings struct {
Endpoint string
DefaultEndpoint string
DefaultEndpointTemplate string
DefaultMTLSEndpoint string
Scopes []string
DefaultScopes []string
Expand Down
19 changes: 19 additions & 0 deletions option/internaloption/internaloption.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,29 @@ func (o defaultEndpointOption) Apply(settings *internal.DialSettings) {
// It should only be used internally by generated clients.
//
// This is similar to WithEndpoint, but allows us to determine whether the user has overridden the default endpoint.
//
// Deprecated: WithDefaultEndpoint does not support setting the universe domain.
// Use WithDefaultEndpointTemplate and WithDefaultUniverseDomain to compose the
// default endpoint instead.
func WithDefaultEndpoint(url string) option.ClientOption {
return defaultEndpointOption(url)
}

type defaultEndpointTemplateOption string

func (o defaultEndpointTemplateOption) Apply(settings *internal.DialSettings) {
settings.DefaultEndpointTemplate = string(o)
}

// WithDefaultEndpointTemplate provides a template for creating the endpoint using a universe domain.
//
// It should only be used internally by generated clients.
//
// TODO(chrisdsmith): Refs: CL-R3 (remove this note before publication)
func WithDefaultEndpointTemplate(url string) option.ClientOption {
return defaultEndpointTemplateOption(url)
}

type defaultMTLSEndpointOption string

func (o defaultMTLSEndpointOption) Apply(settings *internal.DialSettings) {
Expand Down
12 changes: 7 additions & 5 deletions option/internaloption/internaloption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestWithCredentials(t *testing.T) {
func TestDefaultApply(t *testing.T) {
opts := []option.ClientOption{
WithDefaultEndpoint("https://example.com:443"),
WithDefaultEndpointTemplate("https://foo.%s/"),
WithDefaultMTLSEndpoint("http://mtls.example.com:445"),
WithDefaultScopes("a"),
WithDefaultUniverseDomain("foo.com"),
Expand All @@ -45,11 +46,12 @@ func TestDefaultApply(t *testing.T) {
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",
DefaultScopes: []string{"a"},
DefaultEndpoint: "https://example.com:443",
DefaultEndpointTemplate: "https://foo.%s/",
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")))
Expand Down

0 comments on commit deb2e46

Please sign in to comment.