Skip to content

Commit

Permalink
feat(resource_manager): quota_project can be set via library configur…
Browse files Browse the repository at this point in the history
…ation (#7629)
  • Loading branch information
dazuma committed Sep 15, 2020
1 parent 3deb7d2 commit 400a4ed
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def self.resource_manager credentials = nil, scope: nil, retries: 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
config.add_field! :endpoint, nil, match: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def self.new credentials: nil, scope: nil, retries: nil, timeout: nil,

ResourceManager::Manager.new(
ResourceManager::Service.new(
credentials, retries: retries, timeout: timeout, host: endpoint
credentials, retries: retries, timeout: timeout, host: endpoint, quota_project: configure.quota_project
)
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Service

##
# Creates a new Service instance.
def initialize credentials, retries: nil, timeout: nil, host: nil
def initialize credentials, retries: nil, timeout: nil, host: nil, quota_project: nil
@credentials = credentials
@service = API::CloudResourceManagerService.new
@service.client_options.application_name = \
Expand All @@ -48,6 +48,7 @@ def initialize credentials, retries: nil, timeout: nil, host: nil
@service.request_options.header["x-goog-api-client"] = \
"gl-ruby/#{RUBY_VERSION} " \
"gccl/#{Google::Cloud::ResourceManager::VERSION}"
@service.request_options.quota_project = quota_project if quota_project
@service.authorization = @credentials.client
@service.root_url = host if host
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def creds.is_a? target
scope.must_be :nil?
"resource_manager-credentials"
}
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
credentials.must_equal "resource_manager-credentials"
retries.must_be :nil?
timeout.must_be :nil?
Expand Down Expand Up @@ -145,7 +145,7 @@ def creds.is_a? target
scope.must_be :nil?
"resource_manager-credentials"
}
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
credentials.must_equal "resource_manager-credentials"
retries.must_be :nil?
timeout.must_be :nil?
Expand Down Expand Up @@ -175,7 +175,7 @@ def creds.is_a? target
scope.must_be :nil?
"resource_manager-credentials"
}
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
credentials.must_equal "resource_manager-credentials"
retries.must_be :nil?
timeout.must_be :nil?
Expand All @@ -200,7 +200,7 @@ def creds.is_a? target
end

it "uses provided endpoint" do
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
credentials.must_equal default_credentials
retries.must_be :nil?
timeout.must_be :nil?
Expand Down Expand Up @@ -232,7 +232,7 @@ def creds.is_a? target
scope.must_be :nil?
"resource_manager-credentials"
}
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
credentials.must_equal "resource_manager-credentials"
retries.must_be :nil?
timeout.must_be :nil?
Expand Down Expand Up @@ -268,7 +268,7 @@ def creds.is_a? target
scope.must_be :nil?
"resource_manager-credentials"
}
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
credentials.must_equal "resource_manager-credentials"
retries.must_be :nil?
timeout.must_be :nil?
Expand Down Expand Up @@ -304,7 +304,7 @@ def creds.is_a? target
scope.must_be :nil?
"resource_manager-credentials"
}
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
credentials.must_equal "resource_manager-credentials"
retries.must_equal 3
timeout.must_equal 42
Expand Down Expand Up @@ -341,7 +341,7 @@ def creds.is_a? target
scope.must_be :nil?
"resource_manager-credentials"
}
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
credentials.must_equal "resource_manager-credentials"
retries.must_equal 3
timeout.must_equal 42
Expand Down Expand Up @@ -378,7 +378,7 @@ def creds.is_a? target
scope.must_be :nil?
"resource_manager-credentials"
}
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil) {
stubbed_service = ->(credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
credentials.must_equal "resource_manager-credentials"
retries.must_equal 3
timeout.must_equal 42
Expand Down Expand Up @@ -409,5 +409,43 @@ def creds.is_a? target
end
end
end

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

# Clear all environment variables
ENV.stub :[], nil do
# Set new configurations
Google::Cloud::ResourceManager.configure do |config|
config.credentials = "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::ResourceManager::Credentials.stub :new, stubbed_credentials do
Google::Cloud::ResourceManager::Service.stub :new, stubbed_service do
resource_manager = Google::Cloud::ResourceManager.new
resource_manager.must_be_kind_of Google::Cloud::ResourceManager::Manager
resource_manager.service.must_equal "resource-manager-service"
end
end
end
end
end
end
end
end

0 comments on commit 400a4ed

Please sign in to comment.