2011-08-31 16:52:53 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rackspace'))
|
|
|
|
require 'fog/dns'
|
|
|
|
|
2011-08-15 22:22:35 -04:00
|
|
|
module Fog
|
|
|
|
module DNS
|
|
|
|
class Rackspace < Fog::Service
|
|
|
|
|
|
|
|
US_ENDPOINT = 'https://dns.api.rackspacecloud.com/v1.0'
|
|
|
|
UK_ENDPOINT = 'https://lon.dns.api.rackspacecloud.com/v1.0'
|
|
|
|
|
|
|
|
requires :rackspace_api_key, :rackspace_username
|
|
|
|
recognizes :rackspace_auth_url
|
|
|
|
recognizes :rackspace_auth_token
|
|
|
|
|
2011-08-29 11:37:01 -04:00
|
|
|
model_path 'fog/rackspace/models/dns'
|
2011-08-28 15:41:48 -04:00
|
|
|
model :record
|
|
|
|
collection :records
|
2011-08-27 22:06:46 -04:00
|
|
|
model :zone
|
|
|
|
collection :zones
|
2011-08-15 22:22:35 -04:00
|
|
|
|
2011-08-29 11:37:01 -04:00
|
|
|
request_path 'fog/rackspace/requests/dns'
|
2011-08-28 10:53:43 -04:00
|
|
|
#TODO - Import/Export, modify multiple domains, modify multiple records
|
2011-08-27 18:46:15 -04:00
|
|
|
request :callback
|
2011-08-16 20:59:57 -04:00
|
|
|
request :list_domains
|
2011-08-27 18:46:15 -04:00
|
|
|
request :list_domain_details
|
|
|
|
request :modify_domain
|
|
|
|
request :create_domains
|
2011-08-28 10:53:43 -04:00
|
|
|
request :remove_domain
|
|
|
|
request :remove_domains
|
2011-08-27 18:46:15 -04:00
|
|
|
request :list_subdomains
|
2011-08-28 10:53:43 -04:00
|
|
|
request :list_records
|
|
|
|
request :list_record_details
|
|
|
|
request :modify_record
|
|
|
|
request :remove_record
|
|
|
|
request :remove_records
|
|
|
|
request :add_records
|
2011-08-16 20:59:57 -04:00
|
|
|
|
|
|
|
class Mock
|
2011-09-22 20:46:54 -04:00
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
Fog::Mock.not_implemented
|
|
|
|
end
|
|
|
|
|
2011-08-16 20:59:57 -04:00
|
|
|
end
|
2011-08-15 22:22:35 -04:00
|
|
|
|
|
|
|
class Real
|
|
|
|
def initialize(options={})
|
|
|
|
require 'multi_json'
|
|
|
|
@rackspace_api_key = options[:rackspace_api_key]
|
|
|
|
@rackspace_username = options[:rackspace_username]
|
|
|
|
@rackspace_auth_url = options[:rackspace_auth_url]
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2011-08-15 22:22:35 -04:00
|
|
|
uri = URI.parse(options[:rackspace_dns_endpoint] || US_ENDPOINT)
|
|
|
|
|
|
|
|
@auth_token, @account_id = *authenticate
|
2011-09-12 11:01:48 -04:00
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@path = "#{uri.path}/#{@account_id}"
|
2011-08-15 22:22:35 -04:00
|
|
|
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options[:headers] ||= {}
|
|
|
|
@connection_options[:headers].merge!({ 'Content-Type' => 'application/json', 'X-Auth-Token' => @auth_token })
|
|
|
|
|
|
|
|
@connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)
|
2011-08-15 22:22:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def request(params)
|
|
|
|
#TODO - Unify code with other rackspace services
|
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge!({
|
|
|
|
:path => "#{@path}/#{params[:path]}"
|
|
|
|
}))
|
2011-08-16 22:33:15 -04:00
|
|
|
rescue Excon::Errors::BadRequest => error
|
|
|
|
raise Fog::Rackspace::Errors::BadRequest.slurp error
|
2011-08-27 18:46:15 -04:00
|
|
|
rescue Excon::Errors::Conflict => error
|
|
|
|
raise Fog::Rackspace::Errors::Conflict.slurp error
|
|
|
|
rescue Excon::Errors::NotFound => error
|
|
|
|
raise Fog::Rackspace::Errors::NotFound.slurp error
|
2011-08-27 22:06:46 -04:00
|
|
|
rescue Excon::Errors::ServiceUnavailable => error
|
|
|
|
raise Fog::Rackspace::Errors::ServiceUnavailable.slurp error
|
2011-08-15 22:22:35 -04:00
|
|
|
end
|
|
|
|
unless response.body.empty?
|
|
|
|
response.body = MultiJson.decode(response.body)
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate
|
|
|
|
options = {
|
|
|
|
:rackspace_api_key => @rackspace_api_key,
|
|
|
|
:rackspace_username => @rackspace_username,
|
|
|
|
:rackspace_auth_url => @rackspace_auth_url
|
|
|
|
}
|
2011-09-12 11:01:48 -04:00
|
|
|
credentials = Fog::Rackspace.authenticate(options, @connection_options)
|
2011-08-15 22:22:35 -04:00
|
|
|
auth_token = credentials['X-Auth-Token']
|
|
|
|
account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
|
|
|
|
[auth_token, account_id]
|
|
|
|
end
|
2011-08-27 18:46:15 -04:00
|
|
|
|
|
|
|
def array_to_query_string(arr)
|
|
|
|
arr.collect {|k,v| "#{k}=#{v}" }.join('&')
|
|
|
|
end
|
2011-08-28 10:53:43 -04:00
|
|
|
|
|
|
|
def validate_path_fragment(name, fragment)
|
|
|
|
if fragment.nil? or fragment.to_s.empty?
|
2011-08-31 13:37:50 -04:00
|
|
|
raise ArgumentError.new("#{name} cannot be null or empty")
|
2011-08-28 10:53:43 -04:00
|
|
|
end
|
|
|
|
end
|
2011-08-15 22:22:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|