1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[rackspace|dns] zone models

This commit is contained in:
Brian Hartsock 2011-08-27 22:06:46 -04:00
parent 7ac6b2069f
commit d6654e736c
9 changed files with 140 additions and 23 deletions

View file

@ -0,0 +1,74 @@
require 'fog/core/model'
#require 'fog/dns/models/rackspace/records'
module Fog
module DNS
class Rackspace
class Zone < Fog::Model
identity :id
attribute :email, :aliases => 'emailAddress'
attribute :domain, :aliases => 'name'
attribute :created
attribute :updated
attribute :account_id, :aliases => 'accountId'
attribute :ttl
attribute :nameservers
attribute :comment
def destroy
response = connection.delete_domain(identity)
wait_for_job response.body['jobId'], Fog.timeout
true
end
def save
if identity
update
else
create
end
true
end
private
def wait_for_job(job_id, timeout=Fog.timeout, interval=1)
retries = 5
response = nil
Fog.wait_for(timeout, interval) do
response = connection.callback job_id
if response.status != 202
true
elsif retries == 0
raise Fog::Errors::Error.new("Wait on job #{job_id} took too long")
else
retries -= 1
false
end
end
response
end
def create
requires :domain
data = { :name => domain, :email => email }
response = connection.create_domains([data])
response = wait_for_job response.body['jobId']
merge_attributes(response.body['domains'].first)
end
def update
requires :ttl, :email
response = connection.modify_domain(identity, { :ttl => ttl, :comment => comment, :email => email})
wait_for_job response.body['jobId']
end
end
end
end
end

View file

@ -0,0 +1,33 @@
require 'fog/core/collection'
require 'fog/dns/models/rackspace/zone'
module Fog
module DNS
class Rackspace
class Zones < Fog::Collection
model Fog::DNS::Rackspace::Zone
def all
clear
data = connection.list_domains.body['domains']
load(data)
end
def get(zone_id)
if zone_id.nil? or zone_id.to_s.empty?
return nil
end
data = connection.list_domain_details(zone_id).body
new(data)
rescue Fog::Rackspace::Errors::NotFound
nil
#Accessing a valid (but other customer's) id returns a 503 error
rescue Fog::Rackspace::Errors::ServiceUnavailable
nil
end
end
end
end
end

View file

@ -10,11 +10,10 @@ module Fog
recognizes :rackspace_auth_token
model_path 'fog/dns/models/rackspace'
#TODO - Need to add
#model :record
#collection :records
#model :zone
#collection :zones
model :zone
collection :zones
request_path 'fog/dns/requests/rackspace'
#TODO - Import/Export, modify multiple domains
@ -59,6 +58,8 @@ module Fog
raise Fog::Rackspace::Errors::Conflict.slurp error
rescue Excon::Errors::NotFound => error
raise Fog::Rackspace::Errors::NotFound.slurp error
rescue Excon::Errors::ServiceUnavailable => error
raise Fog::Rackspace::Errors::ServiceUnavailable.slurp error
end
unless response.body.empty?
response.body = MultiJson.decode(response.body)

View file

@ -8,10 +8,14 @@ module Fog
}
domains.each do |domain|
data['domains'] << {
'name' => domain[:name],
'emailAddress' => domain[:email_address],
'recordsList' => {
domain_data =
{
'name' => domain[:name],
'emailAddress' => domain[:email]
}
if domain.has_key? :records
domain_data['recordsList'] = {
'records' => domain[:records].collect do |record|
record_data = {
'ttl' => record[:ttl],
@ -27,7 +31,8 @@ module Fog
end
end
}
}
end
data['domains'] << domain_data
end
request(

View file

@ -15,14 +15,11 @@ module Fog
path = path + '?' + array_to_query_string(query_data)
end
puts path
resp = request(
request(
:expects => [202, 204],
:method => 'DELETE',
:path => path
)
puts resp.inspect
resp
end
end
end

View file

@ -13,8 +13,8 @@ module Fog
if options.has_key? :comment
data['comment'] = options[:comment]
end
if options.has_key? :email_address
data['emailAddress'] = options[:email_address]
if options.has_key? :email
data['emailAddress'] = options[:email]
end
if data.empty?

View file

@ -26,6 +26,7 @@ module Fog
class InternalServerError < ServiceError; end
class Conflict < ServiceError; end
class NotFound < ServiceError; end
class ServiceUnavailable < ServiceError; end
class BadRequest < ServiceError
#TODO - Need to find a bette way to print out these validation errors when they are thrown

View file

@ -32,6 +32,12 @@ def dns_providers
},
:zerigo => {
:mocked => false
},
:rackspace => {
:mocked => false,
:zone_attributes => {
:email => 'fog@example.com'
}
}
}
end

View file

@ -134,7 +134,7 @@ Shindo.tests('Fog::DNS[:rackspace] | DNS requests', ['rackspace', 'dns']) do
end
tests('success on simple domain') do
domain_tests({:name => 'basictestdomain.com', :email_address => 'hostmaster@basictestdomain.com', :records => [{:ttl => 300, :name => 'basictestdomain.com', :type => 'A', :data => '192.168.1.1'}]}) do
domain_tests({:name => 'basictestdomain.com', :email => 'hostmaster@basictestdomain.com', :records => [{:ttl => 300, :name => 'basictestdomain.com', :type => 'A', :data => '192.168.1.1'}]}) do
tests('list_domains').formats(LIST_DOMAIN_FORMAT) do
@service.list_domains.body
@ -148,8 +148,8 @@ Shindo.tests('Fog::DNS[:rackspace] | DNS requests', ['rackspace', 'dns']) do
@service.list_domain_details(@domain_id).body
end
tests("modify_domain('#{@domain_id}', :ttl => 500, :comment => 'woot', :email_address => 'randomemail@randomhost.com')").succeeds do
response = @service.modify_domain @domain_id, :ttl => 500, :comment => 'woot', :email_address => 'randomemail@randomhost.com'
tests("modify_domain('#{@domain_id}', :ttl => 500, :comment => 'woot', :email => 'randomemail@randomhost.com')").succeeds do
response = @service.modify_domain @domain_id, :ttl => 500, :comment => 'woot', :email => 'randomemail@randomhost.com'
wait_for response
end
end
@ -159,7 +159,7 @@ Shindo.tests('Fog::DNS[:rackspace] | DNS requests', ['rackspace', 'dns']) do
domain_tests(
{
:name => 'testdomainwithmultiplerecords.com',
:email_address => 'hostmaster@testdomainwithmultiplerecords.com',
:email => 'hostmaster@testdomainwithmultiplerecords.com',
:records =>
[
{
@ -182,16 +182,16 @@ Shindo.tests('Fog::DNS[:rackspace] | DNS requests', ['rackspace', 'dns']) do
tests('success for multiple domains') do
domains_tests(
[
{:name => 'basictestdomain1.com', :email_address => 'hostmaster@basictestdomain1.com', :records => [{:ttl => 300, :name =>'basictestdomain1.com', :type => 'A', :data => '192.168.1.1'}]},
{:name => 'basictestdomain2.com', :email_address => 'hostmaster@basictestdomain2.com', :records => [{:ttl => 300, :name =>'basictestdomain2.com', :type => 'A', :data => '192.168.1.1'}]}
{:name => 'basictestdomain1.com', :email => 'hostmaster@basictestdomain1.com', :records => [{:ttl => 300, :name =>'basictestdomain1.com', :type => 'A', :data => '192.168.1.1'}]},
{:name => 'basictestdomain2.com', :email => 'hostmaster@basictestdomain2.com', :records => [{:ttl => 300, :name =>'basictestdomain2.com', :type => 'A', :data => '192.168.1.1'}]}
])
end
tests('success for domain with subdomain') do
domains_tests(
[
{:name => 'basictestdomain.com', :email_address => 'hostmaster@basictestdomain.com', :records => [{:ttl => 300, :name =>'basictestdomain.com', :type => 'A', :data => '192.168.1.1'}]},
{:name => 'subdomain.basictestdomain.com', :email_address => 'hostmaster@subdomain.basictestdomain.com', :records => [{:ttl => 300, :name =>'subdomain.basictestdomain.com', :type => 'A', :data => '192.168.1.1'}]}
{:name => 'basictestdomain.com', :email => 'hostmaster@basictestdomain.com', :records => [{:ttl => 300, :name =>'basictestdomain.com', :type => 'A', :data => '192.168.1.1'}]},
{:name => 'subdomain.basictestdomain.com', :email => 'hostmaster@subdomain.basictestdomain.com', :records => [{:ttl => 300, :name =>'subdomain.basictestdomain.com', :type => 'A', :data => '192.168.1.1'}]}
], true) do
@root_domain_id = @domain_details.find { |domain| domain['name'] == 'basictestdomain.com' }['id']
@ -229,7 +229,7 @@ Shindo.tests('Fog::DNS[:rackspace] | DNS requests', ['rackspace', 'dns']) do
tests( 'failure') do
tests('create_domain(invalid)').raises(Fog::Rackspace::Errors::BadRequest) do
wait_for @service.create_domains [{:name => 'badtestdomain.com', :email_address => '', :records => [{:ttl => 300, :name => 'badtestdomain.com', :type => 'A', :data => '192.168.1.1'}]}]
wait_for @service.create_domains [{:name => 'badtestdomain.com', :email => '', :records => [{:ttl => 300, :name => 'badtestdomain.com', :type => 'A', :data => '192.168.1.1'}]}]
end
tests('list_domains :limit => 5, :offset => 8').raises(Fog::Rackspace::Errors::BadRequest) do