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 all 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
20 changes: 19 additions & 1 deletion google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
)

const (
googleDiscoveryURL = "https://www.googleapis.com/discovery/v1/apis"
googleDiscoveryURL = "https://www.googleapis.com/discovery/v1/apis"
googleDefaultUniverse = "googleapis.com"
universeDomainPlaceholder = "UNIVERSE_DOMAIN"
)

var (
Expand Down Expand Up @@ -498,6 +500,14 @@ func (a *API) apiBaseURL() string {
return resolveRelative(base, rel)
}

// apiBaseURLTemplate returns the value returned by apiBaseURL with the
// Google Default Universe (googleapis.com) replaced with the placeholder
// UNIVERSE_DOMAIN for universe domain substitution.
func (a *API) apiBaseURLTemplate() (string, error) {
base := a.apiBaseURL()
return strings.Replace(base, googleDefaultUniverse, universeDomainPlaceholder, 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 +770,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 +801,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
32 changes: 32 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,35 @@ 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)
}
}
}
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
4 changes: 4 additions & 0 deletions google-api-go-generator/testdata/param-rename.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "paramrename:v1"
const apiName = "paramrename"
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/quotednum.want
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ const apiId = "adexchangebuyer:v1.1"
const apiName = "adexchangebuyer"
const apiVersion = "v1.1"
const basePath = "https://www.googleapis.com/adexchangebuyer/v1.1/"
const basePathTemplate = "https://www.UNIVERSE_DOMAIN/adexchangebuyer/v1.1/"
const mtlsBasePath = "https://www.mtls.googleapis.com/adexchangebuyer/v1.1/"
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/repeated.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ const apiId = "repeated:v1"
const apiName = "repeated"
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