Skip to content

Commit

Permalink
feat(bigtable): Support for setting the universe domain
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuma committed Feb 20, 2025
1 parent 2fe46bd commit 5ce2e60
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 23 deletions.
5 changes: 3 additions & 2 deletions google-cloud-bigtable/lib/google-cloud-bigtable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ def self.bigtable project_id: nil, credentials: nil, scope: nil, timeout: nil
config.add_field! :quota_project, nil, match: String
config.add_field! :timeout, nil, match: Integer
config.add_field! :emulator_host, default_emulator, match: String, allow_nil: true
config.add_field! :endpoint, "bigtable.googleapis.com", match: String
config.add_field! :endpoint_admin, "bigtableadmin.googleapis.com", match: String
config.add_field! :endpoint, nil, match: String
config.add_field! :endpoint_admin, nil, match: String
config.add_field! :channel_selection, :least_loaded, match: Symbol
config.add_field! :channel_count, 1, match: Integer
config.add_field! :universe_domain, nil, match: String, allow_nil: true
end
16 changes: 11 additions & 5 deletions google-cloud-bigtable/lib/google/cloud/bigtable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ module Bigtable
# should already be composed with a `GRPC::Core::CallCredentials` object.
# `Proc` will be used as an updater_proc for the gRPC channel. The proc transforms the
# metadata for requests, generally, to give OAuth credentials.
# @param [String] endpoint Override of the endpoint host name. Optional.
# @param universe_domain [String] Override of the universe domain. Optional.
# @param endpoint [String] Override of the endpoint host name. Optional.
# If the param is nil, uses the default endpoint.
# @param [String] endpoint_admin Override of the admin service endpoint host name. Optional.
# @param endpoint_admin [String] Override of the admin service endpoint host name. Optional.
# If the param is nil, uses the default admin endpoint.
# @param [String] emulator_host Bigtable emulator host. Optional.
# @param emulator_host [String] Bigtable emulator host. Optional.
# If the parameter is nil, uses the value of the `emulator_host` config.
# @param scope [Array<String>]
# The OAuth 2.0 scopes controlling the set of resources and operations
Expand All @@ -78,6 +79,7 @@ module Bigtable
# rubocop:disable Metrics/AbcSize
def self.new project_id: nil,
credentials: nil,
universe_domain: nil,
emulator_host: nil,
scope: nil,
endpoint: nil,
Expand All @@ -86,6 +88,7 @@ def self.new project_id: nil,
channel_selection: nil,
channel_count: nil
project_id ||= default_project_id
universe_domain ||= configure.universe_domain
scope ||= configure.scope
timeout ||= configure.timeout
emulator_host ||= configure.emulator_host
Expand All @@ -100,8 +103,11 @@ def self.new project_id: nil,
project_id = resolve_project_id project_id, credentials
raise ArgumentError, "project_id is missing" if project_id.empty?

service = Bigtable::Service.new project_id, credentials, host: endpoint,
host_admin: endpoint_admin, timeout: timeout,
service = Bigtable::Service.new project_id, credentials,
universe_domain: universe_domain,
host: endpoint,
host_admin: endpoint_admin,
timeout: timeout,
channel_selection: channel_selection,
channel_count: channel_count
Bigtable::Project.new service
Expand Down
10 changes: 10 additions & 0 deletions google-cloud-bigtable/lib/google/cloud/bigtable/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def initialize service
@service = service
end

##
# The universe domain the client is connected to
#
# @return [String]
#
def universe_domain
ensure_service!
service.universe_domain
end

##
# The identifier for the Cloud Bigtable project.
#
Expand Down
11 changes: 10 additions & 1 deletion google-cloud-bigtable/lib/google/cloud/bigtable/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class Service
# @private
attr_accessor :project_id, :credentials, :host, :host_admin, :timeout

# @private
def universe_domain
tables.universe_domain
end

# @private
# Creates a new Service instance.
#
Expand All @@ -53,21 +58,23 @@ class Service
# The default timeout, in seconds, for calls made through this client.
#
def initialize project_id, credentials, host: nil, host_admin: nil, timeout: nil,
channel_selection: nil, channel_count: nil
channel_selection: nil, channel_count: nil, universe_domain: nil
@project_id = project_id
@credentials = credentials
@host = host
@host_admin = host_admin
@timeout = timeout
@channel_selection = channel_selection
@channel_count = channel_count
@universe_domain_override = universe_domain
@bigtable_clients = ::Gapic::LruHash.new 10
@mutex = Mutex.new
end

def instances
return mocked_instances if mocked_instances
@instances ||= Admin::V2::BigtableInstanceAdmin::Client.new do |config|
config.universe_domain = @universe_domain_override if @universe_domain_override
config.credentials = credentials if credentials
config.timeout = timeout if timeout
config.endpoint = host_admin if host_admin
Expand All @@ -81,6 +88,7 @@ def instances
def tables
return mocked_tables if mocked_tables
@tables ||= Admin::V2::BigtableTableAdmin::Client.new do |config|
config.universe_domain = @universe_domain_override if @universe_domain_override
config.credentials = credentials if credentials
config.timeout = timeout if timeout
config.endpoint = host_admin if host_admin
Expand Down Expand Up @@ -895,6 +903,7 @@ def inspect
def create_bigtable_client table_path, app_profile_id
V2::Bigtable::Client.new do |config|
config.credentials = credentials if credentials
config.universe_domain = @universe_domain_override if @universe_domain_override
config.timeout = timeout if timeout
config.endpoint = host if host
config.lib_name = "gccl"
Expand Down
23 changes: 23 additions & 0 deletions google-cloud-bigtable/test/google/cloud/bigtable/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,27 @@
_(bigtable).must_be_kind_of Google::Cloud::Bigtable::Project
_(bigtable.project_id).must_equal project_id
end

it "returns the default universe domain" do
_(bigtable.universe_domain).must_equal "googleapis.com"
tables_stub = bigtable.service.tables.instance_variable_get :@bigtable_table_admin_stub
_(tables_stub.endpoint).must_equal "bigtableadmin.googleapis.com"
instances_stub = bigtable.service.instances.instance_variable_get :@bigtable_instance_admin_stub
_(instances_stub.endpoint).must_equal "bigtableadmin.googleapis.com"
main_stub = bigtable.service.client("mytable", "myprofile").instance_variable_get :@bigtable_stub
_(main_stub.endpoint).must_equal "bigtable.googleapis.com"
end

it "returns a custom universe domain" do
universe = "myuniverse.com"
service = Google::Cloud::Bigtable::Service.new project_id, credentials, universe_domain: universe
project = Google::Cloud::Bigtable::Project.new service
_(project.universe_domain).must_equal universe
tables_stub = service.tables.instance_variable_get :@bigtable_table_admin_stub
_(tables_stub.endpoint).must_equal "bigtableadmin.myuniverse.com"
instances_stub = service.instances.instance_variable_get :@bigtable_instance_admin_stub
_(instances_stub.endpoint).must_equal "bigtableadmin.myuniverse.com"
main_stub = service.client("mytable", "myprofile").instance_variable_get :@bigtable_stub
_(main_stub.endpoint).must_equal "bigtable.myuniverse.com"
end
end
32 changes: 17 additions & 15 deletions google-cloud-bigtable/test/google/cloud/bigtable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
Expand Down Expand Up @@ -177,7 +177,7 @@ def creds.is_a? target
end

it "uses provided endpoints" do
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(credentials).must_equal default_credentials
_(timeout).must_be :nil?
_(host).must_equal "bigtable-endpoint2.example.com"
Expand Down Expand Up @@ -205,7 +205,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
Expand Down Expand Up @@ -237,7 +237,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
Expand Down Expand Up @@ -269,7 +269,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
Expand Down Expand Up @@ -339,7 +339,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
OpenStruct.new project_id: "project-id"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_be_kind_of OpenStruct
_(credentials.project_id).must_equal "project-id"
Expand Down Expand Up @@ -370,7 +370,7 @@ def creds.is_a? target
end

it "uses channel config" do
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(credentials).must_equal default_credentials
_(timeout).must_be :nil?
_(channel_selection).must_equal :least_loaded
Expand Down Expand Up @@ -406,7 +406,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
Expand Down Expand Up @@ -445,7 +445,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
Expand Down Expand Up @@ -477,19 +477,19 @@ def creds.is_a? target
end
end

it "uses bigtable config for project and keyfile" do
it "uses bigtable config for project, keyfile, and universe domain" do
stubbed_credentials = lambda { |credentials, scope: nil|
_(credentials).must_equal "path/to/keyfile.json"
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_equal 42
_(host).must_equal default_host
_(host_admin).must_equal default_host_admin
OpenStruct.new project_id: project_id
OpenStruct.new project_id: project_id, universe_domain: universe_domain
}

# Clear all environment variables
Expand All @@ -499,6 +499,7 @@ def creds.is_a? target
config.project_id = "project-id"
config.credentials = "path/to/keyfile.json"
config.timeout = 42
config.universe_domain = "myuniverse.com"
end

File.stub :file?, true, ["path/to/keyfile.json"] do
Expand All @@ -508,6 +509,7 @@ def creds.is_a? target
bigtable = Google::Cloud::Bigtable.new
_(bigtable).must_be_kind_of Google::Cloud::Bigtable::Project
_(bigtable.project_id).must_equal "project-id"
_(bigtable.universe_domain).must_equal "myuniverse.com"
_(bigtable.service).must_be_kind_of OpenStruct
end
end
Expand All @@ -522,7 +524,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_equal 42
Expand Down Expand Up @@ -561,7 +563,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(host).must_equal "bigtable-endpoint2.example.com"
Expand Down Expand Up @@ -600,7 +602,7 @@ def creds.is_a? target
_(scope).must_equal default_scope
"bigtable-credentials"
}
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil|
stubbed_service = lambda { |project_id, credentials, timeout: nil, host: nil, host_admin: nil, channel_selection: nil, channel_count: nil, universe_domain: nil|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(channel_selection).must_equal :least_loaded
Expand Down

0 comments on commit 5ce2e60

Please sign in to comment.