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(google-api-go-generator): add universe domain support #2335

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,42 @@ func (a *API) apiBaseURL() string {
return resolveRelative(base, rel)
}

// apiBaseURLTemplate returns the value returned by apiBaseURL with the
// following changes: 1) The hostname (excluding subdomains) is replaced with
// UNIVERSE_DOMAIN for universe domain substitution. 2) If the value does not
// have a scheme, https:// is prepended.
func (a *API) apiBaseURLTemplate() (string, error) {
baseURL := a.apiBaseURL()
return replaceDomain(baseURL, "UNIVERSE_DOMAIN")
}

// replaceDomain detects the domain (excluding subdomains) in the provided
// baseURL and replaces it with newDomain. If baseURL is empty, empty is
// returned. If baseURL is an invalid URL, the error from url.Parse is returned.
// If baseURL is valid but does not have a scheme, https:// is prepended to the
// return value.
func replaceDomain(baseURL, newDomain string) (string, error) {
quartzmo marked this conversation as resolved.
Show resolved Hide resolved
if baseURL == "" {
return baseURL, nil
}
if !strings.Contains(baseURL, "://") {
// Prepend scheme if missing or url.Parse might fail to detect hostname.
baseURL = "https://" + baseURL
}
u, err := url.Parse(baseURL)
if err != nil {
return "", err
}
parts := strings.Split(u.Hostname(), ".")
var domain string
if len(parts) == 1 {
domain = parts[0]
} else {
domain = parts[len(parts)-2] + "." + parts[len(parts)-1]
}
return strings.Replace(baseURL, domain, newDomain, 1), nil
}

func (a *API) mtlsAPIBaseURL() string {
if a.doc.MTLSRootURL != "" {
return resolveRelative(a.doc.MTLSRootURL, a.doc.ServicePath)
Expand Down Expand Up @@ -760,9 +796,15 @@ func (a *API) GenerateCode() ([]byte, error) {
pn("const apiName = %q", a.doc.Name)
pn("const apiVersion = %q", a.doc.Version)
pn("const basePath = %q", a.apiBaseURL())
basePathTemplate, err := a.apiBaseURLTemplate()
if err != nil {
return buf.Bytes(), err
}
pn("const basePathTemplate = %q", basePathTemplate)
if mtlsBase := a.mtlsAPIBaseURL(); mtlsBase != "" {
pn("const mtlsBasePath = %q", mtlsBase)
}
pn("const defaultUniverseDomain = \"googleapis.com\"")

a.generateScopeConstants()
a.PopulateSchemas()
Expand All @@ -785,9 +827,11 @@ func (a *API) GenerateCode() ([]byte, error) {
pn("opts = append([]option.ClientOption{scopesOption}, opts...)")
}
pn("opts = append(opts, internaloption.WithDefaultEndpoint(basePath))")
pn("opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))")
if a.mtlsAPIBaseURL() != "" {
pn("opts = append(opts, internaloption.WithDefaultMTLSEndpoint(mtlsBasePath))")
}
pn("opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))")
pn("client, endpoint, err := htransport.NewClient(ctx, opts...)")
pn("if err != nil { return nil, err }")
pn("s, err := New(client)")
Expand Down
72 changes: 72 additions & 0 deletions google-api-go-generator/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,75 @@ func TestAsComment_LongLink(t *testing.T) {
t.Errorf("got %q, want %q", got, want)
}
}

func TestApiBaseURLTemplate(t *testing.T) {
tests := []struct {
name, want string
}{
{
name: "any.json",
want: "https://logging.UNIVERSE_DOMAIN/",
},
{
name: "blogger-3.json",
want: "https://www.UNIVERSE_DOMAIN/blogger/v3/",
},
{
name: "required-query.json",
want: "https://www.UNIVERSE_DOMAIN/_ah/api/tshealth/v1/",
},
}
for _, tt := range tests {
api, err := apiFromFile(filepath.Join("testdata", tt.name))
if err != nil {
t.Fatalf("Error loading API testdata/%s: %v", tt.name, err)
}
got, err := api.apiBaseURLTemplate()
if err != nil {
t.Fatalf("%s: apiBaseURLTemplate(): %v", tt.name, err)
}
if got != tt.want {
t.Errorf("%s: apiBaseURLTemplate() = %q; want %q", tt.name, got, tt.want)
}
}
}

func TestReplaceDomain(t *testing.T) {
tests := []struct {
url, want string
}{
{
url: "https://logging.googleapis.com/",
want: "https://logging.UNIVERSE_DOMAIN/",
},
{
url: "https://www.googleapis.com/blogger/v3/",
want: "https://www.UNIVERSE_DOMAIN/blogger/v3/",
},
{
url: "ths-prod.googleplex.com/_ah/api/tshealth/v1/",
want: "https://ths-prod.UNIVERSE_DOMAIN/_ah/api/tshealth/v1/",
},
{
url: "localhost:1234",
want: "https://UNIVERSE_DOMAIN:1234",
},
{
url: "localhost",
want: "https://UNIVERSE_DOMAIN",
},
{
url: "",
want: "",
},
}
for _, tt := range tests {
got, err := replaceDomain(tt.url, "UNIVERSE_DOMAIN")
if err != nil {
t.Fatalf("apiBaseURLTemplate(%s): %v", tt.url, err)
}
if got != tt.want {
t.Errorf("apiBaseURLTemplate(%s) = %q; want %q", tt.url, got, tt.want)
}
}
}
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/any.want
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ const apiId = "logging:v1beta3"
const apiName = "logging"
const apiVersion = "v1beta3"
const basePath = "https://logging.googleapis.com/"
const basePathTemplate = "https://logging.UNIVERSE_DOMAIN/"
const mtlsBasePath = "https://logging.mtls.googleapis.com/"
const defaultUniverseDomain = "googleapis.com"

// OAuth2 scopes used by this API.
const (
Expand All @@ -106,7 +108,9 @@ func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, err
// NOTE: prepend, so we don't override user-specified scopes.
opts = append([]option.ClientOption{scopesOption}, opts...)
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(mtlsBasePath))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/arrayofarray-1.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "arrayofarray:v1"
const apiName = "arrayofarray"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/arrayofenum.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "arrayofenum:v1"
const apiName = "arrayofenum"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/arrayofmapofobjects.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "arrayofmapofstrings:v1"
const apiName = "arrayofmapofstrings"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/arrayofmapofstrings.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "arrayofmapofstrings:v1"
const apiName = "arrayofmapofstrings"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/blogger-3.want
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ const apiId = "blogger:v3"
const apiName = "blogger"
const apiVersion = "v3"
const basePath = "https://www.googleapis.com/blogger/v3/"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/blogger/v3/"
const mtlsBasePath = "https://www.mtls.googleapis.com/blogger/v3/"
const defaultUniverseDomain = "googleapis.com"

// OAuth2 scopes used by this API.
const (
Expand All @@ -115,7 +117,9 @@ func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, err
// NOTE: prepend, so we don't override user-specified scopes.
opts = append([]option.ClientOption{scopesOption}, opts...)
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(mtlsBasePath))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/floats.want
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ const apiId = "X:v1"
const apiName = "X"
const apiVersion = "v1"
const basePath = "https://appengine.googleapis.com/"
const basePathTemplate = "https://appengine.UNIVERSE_DOMAIN/"
const mtlsBasePath = "https://appengine.mtls.googleapis.com/"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(mtlsBasePath))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/getwithoutbody.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "getwithoutbody:v1"
const apiName = "getwithoutbody"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/http-body.want
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ const apiId = "healthcare:v1beta1"
const apiName = "healthcare"
const apiVersion = "v1beta1"
const basePath = "https://healthcare.googleapis.com/"
const basePathTemplate = "https://healthcare.UNIVERSE_DOMAIN/"
const mtlsBasePath = "https://healthcare.mtls.googleapis.com/"
const defaultUniverseDomain = "googleapis.com"

// OAuth2 scopes used by this API.
const (
Expand All @@ -106,7 +108,9 @@ func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, err
// NOTE: prepend, so we don't override user-specified scopes.
opts = append([]option.ClientOption{scopesOption}, opts...)
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(mtlsBasePath))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/json-body.want
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ const apiId = "ml:v1"
const apiName = "ml"
const apiVersion = "v1"
const basePath = "https://ml.googleapis.com/"
const basePathTemplate = "https://ml.UNIVERSE_DOMAIN/"
const mtlsBasePath = "https://ml.mtls.googleapis.com/"
const defaultUniverseDomain = "googleapis.com"

// OAuth2 scopes used by this API.
const (
Expand All @@ -106,7 +108,9 @@ func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, err
// NOTE: prepend, so we don't override user-specified scopes.
opts = append([]option.ClientOption{scopesOption}, opts...)
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(mtlsBasePath))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/mapofany.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "mapofany:v1"
const apiName = "mapofany"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/mapofarrayofobjects.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "additionalprops:v1"
const apiName = "additionalprops"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/mapofint64strings.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "androidbuildinternal:v1"
const apiName = "androidbuildinternal"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/mapofobjects.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "additionalpropsobjs:v1"
const apiName = "additionalpropsobjs"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/mapofstrings-1.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "additionalprops:v1"
const apiName = "additionalprops"
const apiVersion = "v1"
const basePath = "https://www.googleapis.com/discovery/v1/apis"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/discovery/v1/apis"
const defaultUniverseDomain = "googleapis.com"

// NewService creates a new Service.
func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, error) {
opts = append(opts, internaloption.WithDefaultEndpoint(basePath))
opts = append(opts, internaloption.WithDefaultEndpointTemplate(basePathTemplate))
opts = append(opts, internaloption.WithDefaultUniverseDomain(defaultUniverseDomain))
client, endpoint, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, err
Expand Down