Class: Bosh::Director::PowerDns

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/dns/powerdns.rb

Constant Summary collapse

SOA =
'localhost hostmaster@localhost 0 10800 604800 30'
TTL_5M =
300
TTL_4H =
3600 * 4
TTL_5H =
3600 * 5

Instance Method Summary collapse

Constructor Details

#initialize(domain_name, logger) ⇒ PowerDns

Returns a new instance of PowerDns.



8
9
10
11
# File 'lib/bosh/director/dns/powerdns.rb', line 8

def initialize(domain_name, logger)
  @domain_name = domain_name
  @logger = logger
end

Instance Method Details

#create_or_update_dns_records(dns_record_name, ip_address) ⇒ Object



31
32
33
34
# File 'lib/bosh/director/dns/powerdns.rb', line 31

def create_or_update_dns_records(dns_record_name, ip_address)
  create_or_update_record(dns_record_name, ip_address, TTL_5M, 'A')
  update_ptr_record(dns_record_name, ip_address)
end

#create_or_update_nameserver(ip_address) ⇒ Object



24
25
26
27
28
29
# File 'lib/bosh/director/dns/powerdns.rb', line 24

def create_or_update_nameserver(ip_address)
  create_or_update_domain
  create_or_update_record(@domain_name, SOA, TTL_5M, 'SOA')
  create_or_update_record(@domain_name, "ns.#{@domain_name}", TTL_4H, 'NS')
  create_or_update_record("ns.#{@domain_name}", ip_address, TTL_5H, 'A')
end

#delete(record_pattern) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bosh/director/dns/powerdns.rb', line 41

def delete(record_pattern)
  records = find_dns_records_by_pattern(record_pattern)

  # delete A records and collect all IPs for later
  ips = []
  records.each do |record|
    ips << record.content
    Models::Dns::Record.grep(:content, record.name).each do |ptr|
      @logger.info("Deleting reverse DNS record: #{ptr.name} -> #{ptr.content}")
      ptr.destroy
    end
    @logger.info("Deleting DNS record: #{record.name}")
    record.destroy
  end
end

#find_dns_record(dns_record_name, ip_address) ⇒ Object



13
14
15
# File 'lib/bosh/director/dns/powerdns.rb', line 13

def find_dns_record(dns_record_name, ip_address)
  Models::Dns::Record.find(name: dns_record_name, type: 'A', content: ip_address)
end

#find_dns_records_by_ip(ip_address) ⇒ Object



17
18
19
20
21
22
# File 'lib/bosh/director/dns/powerdns.rb', line 17

def find_dns_records_by_ip(ip_address)
  domain_id = find_domain_id
  return [] unless domain_id

  Models::Dns::Record.filter(domain_id: domain_id, type: 'A', content: ip_address)
end

#find_dns_records_by_pattern(record_pattern) ⇒ Object



36
37
38
39
# File 'lib/bosh/director/dns/powerdns.rb', line 36

def find_dns_records_by_pattern(record_pattern)
  records = Models::Dns::Record.grep(:name, record_pattern)
  records.filter(:domain_id => find_domain_id).all
end