2014-02-01 21:13:17 -05:00
|
|
|
require 'fog/dynect/core'
|
2011-08-31 16:52:53 -04:00
|
|
|
|
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
|
2013-09-21 15:23:05 -04:00
|
|
|
request :get_all_records
|
2011-07-14 20:22:43 -04:00
|
|
|
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
|
2013-09-25 11:08:02 -04:00
|
|
|
request :put_record
|
2011-07-14 20:22:43 -04:00
|
|
|
|
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] || {}
|
2014-07-30 11:06:04 -04:00
|
|
|
@host = 'api-v4.dynect.net'
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@path = options[:path] || '/REST'
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@scheme = options[:scheme] || 'https'
|
|
|
|
@version = options[:version] || '3.5.2'
|
|
|
|
@job_poll_timeout = options[:job_poll_timeout] || 10
|
2014-02-26 19:50:35 -05:00
|
|
|
@connection = Fog::XML::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
|
2012-06-26 20:49:49 -04: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)}/}
|
2012-06-26 20:49:49 -04:00
|
|
|
|
2013-10-09 16:25:47 -04:00
|
|
|
response = @connection.request(params)
|
2011-07-14 20:22:43 -04:00
|
|
|
|
2012-06-26 20:49:49 -04:00
|
|
|
if response.body.empty?
|
|
|
|
response.body = {}
|
2012-08-03 12:20:30 -04:00
|
|
|
elsif response.headers['Content-Type'] == 'application/json'
|
2012-04-25 10:31:28 -04:00
|
|
|
response.body = Fog::JSON.decode(response.body)
|
2011-07-14 20:22:43 -04:00
|
|
|
end
|
|
|
|
|
2012-06-26 20:49:49 -04:00
|
|
|
if response.body['status'] == 'failure'
|
|
|
|
raise Error, response.body['msgs'].first['INFO']
|
|
|
|
end
|
|
|
|
|
|
|
|
if response.status == 307 && params[:path] !~ %r{^/REST/Job/}
|
2014-07-30 11:06:04 -04:00
|
|
|
response = poll_job(response, params[:expects], @job_poll_timeout)
|
2012-06-26 20:49:49 -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
|
|
|
|
2014-07-30 11:06:04 -04:00
|
|
|
def poll_job(response, original_expects, time_to_wait)
|
2011-11-04 14:00:22 -04:00
|
|
|
job_location = response.headers['Location']
|
2014-07-30 11:06:04 -04:00
|
|
|
|
2014-06-24 17:00:55 -04:00
|
|
|
begin
|
|
|
|
Fog.wait_for(time_to_wait) do
|
|
|
|
response = request(
|
|
|
|
:expects => original_expects,
|
|
|
|
:idempotent => true,
|
|
|
|
:method => :get,
|
|
|
|
:path => job_location
|
|
|
|
)
|
|
|
|
response.body['status'] != 'incomplete'
|
|
|
|
end
|
2014-07-30 11:06:04 -04:00
|
|
|
|
2014-06-24 17:00:55 -04:00
|
|
|
rescue Errors::TimeoutError => error
|
|
|
|
if response.body['status'] == 'incomplete'
|
|
|
|
raise JobIncomplete.new("Job #{response.body['job_id']} is still incomplete")
|
|
|
|
else
|
|
|
|
raise error
|
|
|
|
end
|
2011-11-04 14:00:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
2011-02-07 22:12:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|