Skip to content

Commit

Permalink
feat(storage): quota_project can be set via library configuration (#7656
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dazuma committed Sep 15, 2020
1 parent 115311c commit 4864293
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions google-cloud-storage/lib/google-cloud-storage.rb
Expand Up @@ -136,6 +136,7 @@ def self.storage project_id = nil, credentials = nil, scope: nil,
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
# TODO: Remove once discovery document is updated.
Expand Down
3 changes: 2 additions & 1 deletion google-cloud-storage/lib/google/cloud/storage.rb
Expand Up @@ -93,7 +93,8 @@ def self.new project_id: nil, credentials: nil, scope: nil, retries: nil,
Storage::Project.new(
Storage::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
3 changes: 2 additions & 1 deletion google-cloud-storage/lib/google/cloud/storage/service.rb
Expand Up @@ -39,7 +39,7 @@ class Service
##
# Creates a new Service instance.
def initialize project, credentials,
retries: nil, timeout: nil, host: nil
retries: nil, timeout: nil, host: nil, quota_project: nil
@project = project
@credentials = credentials
@service = API::StorageService.new
Expand All @@ -55,6 +55,7 @@ def initialize project, credentials,
@service.request_options.header["x-goog-api-client"] = \
"gl-ruby/#{RUBY_VERSION} gccl/#{Google::Cloud::Storage::VERSION}"
@service.request_options.header["Accept-Encoding"] = "gzip"
@service.request_options.quota_project = quota_project if quota_project
@service.authorization = @credentials.client if @credentials
@service.root_url = host if host
end
Expand Down
63 changes: 52 additions & 11 deletions google-cloud-storage/test/google/cloud/storage_test.rb
Expand Up @@ -100,7 +100,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"storage-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 "storage-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -130,7 +130,7 @@ def creds.is_a? target

describe "Storage.anonymous" do
it "uses provided options" 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_be :nil?
_(credentials).must_be :nil?
_(retries).must_equal 5
Expand Down Expand Up @@ -176,7 +176,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 @@ -204,7 +204,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"storage-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 "storage-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -237,7 +237,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"storage-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 "storage-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -270,7 +270,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 @@ -315,7 +315,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"storage-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 "storage-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -354,7 +354,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"storage-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 "storage-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -393,7 +393,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"storage-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 "storage-credentials"
_(retries).must_equal 3
Expand Down Expand Up @@ -434,7 +434,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"storage-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 "storage-credentials"
_(retries).must_equal 3
Expand Down Expand Up @@ -473,7 +473,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"storage-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 "storage-credentials"
_(retries).must_equal 3
Expand Down Expand Up @@ -507,5 +507,46 @@ def creds.is_a? target
end
end
end

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

# Clear all environment variables
ENV.stub :[], nil do
# Set new configuration
Google::Cloud::Storage.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::Storage::Credentials.stub :new, stubbed_credentials do
Google::Cloud::Storage::Service.stub :new, stubbed_service do
storage = Google::Cloud::Storage.new
_(storage).must_be_kind_of Google::Cloud::Storage::Project
_(storage.project).must_equal "project-id"
_(storage.service).must_be_kind_of OpenStruct
end
end
end
end
end
end
end
end

0 comments on commit 4864293

Please sign in to comment.