Class: Rubber::Dns::Nettica

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

Instance Attribute Summary

Attributes inherited from Base

#env

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(env) ⇒ Nettica

Returns a new instance of Nettica.



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

def initialize(env)
  super(env)
  @client = ::Nettica::Client.new(env.user, env.password)
end

Instance Method Details

#check_status(response) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubber/dns/nettica.rb', line 17

def check_status(response)
  code = case
    when response.respond_to?(:status)
      response.status
    when response.respond_to?(:result)
      response.result.status
    else
      500
  end
  if code < 200 || code > 299
    msg = "Failed to access nettica api (http_status=#{code})"
    msg += ", check dns_providers.nettica.user/password in rubber.yml" if code == 401
    raise msg
  end
  return response
end

#create_host_record(opts = {}) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rubber/dns/nettica.rb', line 69

def create_host_record(opts = {})
  opts = setup_opts(opts, [:host, :data, :domain, :type, :ttl])
  find_or_create_zone(opts[:domain])
  opts_to_hosts(opts).each do |host|
    check_status @client.add_record(host)
  end
end

#destroy_host_record(opts = {}) ⇒ Object



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

def destroy_host_record(opts = {})
  opts = setup_opts(opts, [:host, :domain])

  find_hosts(opts).each do |h|
    check_status @client.delete_record(h)
  end
end

#find_host_records(opts = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubber/dns/nettica.rb', line 57

def find_host_records(opts = {})
  hosts = find_hosts(opts)
  group = {}
  hosts.each do |h|
    key = "#{h.hostName}.#{h.domainName} #{h.recordType}"
    group[key] ||= []
    group[key] << h
  end
  result = group.values.collect {|h| hosts_to_opts(h).merge(:domain => opts[:domain])}
  return result
end

#find_hosts(opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubber/dns/nettica.rb', line 34

def find_hosts(opts = {})
  opts = setup_opts(opts, [:host, :domain])

  result = []
  hn = opts[:host]
  ht = opts[:type]

  domain_info = find_or_create_zone(opts[:domain])

  domain_info.record.each do |h|
    keep = true
    if hn && h.hostName != hn && hn != '*'
      keep = false
    end
    if ht && h.recordType != ht && ht != '*'
      keep = false
    end
    result << h if keep
  end

  return result
end

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



85
86
87
88
89
90
91
92
93
94
# File 'lib/rubber/dns/nettica.rb', line 85

def update_host_record(old_opts = {}, new_opts = {})
  old_opts = setup_opts(old_opts, [:host, :domain, :type])
  new_opts = setup_opts(new_opts, [:host, :domain, :type, :data])

  # Tricky to update existing hosts since nettica needs a separate host
  # entry for multiple records of same type (MX, etc), so take the easy
  # way out and destroy/create instead of update
  destroy_host_record(old_opts)
  create_host_record(new_opts)
end