mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[rackspace|dns] all important domains requests
This commit is contained in:
parent
da35f1050d
commit
7ac6b2069f
11 changed files with 435 additions and 10 deletions
|
@ -17,7 +17,15 @@ module Fog
|
|||
#collection :zones
|
||||
|
||||
request_path 'fog/dns/requests/rackspace'
|
||||
#TODO - Import/Export, modify multiple domains
|
||||
request :callback
|
||||
request :list_domains
|
||||
request :list_domain_details
|
||||
request :modify_domain
|
||||
request :create_domains
|
||||
request :delete_domain
|
||||
request :delete_domains
|
||||
request :list_subdomains
|
||||
|
||||
class Mock
|
||||
end
|
||||
|
@ -47,6 +55,10 @@ module Fog
|
|||
}))
|
||||
rescue Excon::Errors::BadRequest => error
|
||||
raise Fog::Rackspace::Errors::BadRequest.slurp error
|
||||
rescue Excon::Errors::Conflict => error
|
||||
raise Fog::Rackspace::Errors::Conflict.slurp error
|
||||
rescue Excon::Errors::NotFound => error
|
||||
raise Fog::Rackspace::Errors::NotFound.slurp error
|
||||
end
|
||||
unless response.body.empty?
|
||||
response.body = MultiJson.decode(response.body)
|
||||
|
@ -65,6 +77,10 @@ module Fog
|
|||
account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
|
||||
[auth_token, account_id]
|
||||
end
|
||||
|
||||
def array_to_query_string(arr)
|
||||
arr.collect {|k,v| "#{k}=#{v}" }.join('&')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
15
lib/fog/dns/requests/rackspace/callback.rb
Normal file
15
lib/fog/dns/requests/rackspace/callback.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Fog
|
||||
module DNS
|
||||
class Rackspace
|
||||
class Real
|
||||
def callback(job_id)
|
||||
request(
|
||||
:expects => [200, 202, 204],
|
||||
:method => 'GET',
|
||||
:path => "status/#{job_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
43
lib/fog/dns/requests/rackspace/create_domains.rb
Normal file
43
lib/fog/dns/requests/rackspace/create_domains.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
module Fog
|
||||
module DNS
|
||||
class Rackspace
|
||||
class Real
|
||||
def create_domains(domains)
|
||||
data = {
|
||||
'domains' => []
|
||||
}
|
||||
|
||||
domains.each do |domain|
|
||||
data['domains'] << {
|
||||
'name' => domain[:name],
|
||||
'emailAddress' => domain[:email_address],
|
||||
'recordsList' => {
|
||||
'records' => domain[:records].collect do |record|
|
||||
record_data = {
|
||||
'ttl' => record[:ttl],
|
||||
'data' => record[:data],
|
||||
'name' => record[:name],
|
||||
'type' => record[:type],
|
||||
}
|
||||
|
||||
if record.has_key? :priority
|
||||
record_data.merge!({'priority' => record[:priority]})
|
||||
else
|
||||
record_data
|
||||
end
|
||||
end
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
request(
|
||||
:expects => 202,
|
||||
:method => 'POST',
|
||||
:path => 'domains',
|
||||
:body => MultiJson.encode(data)
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
30
lib/fog/dns/requests/rackspace/delete_domain.rb
Normal file
30
lib/fog/dns/requests/rackspace/delete_domain.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Fog
|
||||
module DNS
|
||||
class Rackspace
|
||||
class Real
|
||||
def delete_domain(domain_id, options={})
|
||||
|
||||
path = "domains/#{domain_id}"
|
||||
query_data = {}
|
||||
|
||||
if options.has_key? :delete_subdomains
|
||||
query_data['deleteSubdomains'] = options[:delete_subdomains].to_s
|
||||
end
|
||||
|
||||
if !query_data.empty?
|
||||
path = path + '?' + array_to_query_string(query_data)
|
||||
end
|
||||
|
||||
puts path
|
||||
resp = request(
|
||||
:expects => [202, 204],
|
||||
:method => 'DELETE',
|
||||
:path => path
|
||||
)
|
||||
puts resp.inspect
|
||||
resp
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
27
lib/fog/dns/requests/rackspace/delete_domains.rb
Normal file
27
lib/fog/dns/requests/rackspace/delete_domains.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
module Fog
|
||||
module DNS
|
||||
class Rackspace
|
||||
class Real
|
||||
def delete_domains(domain_ids, options={})
|
||||
|
||||
path = "domains?" + domain_ids.collect { |domain_id| "id=#{domain_id}" }.join('&')
|
||||
query_data = {}
|
||||
|
||||
if options.has_key? :delete_subdomains
|
||||
query_data['deleteSubdomains'] = options[:delete_subdomains]
|
||||
end
|
||||
|
||||
if !query_data.empty?
|
||||
path = path + '&' + array_to_query_string(query_data)
|
||||
end
|
||||
|
||||
request(
|
||||
:expects => [202, 204],
|
||||
:method => 'DELETE',
|
||||
:path => path
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
30
lib/fog/dns/requests/rackspace/list_domain_details.rb
Normal file
30
lib/fog/dns/requests/rackspace/list_domain_details.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Fog
|
||||
module DNS
|
||||
class Rackspace
|
||||
class Real
|
||||
def list_domain_details(domain_id, options={})
|
||||
|
||||
path = "domains/#{domain_id}"
|
||||
query_data = {}
|
||||
|
||||
if options.has_key? :show_records
|
||||
query_data['showRecords'] = options[:show_records]
|
||||
end
|
||||
if options.has_key? :show_subdomains
|
||||
query_data['showSubdomains'] = options[:show_subdomains]
|
||||
end
|
||||
|
||||
if !query_data.empty?
|
||||
path = path + '?' + array_to_query_string(query_data)
|
||||
end
|
||||
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => path
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -6,7 +6,7 @@ module Fog
|
|||
|
||||
path = 'domains'
|
||||
if !options.empty?
|
||||
path = path + '?' + options.collect {|k,v| "#{k}=#{v}" }.join('&')
|
||||
path = path + '?' + array_to_query_string(options)
|
||||
end
|
||||
|
||||
request(
|
||||
|
|
21
lib/fog/dns/requests/rackspace/list_subdomains.rb
Normal file
21
lib/fog/dns/requests/rackspace/list_subdomains.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module Fog
|
||||
module DNS
|
||||
class Rackspace
|
||||
class Real
|
||||
def list_subdomains(domain_id, options={})
|
||||
|
||||
path = "domains/#{domain_id}/subdomains"
|
||||
if !options.empty?
|
||||
path = path + '?' + array_to_query_string(options)
|
||||
end
|
||||
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => path
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
34
lib/fog/dns/requests/rackspace/modify_domain.rb
Normal file
34
lib/fog/dns/requests/rackspace/modify_domain.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
module Fog
|
||||
module DNS
|
||||
class Rackspace
|
||||
class Real
|
||||
def modify_domain(domain_id, options={})
|
||||
|
||||
path = "domains/#{domain_id}"
|
||||
data = {}
|
||||
|
||||
if options.has_key? :ttl
|
||||
data['ttl'] = options[:ttl]
|
||||
end
|
||||
if options.has_key? :comment
|
||||
data['comment'] = options[:comment]
|
||||
end
|
||||
if options.has_key? :email_address
|
||||
data['emailAddress'] = options[:email_address]
|
||||
end
|
||||
|
||||
if data.empty?
|
||||
return
|
||||
end
|
||||
|
||||
request(
|
||||
:expects => [202, 204],
|
||||
:method => 'PUT',
|
||||
:path => path,
|
||||
:body => MultiJson.encode(data)
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -24,6 +24,8 @@ module Fog
|
|||
end
|
||||
|
||||
class InternalServerError < ServiceError; end
|
||||
class Conflict < ServiceError; end
|
||||
class NotFound < ServiceError; end
|
||||
|
||||
class BadRequest < ServiceError
|
||||
#TODO - Need to find a bette way to print out these validation errors when they are thrown
|
||||
|
|
|
@ -1,11 +1,19 @@
|
|||
Shindo.tests('Fog::DNS[:rackspace] | DNS requests', ['rackspace', 'dns']) do
|
||||
|
||||
@domain = ''
|
||||
@new_zones = []
|
||||
@new_records =[]
|
||||
|
||||
@service = Fog::DNS[:rackspace]
|
||||
|
||||
SUBDOMAIN_FORMAT = {
|
||||
'name' => String,
|
||||
'id' => Integer,
|
||||
'created' => String,
|
||||
'updated' => String
|
||||
}
|
||||
|
||||
LIST_SUBDOMAINS_FORMAT = {
|
||||
'domains' => [SUBDOMAIN_FORMAT],
|
||||
'totalEntries' => Integer
|
||||
}
|
||||
|
||||
LIST_DOMAIN_FORMAT = {
|
||||
'domains' => [
|
||||
{
|
||||
|
@ -25,19 +33,218 @@ Shindo.tests('Fog::DNS[:rackspace] | DNS requests', ['rackspace', 'dns']) do
|
|||
]
|
||||
}
|
||||
|
||||
tests( 'success') do
|
||||
tests('list_domains').formats(LIST_DOMAIN_FORMAT) do
|
||||
@service.list_domains.body
|
||||
RECORD_LIST_FORMAT = {
|
||||
'records' => [
|
||||
{
|
||||
'name' => String,
|
||||
'id' => String,
|
||||
'type' => String,
|
||||
'data' => String,
|
||||
'updated' => String,
|
||||
'created' => String,
|
||||
'ttl' => Integer,
|
||||
'priority' => Fog::Nullable::Integer
|
||||
}
|
||||
],
|
||||
#In some cases this is returned (domain details) and in some cases it isn't (create domain). Marking as nullable.
|
||||
'totalEntries' => Fog::Nullable::Integer
|
||||
}
|
||||
|
||||
NAME_SERVERS_FORMAT = [{
|
||||
'name' => String
|
||||
}]
|
||||
|
||||
BASIC_DOMAIN_DETAIL_FORMAT = {
|
||||
'name' => String,
|
||||
'id' => Integer,
|
||||
'accountId' => Integer,
|
||||
'updated' => String,
|
||||
'created' =>String,
|
||||
'ttl' => Integer,
|
||||
'emailAddress' => String,
|
||||
'nameservers' => NAME_SERVERS_FORMAT
|
||||
}
|
||||
|
||||
LIST_DOMAIN_DETAILS_WITH_RECORDS = BASIC_DOMAIN_DETAIL_FORMAT.merge({
|
||||
'recordsList' => RECORD_LIST_FORMAT
|
||||
})
|
||||
|
||||
LIST_DOMAIN_DETAILS_WITH_RECORDS_AND_SUBDOMAINS_FORMAT = BASIC_DOMAIN_DETAIL_FORMAT.merge({
|
||||
'recordsList' => RECORD_LIST_FORMAT,
|
||||
'subdomains' => [SUBDOMAIN_FORMAT]
|
||||
})
|
||||
|
||||
LIST_DOMAIN_DETAILS_WITHOUT_RECORDS_AND_SUBDOMAINS_FORMAT = BASIC_DOMAIN_DETAIL_FORMAT
|
||||
|
||||
CREATE_DOMAINS_FORMAT = {
|
||||
'domains' => [
|
||||
BASIC_DOMAIN_DETAIL_FORMAT.merge({
|
||||
'recordsList' => RECORD_LIST_FORMAT
|
||||
})
|
||||
]
|
||||
}
|
||||
|
||||
def wait_for(response)
|
||||
job_id = response.body['jobId']
|
||||
while true
|
||||
response = @service.callback(job_id)
|
||||
return response if response.status != 202
|
||||
sleep 5
|
||||
end
|
||||
end
|
||||
|
||||
def domain_tests(domain_attributes)
|
||||
tests("create_domains([#{domain_attributes}])").formats(CREATE_DOMAINS_FORMAT) do
|
||||
response = wait_for @service.create_domains([domain_attributes])
|
||||
@domain_details = response.body['domains']
|
||||
@domain_id = @domain_details[0]['id']
|
||||
response.body
|
||||
end
|
||||
|
||||
tests('list_domains :limit => 5, :offset => 10, :domain => "hartsock" --> All possible attributes').formats(LIST_DOMAIN_FORMAT) do
|
||||
@service.list_domains(:limit => 5, :offset => 10, :domain => 'hartsock').body
|
||||
begin
|
||||
if block_given?
|
||||
yield
|
||||
end
|
||||
ensure
|
||||
tests("delete_domain('#{@domain_id}')").succeeds do
|
||||
wait_for @service.delete_domain @domain_id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def domains_tests(domains_attributes, custom_delete = false)
|
||||
tests("create_domains(#{domains_attributes})").formats(CREATE_DOMAINS_FORMAT) do
|
||||
response = wait_for @service.create_domains(domains_attributes)
|
||||
@domain_details = response.body['domains']
|
||||
@domain_ids = @domain_details.collect { |domain| domain['id'] }
|
||||
response.body
|
||||
end
|
||||
|
||||
begin
|
||||
if block_given?
|
||||
yield
|
||||
end
|
||||
ensure
|
||||
if !custom_delete
|
||||
tests("deletes_domains(#{@domain_ids})").succeeds do
|
||||
wait_for @service.delete_domains @domain_ids
|
||||
end
|
||||
end
|
||||
end
|
||||
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
|
||||
|
||||
tests('list_domains').formats(LIST_DOMAIN_FORMAT) do
|
||||
@service.list_domains.body
|
||||
end
|
||||
|
||||
tests("list_domains :limit => 5, :offset => 10, :domain => #{@domain_details.first['name']} --> All possible attributes").formats(LIST_DOMAIN_FORMAT) do
|
||||
@service.list_domains(:limit => 5, :offset => 10, :domain => @domain_details.first['name']).body
|
||||
end
|
||||
|
||||
tests("list_domain_details('#{@domain_id}')").formats(LIST_DOMAIN_DETAILS_WITH_RECORDS) 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'
|
||||
wait_for response
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
tests('success for domain with multiple records') do
|
||||
domain_tests(
|
||||
{
|
||||
:name => 'testdomainwithmultiplerecords.com',
|
||||
:email_address => 'hostmaster@testdomainwithmultiplerecords.com',
|
||||
:records =>
|
||||
[
|
||||
{
|
||||
:ttl => 300,
|
||||
:name => 'testdomainwithmultiplerecords.com',
|
||||
:type => 'A',
|
||||
:data => '192.168.1.1'
|
||||
},
|
||||
{
|
||||
:ttl => 3600,
|
||||
:name => 'testdomainwithmultiplerecords.com',
|
||||
:type => 'MX',
|
||||
:data => 'mx.testdomainwithmultiplerecords.com',
|
||||
:priority => 10
|
||||
}
|
||||
]
|
||||
})
|
||||
end
|
||||
|
||||
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'}]}
|
||||
])
|
||||
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'}]}
|
||||
], true) do
|
||||
|
||||
@root_domain_id = @domain_details.find { |domain| domain['name'] == 'basictestdomain.com' }['id']
|
||||
|
||||
tests("list_domain_details('#{@root_domain_id}', :show_records => false, :show_subdomains => false)") do
|
||||
response = @service.list_domain_details(@root_domain_id, :show_records => false, :show_subdomains => false)
|
||||
|
||||
formats(LIST_DOMAIN_DETAILS_WITHOUT_RECORDS_AND_SUBDOMAINS_FORMAT) { response.body }
|
||||
returns(nil) { response.body['recordsList'] }
|
||||
returns(nil) { response.body['subdomains'] }
|
||||
end
|
||||
|
||||
tests("list_domain_details('#{@root_domain_id}', :show_records => true, :show_subdomains => true)") do
|
||||
response = @service.list_domain_details(@root_domain_id, :show_records => true, :show_subdomains => true)
|
||||
|
||||
formats(LIST_DOMAIN_DETAILS_WITH_RECORDS_AND_SUBDOMAINS_FORMAT) { response.body }
|
||||
returns(false) { response.body['recordsList'].nil? }
|
||||
returns(false) { response.body['subdomains'].nil? }
|
||||
end
|
||||
|
||||
tests("list_subdomains('#{@root_domain_id}')").formats(LIST_SUBDOMAINS_FORMAT) do
|
||||
@service.list_subdomains(@root_domain_id).body
|
||||
end
|
||||
|
||||
tests("delete_domain('#{@root_domain_id}', :delete_subdomains => true)") do
|
||||
wait_for @service.delete_domain(@root_domain_id, :delete_subdomains => true)
|
||||
|
||||
test('domain and subdomains were really deleted') do
|
||||
(@service.list_domains.body['domains'].collect { |domain| domain['name'] } & ['basictestdomain.com', 'subdomain.basictestdomain.com']).empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
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'}]}]
|
||||
end
|
||||
|
||||
tests('list_domains :limit => 5, :offset => 8').raises(Fog::Rackspace::Errors::BadRequest) do
|
||||
@service.list_domains :limit => 5, :offset => 8
|
||||
end
|
||||
|
||||
tests('list_domain_details 34335353').raises(Fog::Rackspace::Errors::NotFound) do
|
||||
@service.list_domain_details 34335353
|
||||
end
|
||||
|
||||
#tests('create_domains(#{domains})').raises(Fog::Rackspace::Errors::Conflict) do
|
||||
# wait_for @service.create_domains(domains)
|
||||
#end
|
||||
#tests('delete_domain(34343435)').raises(Fog::DNS::Rackspace::DeleteFault) do
|
||||
# @service.delete_domain 34343435
|
||||
#end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue