diff --git a/google-cloud-dns/lib/google-cloud-dns.rb b/google-cloud-dns/lib/google-cloud-dns.rb index 954f81845d3e..0d394265cdf7 100644 --- a/google-cloud-dns/lib/google-cloud-dns.rb +++ b/google-cloud-dns/lib/google-cloud-dns.rb @@ -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 diff --git a/google-cloud-dns/lib/google/cloud/dns.rb b/google-cloud-dns/lib/google/cloud/dns.rb index 4bc1279739f3..e595e402bad7 100644 --- a/google-cloud-dns/lib/google/cloud/dns.rb +++ b/google-cloud-dns/lib/google/cloud/dns.rb @@ -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 diff --git a/google-cloud-dns/lib/google/cloud/dns/service.rb b/google-cloud-dns/lib/google/cloud/dns/service.rb index 1a94e932be62..a04301bc66b5 100644 --- a/google-cloud-dns/lib/google/cloud/dns/service.rb +++ b/google-cloud-dns/lib/google/cloud/dns/service.rb @@ -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 @@ -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 diff --git a/google-cloud-dns/test/google/cloud/dns_test.rb b/google-cloud-dns/test/google/cloud/dns_test.rb index 6deee9cde774..7b4c86c5e11c 100644 --- a/google-cloud-dns/test/google/cloud/dns_test.rb +++ b/google-cloud-dns/test/google/cloud/dns_test.rb @@ -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? @@ -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? @@ -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? @@ -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" @@ -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? @@ -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? @@ -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? @@ -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 @@ -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 @@ -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 @@ -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