From 04cd78a83fcbfb4e8bc725f8c4398ed9416611c3 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Mon, 18 Dec 2023 14:57:58 -0700 Subject: [PATCH] feat(option/internaloption): add WithDefaultEndpointTemplate * Add DefaultEndpointTemplate to internal/settings.go * Deprecate internaloption.WithDefaultEndpoint refs: #2264 --- internal/settings.go | 1 + option/internaloption/internaloption.go | 19 +++++++++++++++++++ option/internaloption/internaloption_test.go | 12 +++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/internal/settings.go b/internal/settings.go index 3356fa97b8f..285e6e04d39 100644 --- a/internal/settings.go +++ b/internal/settings.go @@ -27,6 +27,7 @@ const ( type DialSettings struct { Endpoint string DefaultEndpoint string + DefaultEndpointTemplate string DefaultMTLSEndpoint string Scopes []string DefaultScopes []string diff --git a/option/internaloption/internaloption.go b/option/internaloption/internaloption.go index 3fdee095c1b..c15be9faa96 100644 --- a/option/internaloption/internaloption.go +++ b/option/internaloption/internaloption.go @@ -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. See also WithDefaultUniverseDomain and +// option.WithUniverseDomain. +// +// It should only be used internally by generated clients. +func WithDefaultEndpointTemplate(url string) option.ClientOption { + return defaultEndpointTemplateOption(url) +} + type defaultMTLSEndpointOption string func (o defaultMTLSEndpointOption) Apply(settings *internal.DialSettings) { diff --git a/option/internaloption/internaloption_test.go b/option/internaloption/internaloption_test.go index 8f322f9815c..8b63c8a7e93 100644 --- a/option/internaloption/internaloption_test.go +++ b/option/internaloption/internaloption_test.go @@ -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"), @@ -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")))