Skip to content

Commit

Permalink
Implement getZoneDistribution
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Oct 30, 2017
1 parent 3432077 commit 2aa20af
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/dnsimple/client/zones.rb
Expand Up @@ -94,6 +94,23 @@ def zone_file(account_id, zone_name, options = {})
Dnsimple::Response.new(response, Struct::ZoneFile.new(response["data"]))
end

# Checks if a zone is fully distributed across DNSimple nodes
#
# @see https://developer.dnsimple.com/v2/zones/#get-zone-resolution
#
# @param [Integer] account_id the account ID
# @param [#to_s] zone_id the zone name
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::ZoneDistribution>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def zone_distribution(account_id, zone_id, options = {})
response = client.get(Client.versioned("/%s/zones/%s/distribution" % [account_id, zone_id]), options)

Dnsimple::Response.new(response, Struct::ZoneDistribution.new(response["data"]))
end

end
end
end
1 change: 1 addition & 0 deletions lib/dnsimple/struct.rb
Expand Up @@ -39,5 +39,6 @@ def initialize(attributes = {})
require_relative 'struct/whois_privacy'
require_relative 'struct/zone'
require_relative 'struct/zone_file'
require_relative 'struct/zone_distribution'
require_relative 'struct/webhook'
require_relative 'struct/whoami'
10 changes: 10 additions & 0 deletions lib/dnsimple/struct/zone_distribution.rb
@@ -0,0 +1,10 @@
module Dnsimple
module Struct

class ZoneDistribution < Base
# @return [Boolean] The result of the check.
attr_accessor :distributed
end

end
end
58 changes: 58 additions & 0 deletions spec/dnsimple/client/zones_spec.rb
Expand Up @@ -171,4 +171,62 @@
end
end

describe "#zone_distribution" do
let(:account_id) { 1010 }

before do
stub_request(:get, %r{/v2/#{account_id}/zones/.+$})
.to_return(read_http_fixture("getZoneDistribution/success.http"))
end

it "builds the correct request" do
subject.zone_distribution(account_id, zone = "example.com")

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/zones/#{zone}/distribution")
.with(headers: { 'Accept' => 'application/json' })
end

it "returns the zone distribution check with true when the zone is fully distributed" do
response = subject.zone_distribution(account_id, "example.com")
expect(response).to be_a(Dnsimple::Response)

result = response.data
expect(result).to be_a(Dnsimple::Struct::ZoneDistribution)
expect(result.distributed).to be(true)
end

it "returns the zone distribution check with false when the zone isn't fully distributed" do
stub_request(:get, %r{/v2/#{account_id}/zones/.+$})
.to_return(read_http_fixture("getZoneDistribution/failure.http"))

response = subject.zone_distribution(account_id, "example.com")
expect(response).to be_a(Dnsimple::Response)

result = response.data
expect(result).to be_a(Dnsimple::Struct::ZoneDistribution)
expect(result.distributed).to be(false)
end

it "raises an error when the server wasn't able to complete the check" do
stub_request(:get, %r{/v2/#{account_id}/zones/.+$})
.to_return(read_http_fixture("getZoneDistribution/error.http"))

expect {
subject.zone_distribution(account_id, "example.com")
}.to raise_error(Dnsimple::RequestError, "Could not query zone, connection timed out")
end

context "when the zone does not exist" do
it "raises NotFoundError" do
stub_request(:get, %r{/v2})
.to_return(read_http_fixture("notfound-zone.http"))

expect {
subject.zone_distribution(account_id, "example.com")
}.to raise_error(Dnsimple::NotFoundError)
end
end
end


end
21 changes: 21 additions & 0 deletions spec/fixtures.http/getZoneDistribution/error.http
@@ -0,0 +1,21 @@
HTTP/1.1 500 Internal Server Error
Server: nginx
Date: Mon, 30 Oct 2017 09:09:41 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-RateLimit-Limit: 2400
X-RateLimit-Remaining: 2399
X-RateLimit-Reset: 1509358181
ETag: W/"10fc650d019e6bffa876f08bce8f3380"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 91f124fc-671d-40fa-877b-0b222692f623
X-Runtime: 0.471005
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: DENY
X-Permitted-Cross-Domain-Policies: none
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000

{"message":"Could not query zone, connection timed out"}
21 changes: 21 additions & 0 deletions spec/fixtures.http/getZoneDistribution/failure.http
@@ -0,0 +1,21 @@
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 30 Oct 2017 09:09:41 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-RateLimit-Limit: 2400
X-RateLimit-Remaining: 2399
X-RateLimit-Reset: 1509358181
ETag: W/"10fc650d019e6bffa876f08bce8f3380"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 91f124fc-671d-40fa-877b-0b222692f623
X-Runtime: 0.471005
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: DENY
X-Permitted-Cross-Domain-Policies: none
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000

{"data":{"distributed":false}}
21 changes: 21 additions & 0 deletions spec/fixtures.http/getZoneDistribution/success.http
@@ -0,0 +1,21 @@
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 30 Oct 2017 09:09:41 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-RateLimit-Limit: 2400
X-RateLimit-Remaining: 2399
X-RateLimit-Reset: 1509358181
ETag: W/"10fc650d019e6bffa876f08bce8f3380"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 91f124fc-671d-40fa-877b-0b222692f623
X-Runtime: 0.471005
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: DENY
X-Permitted-Cross-Domain-Policies: none
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000

{"data":{"distributed":true}}

0 comments on commit 2aa20af

Please sign in to comment.