summaryrefslogtreecommitdiff
path: root/lib/puppet/provider/dns_record2/named.rb
blob: 13fe5ee30fa8426d33ac1e8b838543faf14122f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
70
71
72
73
74
75
76
77
78
79
Puppet::Type.type(:dns_record2).provide(:named) do
  def self.instances
    []
  end

  # Create and destroy don't do anything on their own,
  # See comment in type
  def create; end

  def destroy; end

  def exists?
    record != nil
  end

  def type
    resource[:type]
  end

  def value
    t = record
    case t
    when NilClass
      :absent
    when String
      t
    else
      "INVALID VALUE (#{t.class}, #{t})"
    end
  end

  # TODO: should we have this?
  def value=(v); end

  def key
    resource[:key]
  end

  # def key=(v)
  #   resource[:key] = v
  # end

  def zone
    resource[:zone]
  end

  def full_key
    if resource[:key] == '@'
      resource[:zone]
    elsif resource[:key][-1] == '.'
      resource[:key]
    else
      "#{resource[:key]}.#{resource[:zone]}"
    end
  end

  def record
    lines = `named-checkzone -j -q -D #{zone} /var/named/zones/#{zone}db`
            .split("\n")
            .map { |line| line.gsub(%r{[[:space:]]+}, ' ').split(' ') }

    matches =
      lines
      .filter { |line| line[0] == full_key && line[3].to_sym == resource[:type] }
      .map { |line| line[(4..)].join(' ') }

    if matches.empty?
      nil
    elsif matches.length == 1
      matches[0]
    else
      matches
    end
  end

  def cls
    'IN'
  end
end