2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/dynect'
|
2011-08-31 16:52:53 -04:00
|
|
|
require 'fog/dns'
|
|
|
|
|
2011-02-07 22:12:56 -05:00
|
|
|
module Fog
|
2011-07-14 20:22:43 -04:00
|
|
|
module DNS
|
|
|
|
class Dynect < Fog::Service
|
2011-02-07 22:12:56 -05:00
|
|
|
|
|
|
|
requires :dynect_customer, :dynect_username, :dynect_password
|
|
|
|
recognizes :timeout, :persistent
|
|
|
|
recognizes :provider # remove post deprecation
|
|
|
|
|
2011-08-24 20:52:11 -04:00
|
|
|
model_path 'fog/dynect/models/dns'
|
2011-06-16 17:51:13 -04:00
|
|
|
model :record
|
|
|
|
collection :records
|
|
|
|
model :zone
|
|
|
|
collection :zones
|
2011-02-07 22:12:56 -05:00
|
|
|
|
2011-08-24 20:52:11 -04:00
|
|
|
request_path 'fog/dynect/requests/dns'
|
2011-07-14 20:22:43 -04:00
|
|
|
request :delete_record
|
|
|
|
request :delete_zone
|
|
|
|
request :get_node_list
|
|
|
|
request :get_record
|
2011-06-17 06:51:37 -04:00
|
|
|
request :get_zone
|
2011-07-14 20:22:43 -04:00
|
|
|
request :post_record
|
|
|
|
request :post_session
|
|
|
|
request :post_zone
|
|
|
|
request :put_zone
|
|
|
|
|
2011-11-04 14:00:22 -04:00
|
|
|
class JobIncomplete < Error; end
|
|
|
|
|
2011-07-14 20:22:43 -04:00
|
|
|
class Mock
|
2011-08-16 12:57:17 -04:00
|
|
|
def initialize(options={})
|
|
|
|
@dynect_customer = options[:dynect_customer]
|
|
|
|
@dynect_username = options[:dynect_username]
|
|
|
|
@dynect_password = options[:dynect_password]
|
|
|
|
end
|
|
|
|
|
2011-08-29 19:04:13 -04:00
|
|
|
def self.data
|
|
|
|
@data ||= {
|
|
|
|
:zones => {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_token
|
|
|
|
@auth_token ||= Fog::Dynect::Mock.token
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.reset
|
|
|
|
end
|
2011-07-14 20:22:43 -04:00
|
|
|
end
|
2011-02-07 22:12:56 -05:00
|
|
|
|
|
|
|
class Real
|
|
|
|
def initialize(options={})
|
|
|
|
@dynect_customer = options[:dynect_customer]
|
|
|
|
@dynect_username = options[:dynect_username]
|
|
|
|
@dynect_password = options[:dynect_password]
|
|
|
|
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
@host = "api2.dynect.net"
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@path = options[:path] || '/REST'
|
2012-05-28 09:59:34 -04:00
|
|
|
@persistent = options[:persistent] || false
|
2011-09-12 11:01:48 -04:00
|
|
|
@scheme = options[:scheme] || 'https'
|
|
|
|
@version = options[:version] || '2.3.1'
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
2011-02-07 22:12:56 -05:00
|
|
|
end
|
|
|
|
|
2011-02-08 21:47:44 -05:00
|
|
|
def auth_token
|
2011-07-14 20:22:43 -04:00
|
|
|
@auth_token ||= post_session.body['data']['token']
|
2011-02-08 21:47:44 -05:00
|
|
|
end
|
|
|
|
|
2011-02-07 22:12:56 -05:00
|
|
|
def request(params)
|
|
|
|
begin
|
2011-11-04 14:00:22 -04:00
|
|
|
# any request could redirect to a job
|
|
|
|
params[:expects] = Array(params[:expects]) | [307]
|
|
|
|
|
2011-02-07 22:12:56 -05:00
|
|
|
params[:headers] ||= {}
|
2011-07-14 20:22:43 -04:00
|
|
|
params[:headers]['Content-Type'] = 'application/json'
|
2011-02-07 22:12:56 -05:00
|
|
|
params[:headers]['API-Version'] = @version
|
2011-02-08 21:47:44 -05:00
|
|
|
params[:headers]['Auth-Token'] = auth_token unless params[:path] == "Session"
|
2011-11-04 14:00:22 -04:00
|
|
|
params[:path] = "#{@path}/#{params[:path]}" unless params[:path] =~ %r{^#{Regexp.escape(@path)}/}
|
2011-02-07 22:12:56 -05:00
|
|
|
response = @connection.request(params.merge!({:host => @host}))
|
2011-07-14 20:22:43 -04:00
|
|
|
|
2011-11-04 14:00:22 -04:00
|
|
|
if response.status == 307
|
|
|
|
response = poll_job(response)
|
|
|
|
elsif !response.body.empty?
|
2012-04-25 10:31:28 -04:00
|
|
|
response.body = Fog::JSON.decode(response.body)
|
2011-07-14 20:22:43 -04:00
|
|
|
end
|
|
|
|
|
2011-11-04 14:00:22 -04:00
|
|
|
response
|
2011-02-07 22:12:56 -05:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2011-09-09 14:36:28 -04:00
|
|
|
if @auth_token && error.message =~ /login: (Bad or expired credentials|inactivity logout)/
|
2011-09-08 17:25:22 -04:00
|
|
|
@auth_token = nil
|
|
|
|
retry
|
|
|
|
else
|
|
|
|
raise error
|
|
|
|
end
|
2011-02-07 22:12:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
2011-11-04 14:00:22 -04:00
|
|
|
|
|
|
|
def poll_job(response, time_to_wait = 10)
|
|
|
|
job_location = response.headers['Location']
|
|
|
|
|
|
|
|
Fog.wait_for(time_to_wait) do
|
|
|
|
response = request(:expects => 200, :method => :get, :path => job_location)
|
|
|
|
response.body['status'] != 'incomplete'
|
|
|
|
end
|
|
|
|
|
|
|
|
if response.body['status'] == 'incomplete'
|
|
|
|
raise JobIncomplete.new("Job #{response.body['job_id']} is still incomplete")
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
2011-02-07 22:12:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|