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
|
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] || {}
|
Move Dynect endpoint from api2 to api-v4
I've had the problem before that Dynects api2 will fail when being called from a system that has both, IPv6 and IPv4 connectivity.
The message returned looks something like this:
```
@body="{\"status\": \"failure\", \"data\": {}, \"job_id\": 1234567890, \"msgs\": [{\"INFO\": \"login: IP address does not match current session\", \"SOURCE\": \"BLL\", \"ERR_CD\": \"INVALID_DATA\", \"LVL\": \"ERROR\"}, {\"INFO\": \"login: There was a problem with your credentials\", \"SOURCE\": \"BLL\", \"ERR_CD\": null, \"LVL\": \"INFO\"}]}", @status=400>
```
For our own (custom) library this was fixed by just moving from the 'api2.dynect.net' endpoint to 'api-v4.dynect.net'. We didn't change anything else and it just worked for us.
I just tried this locally and switching the endpoint solved the problem in fog for me.
2012-08-10 05:51:29 -04:00
|
|
|
@host = 'api-v4.dynect.net'
|
2011-09-12 11:01:48 -04:00
|
|
|
@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'
|
2013-09-21 15:38:15 -04:00
|
|
|
@version = options[:version] || '3.5.2'
|
2011-09-12 11:01:48 -04:00
|
|
|
@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
|
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
|
|
|
|
2011-02-07 22:12:56 -05:00
|
|
|
response = @connection.request(params.merge!({:host => @host}))
|
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/}
|
2012-08-13 10:13:52 -04:00
|
|
|
response = poll_job(response, params[:expects])
|
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
|
|
|
|
2012-08-13 10:13:52 -04:00
|
|
|
def poll_job(response, original_expects, time_to_wait = 10)
|
2011-11-04 14:00:22 -04:00
|
|
|
job_location = response.headers['Location']
|
|
|
|
|
|
|
|
Fog.wait_for(time_to_wait) do
|
2012-08-13 10:13:52 -04:00
|
|
|
response = request(:expects => original_expects, :method => :get, :path => job_location)
|
2011-11-04 14:00:22 -04:00
|
|
|
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
|