Skip to content
Browse files

initial work on dnsimple domain requests

  • Loading branch information...
1 parent 8fdab3e commit 70a7647d5b8ae71d77fa7584c2be9583b0fcb91d @dje dje committed Feb 23, 2011
View
1 lib/fog/bin.rb
@@ -66,3 +66,4 @@ def collections
require 'fog/bin/slicehost'
require 'fog/bin/terremark'
require 'fog/bin/zerigo'
+require 'fog/bin/dnsimple'
View
30 lib/fog/bin/dnsimple.rb
@@ -0,0 +1,30 @@
+class DNSimple < Fog::Bin
+ class << self
+
+ def class_for(key)
+ case key
+ when :dns
+ Fog::DNSimple::DNS
+ else
+ raise ArgumentError, "Unrecognized service: #{key}"
+ end
+ end
+
+ def [](service)
+ @@connections ||= Hash.new do |hash, key|
+ hash[key] = case key
+ when :dns
+ Fog::DNS.new(:provider => 'DNSimple')
+ else
+ raise ArgumentError, "Unrecognized service: #{key.inspect}"
+ end
+ end
+ @@connections[service]
+ end
+
+ def services
+ Fog::DNSimple.services
+ end
+
+ end
+end
View
3 lib/fog/dns.rb
@@ -16,6 +16,9 @@ def self.new(attributes)
when 'Zerigo'
require 'fog/dns/zerigo'
Fog::Zerigo::DNS.new(attributes)
+ when 'DNSimple'
+ require 'fog/dns/dnsimple'
+ Fog::DNSimple::DNS.new(attributes)
else
raise ArgumentError.new("#{provider} is not a recognized dns provider")
end
View
59 lib/fog/dns/dnsimple.rb
@@ -0,0 +1,59 @@
+module Fog
+ module DNSimple
+ class DNS < Fog::Service
+
+ requires :dnsimple_email, :dnsimple_password
+ recognizes :provider # remove post deprecation
+
+ request_path 'fog/dns/requests/dnsimple'
+ request :list_domains
+ request :create_domain
+ request :get_domain
+ request :delete_domain
+
+ class Mock
+ # TODO
+ end
+
+ class Real
+
+ def initialize(options={})
+ unless options.delete(:provider)
+ location = caller.first
+ warning = "[yellow][WARN] Fog::DNSimple::DNS.new is deprecated, use Fog::DNS.new(:provider => 'DNSimple') instead[/]"
+ warning << " [light_black](" << location << ")[/] "
+ Formatador.display_line(warning)
+ end
+
+ require 'json'
+
+ @dnsimple_email = options[:dnsimple_email]
+ @dnsimple_password = options[:dnsimple_password]
+ @host = options[:host] || "test.dnsimple.com"
+ @port = options[:port] || 443
+ @scheme = options[:scheme] || 'https'
+ @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
+ end
+
+ def reload
+ @connection.reset
+ end
+
+ def request(params)
+ params[:headers] ||= {}
+ key = "#{@dnsimple_email}:#{@dnsimple_password}"
+ params[:headers].merge!({ "Authorization" => "Basic " + Base64.encode64(key).chomp,
+ "Accept" => "application/json",
+ "Content-Type" => "application/json" })
+
+ response = @connection.request(params.merge!({:host => @host}))
+
+ unless response.body.empty?
+ response.body = JSON.parse(response.body)
+ end
+ response
+ end
+ end
+ end
+ end
+end
View
27 lib/fog/dns/requests/dnsimple/create_domain.rb
@@ -0,0 +1,27 @@
+module Fog
+ module DNSimple
+ class DNS
+ class Real
+
+ # Create a single domain in DNSimple in your account.
+ # ==== Parameters
+ # * name<~String> - domain name to host (ie example.com)
+ #
+ # ==== Returns
+ # * response<~Excon::Response>:
+ # * body<~Hash>:
+ # * 'name'<~String>
+ def create_domain(name)
+ body = { "domain" => { "name" => name } }
+ request(
+ :body => body.to_json,
+ :expects => 201,
+ :method => 'POST',
+ :path => '/domains'
+ )
+ end
+
+ end
+ end
+ end
+end
View
26 lib/fog/dns/requests/dnsimple/delete_domain.rb
@@ -0,0 +1,26 @@
+module Fog
+ module DNSimple
+ class DNS
+ class Real
+
+ # Delete the given domain from your account. You may use
+ # either the domain ID or the domain name.
+ #
+ # Please note that for domains which are registered with
+ # DNSimple this will not delete the domain from the registry.
+ #
+ # ==== Parameters
+ # * name<~String> - domain name or numeric ID
+ #
+ def delete_domain(name)
+ request(
+ :expects => 200,
+ :method => 'DELETE',
+ :path => "/domains/#{name}"
+ )
+ end
+
+ end
+ end
+ end
+end
View
37 lib/fog/dns/requests/dnsimple/get_domain.rb
@@ -0,0 +1,37 @@
+module Fog
+ module DNSimple
+ class DNS
+ class Real
+
+ # Get the details for a specific domain in your account. You
+ # may pass either the domain numeric ID or the domain name
+ # itself.
+ #
+ # ==== Parameters
+ # * id<~String> - domain name or numeric ID
+ #
+ # ==== Returns
+ # * response<~Excon::Response>:
+ # * body<~Hash>:
+ # * 'domain'<~Hash>
+ # * 'name'<~String>
+ # * 'expires_at'<~String>
+ # * 'created_at'<~String>
+ # * 'registration_status'<~String>
+ # * 'updated_at'<~String>
+ # * 'registrant_id'<~Integer>
+ # * 'id'<~Integer>
+ # * 'user_id'<~Integer>
+ # * 'name_server_status'<~String>
+ def get_domain(id)
+ request(
+ :expects => 200,
+ :method => 'GET',
+ :path => '/domains/#{id}'
+ )
+ end
+
+ end
+ end
+ end
+end
View
35 lib/fog/dns/requests/dnsimple/list_domains.rb
@@ -0,0 +1,35 @@
+module Fog
+ module DNSimple
+ class DNS
+ class Real
+
+ # Get the details for a specific domain in your account. You
+ # may pass either the domain numeric ID or the domain name itself.
+ # ==== Parameters
+ # * id<~String> - domain name or numeric ID
+ #
+ # ==== Returns
+ # * response<~Excon::Response>:
+ # * body<~Hash>:
+ # * 'domains'<~Array>
+ # * 'name'<~String>
+ # * 'expires_at'<~String>
+ # * 'created_at'<~String>
+ # * 'registration_status'<~String>
+ # * 'updated_at'<~String>
+ # * 'registrant_id'<~Integer>
+ # * 'id'<~Integer>
+ # * 'user_id'<~Integer>
+ # * 'name_server_status'<~String>
+ def list_domains
+ request(
+ :expects => 200,
+ :method => 'GET',
+ :path => '/domains'
+ )
+ end
+
+ end
+ end
+ end
+end
View
1 lib/fog/providers.rb
@@ -18,3 +18,4 @@ def self.providers
require 'fog/providers/rackspace'
require 'fog/providers/slicehost'
require 'fog/providers/zerigo'
+require 'fog/providers/dnsimple'
View
11 lib/fog/providers/dnsimple.rb
@@ -0,0 +1,11 @@
+require 'fog/core'
+
+module Fog
+ module DNSimple
+
+ extend Fog::Provider
+
+ service(:dns, 'dns/dnsimple')
+
+ end
+end
View
5 tests/dns/helper.rb
@@ -14,6 +14,9 @@ def dns_providers
},
Zerigo => {
:mocked => false
+ },
+ DNSimple => {
+ :mocked => false
}
}
-end
+end
View
75 tests/dns/requests/dnsimple/dns_tests.rb
@@ -0,0 +1,75 @@
+Shindo.tests('DNSimple::dns | DNS requests', ['dnsimple', 'dns']) do
+
+ @domain = ''
+ @domain_count = 0
+ @new_domains = []
+ @new_records = []
+
+ def generate_unique_domain( with_trailing_dot = false)
+ #get time (with 1/100th of sec accuracy)
+ #want unique domain name and if provider is fast, this can be called more than once per second
+ time= (Time.now.to_f * 100).to_i
+ domain = 'test-' + time.to_s + '.com'
+ if with_trailing_dot
+ domain+= '.'
+ end
+
+ domain
+ end
+
+ tests( 'success') do
+
+ test('get current domain count') do
+ pending if Fog.mocking?
+
+ response = DNSimple[:dns].list_domains()
+ if response.status == 200
+ @domain_count = response.body.size
+ end
+
+ response.status == 200
+ end
+
+ test('create domain') do
+ pending if Fog.mocking?
+
+ domain = generate_unique_domain
+ response = DNSimple[:dns].create_domain(domain)
+ if response.status == 201
+ @new_domains << response.body
+ end
+
+ response.status == 201
+ end
+
+ test('get domain by id') do
+ pending if Fog.mocking?
+
+ id = @new_domains.first["domain"]["id"]
+ response = DNSimple[:dns].get_domain(id)
+
+ response.status == 200
+ end
+
+ test("delete #{@new_domains.count} domains created") do
+ pending if Fog.mocking?
+
+ result = true
+
+ @new_domains.each do |domain|
+ name = domain["domain"]["name"]
+ response = DNSimple[:dns].delete_domain(name)
+ if response.status != 200
+ result= false;
+ end
+ end
+
+ result
+ end
+
+ end
+
+ tests( 'failure') do
+ end
+
+end
View
2 tests/helper.rb
@@ -8,7 +8,7 @@ def lorem_file
end
# check to see which credentials are available and add others to the skipped tags list
-all_providers = ['aws', 'bluebox', 'brightbox', 'gogrid', 'google', 'linode', 'local', 'newservers', 'rackspace', 'slicehost', 'terremarkecloud', 'zerigo']
+all_providers = ['aws', 'bluebox', 'brightbox', 'gogrid', 'google', 'linode', 'local', 'newservers', 'rackspace', 'slicehost', 'terremarkecloud', 'zerigo', 'dnsimple']
available_providers = Fog.providers.map {|provider| provider.downcase}
for provider in (all_providers - available_providers)
Formatador.display_line("[yellow]Skipping tests for [bold]#{provider}[/] [yellow]due to lacking credentials (add some to '~/.fog' to run them)[/]")

0 comments on commit 70a7647

Please sign in to comment.
Something went wrong with that request. Please try again.