Skip to content

Commit

Permalink
feat(dns): quota_project can be set via library configuration (#7628)
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuma committed Sep 15, 2020
1 parent 400a4ed commit 651cedd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions google-cloud-dns/lib/google-cloud-dns.rb
Expand Up @@ -130,6 +130,7 @@ def self.dns project_id = nil, 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
2 changes: 1 addition & 1 deletion google-cloud-dns/lib/google/cloud/dns.rb
Expand Up @@ -92,7 +92,7 @@ def self.new project_id: nil, credentials: nil, scope: nil, retries: nil,
Dns::Project.new(
Dns::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-dns/lib/google/cloud/dns/service.rb
Expand Up @@ -34,7 +34,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::DnsService.new
Expand All @@ -48,6 +48,7 @@ def initialize project, credentials,
@service.request_options.header ||= {}
@service.request_options.header["x-goog-api-client"] = \
"gl-ruby/#{RUBY_VERSION} gccl/#{Google::Cloud::Dns::VERSION}"
@service.request_options.quota_project = quota_project if quota_project
@service.authorization = @credentials.client
@service.root_url = host if host
end
Expand Down
61 changes: 51 additions & 10 deletions google-cloud-dns/test/google/cloud/dns_test.rb
Expand Up @@ -100,7 +100,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"dns-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 "dns-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -158,7 +158,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"dns-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 "dns-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -190,7 +190,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"dns-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 "dns-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -222,7 +222,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 @@ -253,7 +253,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 @@ -289,7 +289,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"dns-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 "dns-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -327,7 +327,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"dns-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 "dns-credentials"
_(retries).must_be :nil?
Expand Down Expand Up @@ -365,7 +365,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"dns-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 "dns-credentials"
_(retries).must_equal 3
Expand Down Expand Up @@ -405,7 +405,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"dns-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 "dns-credentials"
_(retries).must_equal 3
Expand Down Expand Up @@ -445,7 +445,7 @@ def creds.is_a? target
_(scope).must_be :nil?
"dns-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 "dns-credentials"
_(retries).must_equal 3
Expand Down Expand Up @@ -479,5 +479,46 @@ def creds.is_a? target
end
end
end

it "uses dns config for quota project" do
stubbed_credentials = ->(keyfile, scope: nil) {
_(keyfile).must_equal "path/to/keyfile.json"
_(scope).must_be :nil?
"dns-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "dns-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 configurations
Google::Cloud::Dns.configure do |config|
config.project_id = "project-id"
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::Dns::Credentials.stub :new, stubbed_credentials do
Google::Cloud::Dns::Service.stub :new, stubbed_service do
dns = Google::Cloud::Dns.new
_(dns).must_be_kind_of Google::Cloud::Dns::Project
_(dns.project).must_equal "project-id"
_(dns.service).must_be_kind_of OpenStruct
end
end
end
end
end
end
end
end

0 comments on commit 651cedd

Please sign in to comment.