Class: Rubber::Dns::Zerigo

Inherits:
Base
  • Object
show all
Defined in:
lib/rubber/dns/zerigo.rb

Instance Attribute Summary

Attributes inherited from Base

#env, #provider_env, #provider_name

Instance Method Summary collapse

Methods inherited from Base

#destroy, #host_records_equal?, #setup_opts, #up_to_date, #update

Constructor Details

#initialize(env) ⇒ Zerigo

Returns a new instance of Zerigo.



15
16
17
18
19
20
# File 'lib/rubber/dns/zerigo.rb', line 15

def initialize(env)
  super(env, "zerigo")

  ::Zerigo::DNS::Base.user = provider_env.email
  ::Zerigo::DNS::Base.password = provider_env.token
end

Instance Method Details

#create_host_record(opts = {}) ⇒ Object



84
85
86
87
88
# File 'lib/rubber/dns/zerigo.rb', line 84

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

#destroy_host_record(opts = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/rubber/dns/zerigo.rb', line 90

def destroy_host_record(opts = {})
  opts = setup_opts(opts, [:host, :domain])
  zone = ::Zerigo::DNS::Zone.find_or_create(opts[:domain])

  find_hosts(opts).each do |h|
    h.destroy || raise("Failed to destroy #{h.hostname}")
  end
end

#find_host_records(opts = {}) ⇒ Object



78
79
80
81
82
# File 'lib/rubber/dns/zerigo.rb', line 78

def find_host_records(opts = {})
  hosts = find_hosts(opts)
  result = hosts.collect {|h| host_to_opts(h).merge(:domain => opts[:domain]) }
  return result
end

#find_hosts(opts = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rubber/dns/zerigo.rb', line 42

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 }

  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

  begin
    hosts = ::Zerigo::DNS::Host.find(:all, :params=> params)

    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

  return result
end

#host_to_opts(host) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/rubber/dns/zerigo.rb', line 22

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

#opts_to_host(opts, host = {}) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/rubber/dns/zerigo.rb', line 33

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

#update_host_record(old_opts = {}, new_opts = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rubber/dns/zerigo.rb', line 99

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])

  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