Class: Rubber::Dns::Zone

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

Constant Summary collapse

@@zones =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(customer_id, email, token, domain) ⇒ Zone

Returns a new instance of Zone.



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

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()
end

Class Method Details

.get_zone(domain, provider_env) ⇒ Object



13
14
15
# File 'lib/rubber/dns/zerigo.rb', line 13

def self.get_zone(domain, provider_env)
 @@zones[domain] ||= Zone.new(provider_env.customer_id, provider_env.email, provider_env.token, domain)
end

Instance Method Details

#check_status(response) ⇒ Object



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

def check_status(response)
  code = response.code
  if code < 200 || code > 299
    msg = "Failed to access zerigo api (http_status=#{code})"
    msg += ", check dns_providers.zerigo.customer_id/email/token in rubber.yml" if code == 401
    raise msg
  end
  return response
end

#create_host(opts) ⇒ Object



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

def create_host(opts)
  host = opts_to_host(opts, new_host())
  check_status self.class.post("/zones/#{@zone['id']}/hosts.xml", :body => {:host => host})
end

#delete_host(host_id) ⇒ Object



76
77
78
# File 'lib/rubber/dns/zerigo.rb', line 76

def delete_host(host_id)
  check_status self.class.delete("/zones/#{@zone['id']}/hosts/#{host_id}.xml")
end

#find_host_records(opts = {}) ⇒ Object



39
40
41
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
# File 'lib/rubber/dns/zerigo.rb', line 39

def find_host_records(opts={})
  result = []
  hn = opts[:host]
  ht = opts[:type]
  hd = opts[:data]
  has_host = hn && hn != '*'

  url = "/zones/#{@zone['id']}/hosts.xml"
  if has_host
    url << "?fqdn="
    url << "#{hn}." if hn.strip.size > 0
    url << "#{@domain}"
  end
  hosts = self.class.get(url)

  # returns 404 on not found, so don't check status
  hosts = check_status hosts unless has_host

  hosts['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 << host_to_opts(h) if keep
  end if hosts['hosts']

  return result
end

#refreshObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubber/dns/zerigo.rb', line 80

def refresh
  zone_id = @zone['id'] rescue nil
  if zone_id
    @zone = check_status self.class.get("/zones/#{zone_id}.xml")
  else
    zones = check_status self.class.get('/zones.xml')
    @zone = zones["zones"].find {|z| z["domain"] == @domain }
  end
  if ! @zone
    zone = new_zone()
    zone['domain'] = @domain
    zones = check_status self.class.post('/zones.xml', :body => {:zone => zone})
    @zone = zones['zone']
  end
end

#update_host(host_id, opts) ⇒ Object



71
72
73
74
# File 'lib/rubber/dns/zerigo.rb', line 71

def update_host(host_id, opts)
  host = opts_to_host(opts, new_host())
  check_status self.class.put("/zones/#{@zone['id']}/hosts/#{host_id}.xml", :body => {:host => host})
end

#zone_recordObject



96
97
98
# File 'lib/rubber/dns/zerigo.rb', line 96

def zone_record
  return @zone
end