initial work on dnsimple domain requests

This commit is contained in:
Darrin Eden 2011-02-23 19:35:05 -08:00
parent 8fdab3e8b4
commit 70a7647d5b
13 changed files with 310 additions and 2 deletions

View File

@ -66,3 +66,4 @@ require 'fog/bin/rackspace'
require 'fog/bin/slicehost'
require 'fog/bin/terremark'
require 'fog/bin/zerigo'
require 'fog/bin/dnsimple'

30
lib/fog/bin/dnsimple.rb Normal file
View File

@ -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 File

@ -16,6 +16,9 @@ module Fog
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

59
lib/fog/dns/dnsimple.rb Normal file
View File

@ -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 File

@ -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 File

@ -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 File

@ -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 File

@ -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 File

@ -18,3 +18,4 @@ require 'fog/providers/new_servers'
require 'fog/providers/rackspace'
require 'fog/providers/slicehost'
require 'fog/providers/zerigo'
require 'fog/providers/dnsimple'

View File

@ -0,0 +1,11 @@
require 'fog/core'
module Fog
module DNSimple
extend Fog::Provider
service(:dns, 'dns/dnsimple')
end
end

View File

@ -14,6 +14,9 @@ def dns_providers
},
Zerigo => {
:mocked => false
},
DNSimple => {
:mocked => false
}
}
end
end

View File

@ -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 File

@ -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)[/]")