lib/rubber/dns/zerigo.rb in sml-rubber-0.9.13 vs lib/rubber/dns/zerigo.rb in sml-rubber-1.5.5
- old
+ new
@@ -1,119 +1,113 @@
require 'rubygems'
-require 'httparty'
+begin
+ require 'zerigo_dns'
+rescue LoadError
+ puts "Missing the zerigo_dns gem. Install with `sudo gem install zerigo_dns`."
+ exit(-1)
+end
+
module Rubber
module Dns
+ class Zerigo < Base
- class Zone
- include HTTParty
- format :xml
+ def initialize(env)
+ super(env, "zerigo")
- def initialize(customer_id, email, token, domain)
- self.class.basic_auth email, token
- self.class.base_uri "https://ns.zerigo.com/accounts/#{customer_id}"
- @domain = domain
- refresh()
+ ::Zerigo::DNS::Base.user = provider_env.email
+ ::Zerigo::DNS::Base.password = provider_env.token
end
- def hosts
- hosts = self.class.get("/zones/#{@zone['id']}/hosts.xml")
- return hosts['hosts']
+ def host_to_opts(host)
+ opts = {}
+ opts[:id] = host.id
+ opts[:host] = host.hostname || ''
+ opts[:type] = host.host_type
+ opts[:data] = host.data if host.data
+ opts[:ttl] = host.ttl if host.ttl
+ opts[:priority] = host.priority if host.priority
+ return opts
end
- def host(hostname)
- hosts = self.class.get("/zones/#{@zone['id']}/hosts.xml?fqdn=#{hostname}.#{@domain}")
- return (hosts['hosts'] || []).first
+ def opts_to_host(opts, host={})
+ host['hostname'] = opts[:host]
+ host['host_type'] = opts[:type]
+ host['data'] = opts[:data] if opts[:data]
+ host['ttl'] = opts[:ttl] if opts[:ttl]
+ host['priority'] = opts[:priority] if opts[:priority]
+ return host
end
- def new_host
- self.class.get("/zones/#{@zone['id']}/hosts/new.xml")['host']
- end
+ def find_hosts(opts = {})
+ opts = setup_opts(opts, [:host, :domain])
+ result = []
+ zone = ::Zerigo::DNS::Zone.find_or_create(opts[:domain])
+ params = { :zone_id => zone.id }
- def create_host(host)
- self.class.post("/zones/#{@zone['id']}/hosts.xml", :body => {:host => host})
- end
+ hn = opts[:host]
+ ht = opts[:type]
+ hd = opts[:data]
+ has_host = hn && hn != '*'
+ if has_host
+ url = ""
+ url << "#{hn}." if hn.strip.size > 0
+ url << "#{opts[:domain]}"
+ params[:fqdn] = url
+ end
- def update_host(host)
- host_id = host['id']
- self.class.put("/zones/#{@zone['id']}/hosts/#{host_id}.xml", :body => {:host => host})
- end
+ begin
+ hosts = ::Zerigo::DNS::Host.find(:all, :params=> params)
- def delete_host(hostname)
- host_id = host(hostname)['id']
- self.class.delete("/zones/#{@zone['id']}/hosts/#{host_id}.xml")
- end
-
- def refresh
- zone_id = @zone['id'] rescue nil
- if zone_id
- @zone = self.class.get("/zones/#{zone_id}.xml")
- else
- zones = self.class.get('/zones.xml')
- @zone = zones["zones"].find {|z| z["domain"] == @domain }
+ hosts.each do |h|
+ keep = true
+ if ht && h.host_type != ht && ht != '*'
+ keep = false
+ end
+ if hd && h.data != hd
+ keep = false
+ end
+ result << h if keep
+ end if hosts
+ rescue ActiveResource::ResourceNotFound => e
end
- end
- def data
- return @zone
+ return result
end
- protected
-
- def zones()
- self.class.get('/zones.xml')
+ def find_host_records(opts = {})
+ hosts = find_hosts(opts)
+ result = hosts.collect {|h| host_to_opts(h).merge(:domain => opts[:domain]) }
+ return result
end
- def zone(domain_name)
- zone = zones
- return zone
+ def create_host_record(opts = {})
+ opts = setup_opts(opts, [:host, :data, :domain, :type, :ttl])
+ zone = ::Zerigo::DNS::Zone.find_or_create(opts[:domain])
+ ::Zerigo::DNS::Host.create(opts_to_host(opts).merge(:zone_id => zone.id))
end
-
- end
-
- class Zerigo < Base
- def initialize(env)
- super(env)
- @zerigo_env = env.dns_providers.zerigo
- @ttl = (@zerigo_env.ttl || 300).to_i
- @record_type = @zerigo_env.record_type || "A"
- @zone = Zone.new(@zerigo_env.customer_id, @zerigo_env.email, @zerigo_env.token, env.domain)
- end
+ def destroy_host_record(opts = {})
+ opts = setup_opts(opts, [:host, :domain])
+ zone = ::Zerigo::DNS::Zone.find_or_create(opts[:domain])
- def nameserver
- "a.ns.zerigo.net"
+ find_hosts(opts).each do |h|
+ h.destroy || raise("Failed to destroy #{h.hostname}")
+ end
end
- def host_exists?(host)
- @zone.host(host)
- end
+ def update_host_record(old_opts={}, new_opts={})
+ old_opts = setup_opts(old_opts, [:host, :domain])
+ new_opts = setup_opts(new_opts, [:host, :domain, :type, :data])
+ zone = ::Zerigo::DNS::Zone.find_or_create(old_opts[:domain])
- def create_host_record(host, ip)
- host = @zone.new_host()
- host['host-type'] = @record_type
- host['ttl'] = @ttl
- host['hostname'] = host
- host['data'] = ip
- @zone.create_host(host)
- end
-
- def destroy_host_record(host)
- @zone.delete_host(host)
- end
-
- def update_host_record(host, ip)
- old = @zone.host(host)
- old['data'] = ip
- @zone.update_host(old)
- end
-
- # update the top level domain record which has an empty hostName
- def update_domain_record(ip)
- old = @zone.hosts.find {|h| h['hostname'].nil? }
- old['data'] = ip
- @zone.update_host(old)
+ find_hosts(old_opts).each do |h|
+ opts_to_host(new_opts).each do |k, v|
+ h.send("#{k}=", v)
+ end
+ h.save || raise("Failed to update host #{h.hostname}, #{h.errors.full_messages.join(', ')}")
+ end
end
end
end