2014-02-01 21:13:17 -05:00
require 'fog/rackspace/core'
2011-08-31 16:52:53 -04:00
2011-08-15 22:22:35 -04:00
module Fog
module DNS
class Rackspace < Fog :: Service
2013-04-16 16:01:07 -04:00
include Fog :: Rackspace :: Errors
2011-08-15 22:22:35 -04:00
2013-06-21 16:11:17 -04:00
class ServiceError < Fog :: Rackspace :: Errors :: ServiceError ; end
class InternalServerError < Fog :: Rackspace :: Errors :: InternalServerError ; end
class BadRequest < Fog :: Rackspace :: Errors :: BadRequest ; end
class Conflict < Fog :: Rackspace :: Errors :: Conflict ; end
class ServiceUnavailable < Fog :: Rackspace :: Errors :: ServiceUnavailable ; end
2011-11-24 11:29:12 -05:00
class CallbackError < Fog :: Errors :: Error
attr_reader :response , :message , :details
def initialize ( response )
@response = response
@message = response . body [ 'error' ] [ 'message' ]
@details = response . body [ 'error' ] [ 'details' ]
end
end
2011-08-15 22:22:35 -04:00
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
2013-03-13 17:01:32 -04:00
recognizes :rackspace_auth_url , :rackspace_auth_token , :rackspace_dns_endpoint , :rackspace_dns_url , :rackspace_region
2011-08-15 22:22:35 -04:00
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
2013-03-13 17:01:32 -04:00
class Mock < Fog :: Rackspace :: Service
2011-09-22 20:46:54 -04:00
def initialize ( options = { } )
2011-09-23 11:59:30 -04:00
@rackspace_api_key = options [ :rackspace_api_key ]
@rackspace_username = options [ :rackspace_username ]
@rackspace_auth_url = options [ :rackspace_auth_url ]
@connection_options = options [ :connection_options ] || { }
end
def self . data
@data || = {
}
end
def self . reset
@data = nil
end
def data
self . class . data
end
def reset_data
self . class . reset
2011-09-22 20:46:54 -04:00
end
2011-08-16 20:59:57 -04:00
end
2011-08-15 22:22:35 -04:00
2013-03-13 17:01:32 -04:00
class Real < Fog :: Rackspace :: Service
def service_name
:cloudDNS
end
def region
#Note: DNS does not currently support multiple regions
@rackspace_region
end
2011-08-15 22:22:35 -04:00
def initialize ( options = { } )
@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 ] || { }
2013-03-20 14:25:38 -04:00
@rackspace_endpoint = Fog :: Rackspace . normalize_url ( options [ :rackspace_dns_url ] || options [ :rackspace_dns_endpoint ] )
2013-03-13 17:01:32 -04:00
@rackspace_region = options [ :rackspace_region ]
2011-08-15 22:22:35 -04:00
2013-03-13 17:01:32 -04:00
authenticate
deprecation_warnings ( options )
2011-08-15 22:22:35 -04:00
2013-03-13 17:01:32 -04:00
@persistent = options [ :persistent ] || false
2014-02-26 19:50:35 -05:00
@connection = Fog :: XML :: Connection . new ( endpoint_uri . to_s , @persistent , @connection_options )
2013-03-13 17:01:32 -04:00
end
2011-09-12 11:01:48 -04:00
2013-03-13 17:01:32 -04:00
def endpoint_uri ( service_endpoint_url = nil )
@uri = super ( @rackspace_endpoint || service_endpoint_url , :rackspace_dns_url )
2011-08-15 22:22:35 -04:00
end
private
2013-09-24 13:02:10 -04:00
def request ( params , parse_json = true )
super
rescue Excon :: Errors :: NotFound = > error
raise NotFound . slurp ( error , self )
rescue Excon :: Errors :: BadRequest = > error
raise BadRequest . slurp ( error , self )
rescue Excon :: Errors :: InternalServerError = > error
raise InternalServerError . slurp ( error , self )
rescue Excon :: Errors :: ServiceUnavailable = > error
raise ServiceUnavailable . slurp ( error , self )
rescue Excon :: Errors :: Conflict = > error
raise Conflict . slurp ( error , self )
rescue Excon :: Errors :: HTTPStatusError = > error
raise ServiceError . slurp ( error , self )
2011-08-15 22:22:35 -04:00
end
2011-08-27 18:46:15 -04:00
def array_to_query_string ( arr )
2013-06-18 13:42:35 -04:00
return " " unless arr
query_array = arr . collect do | k , v |
val_str = v . is_a? ( Array ) ? v . join ( " , " ) : v . to_s
" #{ k } = #{ val_str } "
end
query_array . join ( '&' )
2011-08-27 18:46:15 -04:00
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
2013-03-13 17:01:32 -04:00
private
def deprecation_warnings ( options )
Fog :: Logger . deprecation ( " The :rackspace_dns_endpoint option is deprecated. Please use :rackspace_dns_url for custom endpoints " ) if options [ :rackspace_dns_endpoint ]
end
def setup_endpoint ( credentials )
account_id = credentials [ 'X-Server-Management-Url' ] . match ( / .* \/ ([ \ d]+)$ / ) [ 1 ]
2013-03-18 11:37:05 -04:00
@uri = URI . parse ( @rackspace_endpoint || US_ENDPOINT )
2013-03-13 17:01:32 -04:00
@uri . path = " #{ @uri . path } / #{ account_id } "
end
def authenticate_v1 ( options )
credentials = Fog :: Rackspace . authenticate ( options , @connection_options )
setup_endpoint credentials
@auth_token = credentials [ 'X-Auth-Token' ]
end
2013-06-21 16:11:17 -04:00
def authenticate ( options = { } )
super ( {
2013-03-13 17:01:32 -04:00
:rackspace_api_key = > @rackspace_api_key ,
:rackspace_username = > @rackspace_username ,
2013-05-30 13:52:37 -04:00
:rackspace_auth_url = > @rackspace_auth_url ,
:connection_options = > @connection_options
2013-06-21 16:11:17 -04:00
} )
2013-03-13 17:01:32 -04:00
end
2011-08-15 22:22:35 -04:00
end
end
end
end