summaryrefslogtreecommitdiff
path: root/lib/puppet/provider/dns_record2/named.rb
blob: 2060c628b6f4bb395965e956c539271a1e6dcc07 (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
Puppet::Type.type(:dns_record2).provide(:named) do
  def self.instances
    raise _('Listing DNS records not supported.')
  end

  def exists?
    record != nil
  end

  def type
    resource[:type]
  end

  def value
    record
  end

  def value=(new)
    print("Updating value to #{new}\n")
  end

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

  def key
    resource[:key]
  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 = `#{resource[: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(' ') }

    matches

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

  def cls
    'IN'
  end
end