Skip to content

Commit f5a7dbf

Browse files
authored
feat(core): Support for universe_domain (#17131)
1 parent f4dfbca commit f5a7dbf

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

google-apis-core/google-apis-core.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Gem::Specification.new do |gem|
2424
gem.add_runtime_dependency "retriable", ">= 2.0", "< 4.a"
2525
gem.add_runtime_dependency "addressable", "~> 2.5", ">= 2.5.1"
2626
gem.add_runtime_dependency "mini_mime", "~> 1.0"
27-
gem.add_runtime_dependency "googleauth", ">= 0.16.2", "< 2.a"
27+
gem.add_runtime_dependency "googleauth", "~> 1.9"
2828
gem.add_runtime_dependency "httpclient", ">= 2.8.1", "< 3.a"
2929
gem.add_runtime_dependency "rexml"
3030
end

google-apis-core/lib/google/apis/core/base_service.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,49 @@ def each
9393
# Base service for all APIs. Not to be used directly.
9494
#
9595
class BaseService
96+
##
97+
# A substitution string for the universe domain in an endpoint template
98+
# @return [String]
99+
#
100+
ENDPOINT_SUBSTITUTION = "$UNIVERSE_DOMAIN$".freeze
101+
96102
include Logging
97103

104+
# Universe domain
105+
# @return [String]
106+
attr_reader :universe_domain
107+
108+
# Set the universe domain.
109+
# If the root URL was set with a universe domain substitution, it is
110+
# updated to reflect the new universe domain.
111+
#
112+
# @param new_ud [String,nil] The new universe domain, or nil to use the Google Default Universe
113+
def universe_domain= new_ud
114+
new_ud ||= ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] || "googleapis.com"
115+
if @root_url_template
116+
@root_url = @root_url_template.gsub ENDPOINT_SUBSTITUTION, new_ud
117+
end
118+
@universe_domain = new_ud
119+
end
120+
98121
# Root URL (host/port) for the API
99122
# @return [Addressable::URI]
100-
attr_accessor :root_url
123+
attr_reader :root_url
124+
125+
# Set the root URL.
126+
# If the given url includes a universe domain substitution, it is
127+
# resolved in the current universe domain
128+
#
129+
# @param url_or_template [String] The URL, which can include a universe domain substitution
130+
def root_url= url_or_template
131+
if url_or_template.include? ENDPOINT_SUBSTITUTION
132+
@root_url_template = url_or_template
133+
@root_url = url_or_template.gsub ENDPOINT_SUBSTITUTION, universe_domain
134+
else
135+
@root_url_template = nil
136+
@root_url = url_or_template
137+
end
138+
end
101139

102140
# Additional path prefix for all API methods
103141
# @return [Addressable::URI]
@@ -136,7 +174,9 @@ class BaseService
136174
# @param [String,Addressable::URI] base_path
137175
# Additional path prefix for all API methods
138176
# @api private
139-
def initialize(root_url, base_path, client_name: nil, client_version: nil)
177+
def initialize(root_url, base_path, client_name: nil, client_version: nil, universe_domain: nil)
178+
@root_url_template = nil
179+
self.universe_domain = universe_domain
140180
self.root_url = root_url
141181
self.base_path = base_path
142182
self.client_name = client_name || 'google-api-ruby-client'

google-apis-core/spec/google/apis/core/service_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
let(:client_version) { "1.2.3" }
2424
let(:service) { Google::Apis::Core::BaseService.new('https://www.googleapis.com/', '', client_version: client_version) }
25+
let(:service_ud) { Google::Apis::Core::BaseService.new('https://www.$UNIVERSE_DOMAIN$/', '', client_version: client_version) }
2526
let(:service_with_base_path) { Google::Apis::Core::BaseService.new('https://www.googleapis.com/', 'my_service/v1/', client_version: client_version) }
2627
let(:x_goog_api_client_value) { "gl-ruby/#{RUBY_VERSION} gdcl/#{client_version}" }
2728

@@ -477,4 +478,57 @@
477478
end
478479
end
479480
end
481+
482+
it "should default to the global universe domain" do
483+
expect(service_ud.universe_domain).to eql "googleapis.com"
484+
end
485+
486+
it "should default to a universe domain from the environment" do
487+
ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = "mydomain1.com"
488+
expect(service_ud.universe_domain).to eql "mydomain1.com"
489+
ensure
490+
ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = nil
491+
end
492+
493+
it "should support setting universe domain" do
494+
service_ud.universe_domain = "mydomain2.com"
495+
expect(service_ud.universe_domain).to eql "mydomain2.com"
496+
end
497+
498+
it "should support setting universe domain to nil" do
499+
service_ud.universe_domain = "mydomain2.com"
500+
service_ud.universe_domain = nil
501+
expect(service_ud.universe_domain).to eql "googleapis.com"
502+
end
503+
504+
it "should update root url when universe domain is set" do
505+
service_ud.universe_domain = "mydomain3.com"
506+
expect(service_ud.root_url).to eql "https://www.mydomain3.com/"
507+
end
508+
509+
it "should initialize root url using the universe domain" do
510+
ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = "mydomain4.com"
511+
expect(service_ud.root_url).to eql "https://www.mydomain4.com/"
512+
ensure
513+
ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = nil
514+
end
515+
516+
it "should initialize root url with a static value" do
517+
expect(service.root_url).to eql "https://www.googleapis.com/"
518+
end
519+
520+
it "should initialize root url with a dynamic value" do
521+
expect(service_ud.root_url).to eql "https://www.googleapis.com/"
522+
end
523+
524+
it "should suppport setting root url to a static value" do
525+
service_ud.root_url = "https://endpoint1.mydomain5.com/"
526+
expect(service_ud.root_url).to eql "https://endpoint1.mydomain5.com/"
527+
end
528+
529+
it "should suppport setting root url to a dynamic value" do
530+
service.universe_domain = "mydomain6.com"
531+
service.root_url = "https://endpoint2.$UNIVERSE_DOMAIN$/"
532+
expect(service.root_url).to eql "https://endpoint2.mydomain6.com/"
533+
end
480534
end

0 commit comments

Comments
 (0)