Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gengapic): add universe domain support #1452

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions internal/gengapic/client_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
)

const (
googleDefaultUniverse = "googleapis.com"
universeDomainPlaceholder = "UNIVERSE_DOMAIN"
)

func (g *generator) clientHook(servName string) {
p := g.printf

Expand Down Expand Up @@ -385,6 +390,21 @@ func (g *generator) makeClients(serv *descriptor.ServiceDescriptorProto, servNam
return nil
}

// generateDefaultEndpointTemplate returns the default endpoint with the
// Google Default Universe (googleapis.com) replaced with the placeholder
// UNIVERSE_DOMAIN for universe domain substitution.
//
// We need to apply the following type of transformation:
// 1. pubsub.googleapis.com to pubsub.UNIVERSE_DOMAIN
// 2. pubsub.sandbox.googleapis.com to pubsub.sandbox.UNIVERSE_DOMAIN
//
// This function is needed because the default endpoint template is currently
// not part of the service proto. In the future, we should update the
// service proto to include a new "google.api.default_endpoint_template" option.
func generateDefaultEndpointTemplate(defaultEndpoint string) string {
return strings.Replace(defaultEndpoint, googleDefaultUniverse, universeDomainPlaceholder, 1)
}

// generateDefaultMTLSEndpoint attempts to derive the mTLS version of the
// defaultEndpoint via regex, and returns defaultEndpoint if unsuccessful.
//
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/gengrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ func (g *generator) grpcClientOptions(serv *descriptor.ServiceDescriptorProto, s
p("func default%sGRPCClientOptions() []option.ClientOption {", servName)
p(" return []option.ClientOption{")
p(" internaloption.WithDefaultEndpoint(%q),", host)
p(" internaloption.WithDefaultEndpointTemplate(%q),", generateDefaultEndpointTemplate(host))
p(" internaloption.WithDefaultMTLSEndpoint(%q),", generateDefaultMTLSEndpoint(host))
p(" internaloption.WithDefaultUniverseDomain(%q),", googleDefaultUniverse)
Comment on lines +180 to +182
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without knowing much how this will ultimately work, do the WithDefaultEndpoint and WithDefaultMTLSEndpoint options go away assuming endpoint resolution is done by the middleware?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithDefaultEndpoint is already deprecated, and will eventually go away post-transition. WithDefaultMTLSEndpoint is unaffected by these changes as that auth flow does not support universe domain.

p(" internaloption.WithDefaultAudience(%q),", generateDefaultAudience(host))
p(" internaloption.WithDefaultScopes(DefaultAuthScopes()...),")
p(" internaloption.EnableJwtWithScope(),")
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ func (g *generator) restClientOptions(serv *descriptor.ServiceDescriptorProto, s
p("func default%sRESTClientOptions() []option.ClientOption {", servName)
p(" return []option.ClientOption{")
p(" internaloption.WithDefaultEndpoint(%q),", host)
p(" internaloption.WithDefaultEndpointTemplate(%q),", generateDefaultEndpointTemplate(host))
p(" internaloption.WithDefaultMTLSEndpoint(%q),", generateDefaultMTLSEndpoint(host))
p(" internaloption.WithDefaultUniverseDomain(%q),", googleDefaultUniverse)
p(" internaloption.WithDefaultAudience(%q),", generateDefaultAudience(host))
p(" internaloption.WithDefaultScopes(DefaultAuthScopes()...),")
p(" }")
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/testdata/empty_opt.want
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ type CallOptions struct {
func defaultGRPCClientOptions() []option.ClientOption {
return []option.ClientOption{
internaloption.WithDefaultEndpoint("foo.googleapis.com:443"),
internaloption.WithDefaultEndpointTemplate("foo.UNIVERSE_DOMAIN:443"),
internaloption.WithDefaultMTLSEndpoint("foo.mtls.googleapis.com:443"),
internaloption.WithDefaultUniverseDomain("googleapis.com"),
internaloption.WithDefaultAudience("https://foo.googleapis.com/"),
internaloption.WithDefaultScopes(DefaultAuthScopes()...),
internaloption.EnableJwtWithScope(),
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/testdata/foo_opt.want
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ type FooCallOptions struct {
func defaultFooGRPCClientOptions() []option.ClientOption {
return []option.ClientOption{
internaloption.WithDefaultEndpoint("foo.googleapis.com:443"),
internaloption.WithDefaultEndpointTemplate("foo.UNIVERSE_DOMAIN:443"),
internaloption.WithDefaultMTLSEndpoint("foo.mtls.googleapis.com:443"),
internaloption.WithDefaultUniverseDomain("googleapis.com"),
internaloption.WithDefaultAudience("https://foo.googleapis.com/"),
internaloption.WithDefaultScopes(DefaultAuthScopes()...),
internaloption.EnableJwtWithScope(),
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/testdata/host_port_opt.want
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ type BarCallOptions struct {
func defaultBarGRPCClientOptions() []option.ClientOption {
return []option.ClientOption{
internaloption.WithDefaultEndpoint("foo.googleapis.com:1234"),
internaloption.WithDefaultEndpointTemplate("foo.UNIVERSE_DOMAIN:1234"),
internaloption.WithDefaultMTLSEndpoint("foo.mtls.googleapis.com:1234"),
internaloption.WithDefaultUniverseDomain("googleapis.com"),
internaloption.WithDefaultAudience("https://foo.googleapis.com/"),
internaloption.WithDefaultScopes(DefaultAuthScopes()...),
internaloption.EnableJwtWithScope(),
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/testdata/iam_override_opt.want
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type BazCallOptions struct {
func defaultBazGRPCClientOptions() []option.ClientOption {
return []option.ClientOption{
internaloption.WithDefaultEndpoint("foo.googleapis.com:1234"),
internaloption.WithDefaultEndpointTemplate("foo.UNIVERSE_DOMAIN:1234"),
internaloption.WithDefaultMTLSEndpoint("foo.mtls.googleapis.com:1234"),
internaloption.WithDefaultUniverseDomain("googleapis.com"),
internaloption.WithDefaultAudience("https://foo.googleapis.com/"),
internaloption.WithDefaultScopes(DefaultAuthScopes()...),
internaloption.EnableJwtWithScope(),
Expand Down