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(bigtable): Support for setting the universe domain #29173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 @@ -52,11 +52,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 @@ -79,6 +80,7 @@ module Bigtable
#
def self.new project_id: nil,
credentials: nil,
universe_domain: nil,
emulator_host: nil,
scope: nil,
endpoint: nil,
Expand All @@ -87,6 +89,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 @@ -101,8 +104,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
9 changes: 9 additions & 0 deletions google-cloud-bigtable/lib/google/cloud/bigtable/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ def table_admin_client
service.tables
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
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
50 changes: 17 additions & 33 deletions google-cloud-bigtable/test/google/cloud/bigtable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,10 @@ 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, **_other_keywords|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
_(host).must_equal default_host
_(host_admin).must_equal default_host_admin
OpenStruct.new(project_id: project_id)
}

Expand Down Expand Up @@ -177,7 +175,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, **_other_keywords|
_(credentials).must_equal default_credentials
_(timeout).must_be :nil?
_(host).must_equal "bigtable-endpoint2.example.com"
Expand Down Expand Up @@ -205,12 +203,10 @@ 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, **_other_keywords|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
_(host).must_equal default_host
_(host_admin).must_equal default_host_admin
OpenStruct.new project_id: project_id
}

Expand All @@ -237,12 +233,10 @@ 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, **_other_keywords|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
_(host).must_equal default_host
_(host_admin).must_equal default_host_admin
OpenStruct.new project_id: project_id
}

Expand All @@ -269,12 +263,10 @@ 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, channel_selection: nil, channel_count: nil, **_other_keywords|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
_(host).must_equal default_host
_(host_admin).must_equal default_host_admin
_(channel_selection).must_equal :least_loaded
_(channel_count).must_equal 5
OpenStruct.new project_id: project_id
Expand Down Expand Up @@ -339,13 +331,11 @@ 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, **_other_keywords|
_(project_id).must_equal "project-id"
_(credentials).must_be_kind_of OpenStruct
_(credentials.project_id).must_equal "project-id"
_(timeout).must_be :nil?
_(host).must_equal default_host
_(host_admin).must_equal default_host_admin
OpenStruct.new project_id: project_id
}
empty_env = OpenStruct.new
Expand All @@ -370,7 +360,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, channel_selection: nil, channel_count: nil, **_other_keywords|
_(credentials).must_equal default_credentials
_(timeout).must_be :nil?
_(channel_selection).must_equal :least_loaded
Expand Down Expand Up @@ -406,12 +396,10 @@ 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, **_other_keywords|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
_(host).must_equal default_host
_(host_admin).must_equal default_host_admin

OpenStruct.new project_id: project_id
}
Expand Down Expand Up @@ -445,12 +433,10 @@ 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, **_other_keywords|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(timeout).must_be :nil?
_(host).must_equal default_host
_(host_admin).must_equal default_host_admin
OpenStruct.new project_id: project_id
}

Expand All @@ -477,19 +463,17 @@ 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, universe_domain: nil, **_other_keywords|
_(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 +483,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 +493,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,12 +508,10 @@ 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, **_other_keywords|
_(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
}

Expand Down Expand Up @@ -561,7 +545,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, **_other_keywords|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(host).must_equal "bigtable-endpoint2.example.com"
Expand Down Expand Up @@ -600,7 +584,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, channel_selection: nil, channel_count: nil, **_other_keywords|
_(project_id).must_equal "project-id"
_(credentials).must_equal "bigtable-credentials"
_(channel_selection).must_equal :least_loaded
Expand Down
Loading