2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/zerigo'
|
2011-08-31 15:52:53 -05:00
|
|
|
require 'fog/dns'
|
|
|
|
|
2010-12-11 20:01:33 -05:00
|
|
|
module Fog
|
2011-06-15 17:25:01 -07:00
|
|
|
module DNS
|
|
|
|
class Zerigo < Fog::Service
|
2010-12-11 20:01:33 -05:00
|
|
|
|
2010-12-21 13:45:53 -08:00
|
|
|
requires :zerigo_email, :zerigo_token
|
2011-08-03 09:44:05 -05:00
|
|
|
recognizes :host, :persistent, :port, :scheme, :timeout
|
2010-12-11 20:01:33 -05:00
|
|
|
|
2011-08-24 14:40:47 -05:00
|
|
|
model_path 'fog/zerigo/models/dns'
|
2010-12-22 20:36:53 -08:00
|
|
|
model :record
|
|
|
|
collection :records
|
|
|
|
model :zone
|
|
|
|
collection :zones
|
2010-12-11 20:01:33 -05:00
|
|
|
|
2011-08-24 14:40:47 -05:00
|
|
|
request_path 'fog/zerigo/requests/dns'
|
2010-12-22 20:36:53 -08:00
|
|
|
request :count_hosts
|
2010-12-12 09:51:52 -05:00
|
|
|
request :count_zones
|
2010-12-22 20:36:53 -08:00
|
|
|
request :create_host
|
2010-12-12 09:51:52 -05:00
|
|
|
request :create_zone
|
2010-12-22 20:36:53 -08:00
|
|
|
request :delete_host
|
2010-12-12 18:02:02 -05:00
|
|
|
request :delete_zone
|
|
|
|
request :find_hosts
|
|
|
|
request :get_host
|
2010-12-22 20:36:53 -08:00
|
|
|
request :get_zone
|
|
|
|
request :get_zone_stats
|
|
|
|
request :list_zones
|
|
|
|
request :list_hosts
|
2010-12-12 18:02:02 -05:00
|
|
|
request :update_host
|
2010-12-22 20:36:53 -08:00
|
|
|
request :update_zone
|
2010-12-11 20:01:33 -05:00
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
2011-10-08 20:19:32 -04:00
|
|
|
hash[key] = key == :zones ? [] : {}
|
2010-12-11 20:01:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-19 10:05:33 -04:00
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
2010-12-11 20:01:33 -05:00
|
|
|
def initialize(options={})
|
2010-12-15 15:45:19 -08:00
|
|
|
@zerigo_email = options[:zerigo_email]
|
2010-12-21 13:45:53 -08:00
|
|
|
@zerigo_token = options[:zerigo_token]
|
2011-05-19 15:35:33 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2011-10-08 20:19:32 -04:00
|
|
|
self.class.data
|
2011-03-10 11:16:55 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
2011-10-08 20:19:32 -04:00
|
|
|
self.class.reset
|
2010-12-11 20:01:33 -05:00
|
|
|
end
|
|
|
|
|
2011-10-08 20:19:32 -04:00
|
|
|
def find_by_zone_id(zone_id)
|
|
|
|
self.data[:zones].find { |z| z['id'] == zone_id }
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_domain(domain)
|
|
|
|
self.data[:zones].find { |z| z['domain'] == domain }
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_host(host_id)
|
2011-10-09 11:49:02 -04:00
|
|
|
self.data[:zones].collect { |z| z['hosts'].find { |h| h['id'] == host_id } }.compact.first
|
2011-10-08 20:19:32 -04:00
|
|
|
end
|
2010-12-11 20:01:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options={})
|
2011-02-16 17:25:50 -08:00
|
|
|
require 'fog/core/parser'
|
|
|
|
|
2011-01-07 18:14:39 -08:00
|
|
|
@zerigo_email = options[:zerigo_email]
|
2010-12-21 13:45:53 -08:00
|
|
|
@zerigo_token = options[:zerigo_token]
|
2011-09-12 10:01:48 -05:00
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
@host = options[:host] || "ns.zerigo.com"
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@port = options[:port] || 80
|
|
|
|
@scheme = options[:scheme] || 'http'
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
2010-12-11 20:01:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params)
|
|
|
|
params[:headers] ||= {}
|
2010-12-21 13:45:53 -08:00
|
|
|
key= "#{@zerigo_email}:#{@zerigo_token}"
|
2010-12-11 20:01:33 -05:00
|
|
|
params[:headers].merge!({
|
|
|
|
'Authorization' => "Basic #{Base64.encode64(key).delete("\r\n")}"
|
|
|
|
})
|
|
|
|
case params[:method]
|
|
|
|
when 'DELETE', 'GET', 'HEAD'
|
|
|
|
params[:headers]['Accept'] = 'application/xml'
|
|
|
|
when 'POST', 'PUT'
|
|
|
|
params[:headers]['Content-Type'] = 'application/xml'
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge!({:host => @host}))
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
2011-06-15 17:25:01 -07:00
|
|
|
Fog::DNS::Zerigo::NotFound.slurp(error)
|
2010-12-11 20:01:33 -05:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|