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(bigquery): quota_project can be set via library configuration #7627

Merged
merged 1 commit into from Sep 15, 2020
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
1 change: 1 addition & 0 deletions google-cloud-bigquery/lib/google-cloud-bigquery.rb
Expand Up @@ -133,6 +133,7 @@ def self.bigquery project_id = nil, credentials = nil, scope: nil, retries: nil,
config.add_field! :credentials, default_creds, match: [String, Hash, Google::Auth::Credentials], allow_nil: true
config.add_alias! :keyfile, :credentials
config.add_field! :scope, nil, match: [String, Array]
config.add_field! :quota_project, nil, match: String
config.add_field! :retries, nil, match: Integer
config.add_field! :timeout, nil, match: Integer
config.add_field! :endpoint, nil, match: String
Expand Down
2 changes: 1 addition & 1 deletion google-cloud-bigquery/lib/google/cloud/bigquery.rb
Expand Up @@ -84,7 +84,7 @@ def self.new project_id: nil, credentials: nil, scope: nil, retries: nil, timeou
Bigquery::Project.new(
Bigquery::Service.new(
project_id, credentials,
retries: retries, timeout: timeout, host: endpoint
retries: retries, timeout: timeout, host: endpoint, quota_project: configure.quota_project
)
)
end
Expand Down
4 changes: 3 additions & 1 deletion google-cloud-bigquery/lib/google/cloud/bigquery/service.rb
Expand Up @@ -43,12 +43,13 @@ class Service

##
# Creates a new Service instance.
def initialize project, credentials, retries: nil, timeout: nil, host: nil
def initialize project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil
@project = project
@credentials = credentials
@retries = retries
@timeout = timeout
@host = host
@quota_project = quota_project
end

def service
Expand All @@ -64,6 +65,7 @@ def service
service.request_options.header ||= {}
service.request_options.header["x-goog-api-client"] = \
"gl-ruby/#{RUBY_VERSION} gccl/#{Google::Cloud::Bigquery::VERSION}"
service.request_options.quota_project = @quota_project if @quota_project
service.authorization = @credentials.client
service.root_url = host if host
service
Expand Down
62 changes: 52 additions & 10 deletions google-cloud-bigquery/test/google/cloud/bigquery_test.rb
Expand Up @@ -100,7 +100,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"bigquery-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "bigquery-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -153,7 +153,7 @@ def creds.is_a? target
end

it "uses provided endpoint" do
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal default_credentials
_(retries).must_be :nil?
Expand Down Expand Up @@ -181,7 +181,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"bigquery-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "bigquery-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -213,7 +213,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"bigquery-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "bigquery-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -245,7 +245,7 @@ def creds.is_a? target
_(scope).must_be :nil?
OpenStruct.new project_id: "project-id"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_be_kind_of OpenStruct
_(credentials.project_id).must_equal "project-id"
Expand Down Expand Up @@ -289,7 +289,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"bigquery-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "bigquery-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -327,7 +327,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"bigquery-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "bigquery-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -365,7 +365,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"bigquery-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "bigquery-credentials"
_(retries).must_equal 3
Expand Down Expand Up @@ -405,7 +405,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"bigquery-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "bigquery-credentials"
_(retries).must_equal 3
Expand Down Expand Up @@ -445,7 +445,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"bigquery-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "bigquery-credentials"
_(retries).must_equal 3
Expand Down Expand Up @@ -479,5 +479,47 @@ def creds.is_a? target
end
end
end

it "uses bigquery config for quota project" do
stubbed_credentials = ->(keyfile, scope: nil) {
_(keyfile).must_equal "path/to/keyfile.json"
_(scope).must_be :nil?
"bigquery-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "bigquery-credentials"
_(retries).must_equal 3
_(timeout).must_equal 42
_(host).must_be :nil?
_(quota_project).must_equal "project-id-2"
OpenStruct.new project: project
}

# Clear all environment variables
ENV.stub :[], nil do
# Set new configuration
Google::Cloud::Bigquery.configure do |config|
config.project = "project-id"
config.keyfile = "path/to/keyfile.json"
config.retries = 3
config.timeout = 42
config.quota_project = "project-id-2"
end

File.stub :file?, true, ["path/to/keyfile.json"] do
File.stub :read, found_credentials, ["path/to/keyfile.json"] do
Google::Cloud::Bigquery::Credentials.stub :new, stubbed_credentials do
Google::Cloud::Bigquery::Service.stub :new, stubbed_service do
bigquery = Google::Cloud::Bigquery.new
_(bigquery).must_be_kind_of Google::Cloud::Bigquery::Project
_(bigquery.project).must_equal "project-id"
_(bigquery.service).must_be_kind_of OpenStruct
end
end
end
end
end
end
end
end