summaryrefslogtreecommitdiff
path: root/lib/puppet/type/dns_zone2.rb
blob: 8b3876adf296acb96157cf0f8e5a1925b38380d2 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
require 'puppet/type/file'

Puppet::Type.newtype(:dns_zone2, self_refresh: true) do
  @doc = <<~EOF
  A complete DNS zonefile.

  The SOA record comes backed in, this for two reasons:
  1. A SOA record is REQUIRED in all zonefiles, so it doesn't make
     sense to allow it to be emitted.
  2. This is the only way to ensure that our serial is only updated
     when something else has actually changed in the zone.
  EOF

  ensurable

  def refresh
    catalog.resource("File[#{self[:directory]}/#{self[:name]}db]")[:content] = should_content(1)
  end

  # List of all DNS records (at all)
  #
  # Should ideally be limited to records in this zone here instead of
  # further down.
  def records
    catalog.resources.filter do |r|
      r.is_a?(Puppet::Type.type(:dns_record2))
    end
  end

  def create
    print("Create called\n")
  end

  # Create the file resource for us.
  # We also propagate some parameters and metaparameters to the
  # underlying file here
  def generate
    params = {
      ensure: self[:ensure],
      path: provider.filename,
      # notify: self[:notify],
      validate_cmd: "#{self[:named_checkzone]} #{self[:origin]} %"
    }

    [:owner,
     :group,
     :backup,
     :selinux_ignore_defaults,
     :selrange,
     :selrole,
     :seltype,
     :seluser,
     :show_diff].each do |param|
      params[param] = self[param] unless self[param].nil?
    end

    excluded_metaparams = [:before, :notify, :require, :subscribe, :tag]

    Puppet::Type.metaparams.each do |metaparam|
      unless self[metaparam].nil? || excluded_metaparams.include?(metaparam)
        params[metaparam] = self[metaparam]
      end
    end

    [Puppet::Type.type(:file).new(params)]
  end

  newparam(:named_checkzone) do
    desc <<-EOF
    Absolute path to the named-checkzone binary.
    EOF

    defaultto '/usr/bin/named-checkzone'
  end

  newparam(:named_checkconf) do
    desc <<-EOF
    Absolute pathto the named-checkconf binary
    EOF
    defaultto '/usr/bin/named-checkconf'
  end

  newparam(:rndc) do
    desc <<-EOF
    Absolute path to the rndc binary
    EOF
    defaultto '/usr/bin/rndc'
  end

  # Returning "our" file resource causes errors on the file to
  # propagate out from us (which is NEEDED for `validate_cmd` to
  # work).
  #
  # This always sets the content, but is run is *before* refresh
  # eventns. This means that this works for creation events, but not
  # refresh events. For refresh events we simply generate the content
  # a second time, overriding this instance.
  #
  # Ideally we wouldn't generate content here, but instead in
  # `create`. But that method isn't called
  def eval_generate
    catalog.resource("File[#{self[:directory]}/#{self[:name]}db]")[:content] = should_content(0)
    [catalog.resource("File[#{provider.filename}]")]
  end

  def should_content(serial_change)
    provider.zone_content(records, serial_change)
  end

  newparam(:origin, namevar: true) do
    desc <<-EOF
    Origin parameter of the source.
    Should almost always be the same as the name of the zone.
    EOF
    munge do |value|
      if value[-1] == '.'
        value
      else
        "#{value}."
      end
    end
  end

  newparam(:cls) do
    desc <<-EOF
    DNS class of the zone.
    EOF
    defaultto :IN
  end

  newparam(:view) do
    desc <<-EOF
    Named view this zone belongs to.
    TODO: support views
    EOF
    defaultto '_default'
  end

  newparam(:type) do
    desc <<-EOF
    The type of the zone.
    TODO: support types
    Currently only master is tested.
    EOF

    newvalues(:master, :slave,
              :mirror, :hint, :stub, :static_stub, :forward, :redirect)
    aliasvalue :primary, :master
    aliasvalue :secondary, :slave
    defaultto :master
  end

  newparam(:default_ttl) do
    desc <<-EOF
    TTL for all records in zone without an explicit TTL set.
    EOF
    defaultto '300'
  end

  newparam(:mname) do
    desc <<-EOF
    Domain name of zone master.

    Part of the SOA record.
    EOF
    munge do |value|
      if value[-1] == '.'
        value
      else
        "#{value}."
      end
    end
  end

  newparam(:rname) do
    desc <<-EOF
    Email to the zone admin, but with the '@' replaced with a period ('.')

    Part of the SOA record.
    EOF
    munge do |value|
      if value[-1] == '.'
        value
      else
        "#{value}."
      end
    end
  end

  newparam(:soa_ttl) do
    desc <<-EOF
    TTL of the SOA record.
    EOF
  end

  newparam(:refresh) do
    desc <<-EOF
    Refresh value in the SOA record.
    EOF
  end

  newparam(:retry) do
    desc <<-EOF
    Retry value in the SOA record.
    EOF
  end

  newparam(:expire) do
    desc <<-EOF
    Expire value in the SOA record.
    EOF
  end

  newparam(:negative_ttl) do
    desc <<-EOF
    Negative TTL value in the SOA value.

    This file is sometimes call Minimum TTL due to historical reasons.
    EOF
  end

  newparam(:owner, parent: Puppet::Type::File::Owner) do
    desc <<-EOF
    Owner of the zonefile.
    EOF
  end

  newparam(:group, parent: Puppet::Type::File::Group) do
    desc <<-EOF
    Group of the zonefile.
    EOF
  end

  newparam(:backup) do
    desc <<-EOF
    Should the pre-existing file be backed up.

    See the file resource's documentation for details.
    EOF

    validate do |value|
      unless [TrueClass, FalseClass, String].include?(value.class)
        raise ArgumentError, _('Backup must be a Boolean or String')
      end
    end
  end

  newparam(:selinux_ignore_defaults, boolean: true, parent: Puppet::Parameter::Boolean) do
    desc <<-EOF
    See the file resource's documentation for details.
    EOF
  end

  newparam(:selrange) do
    desc <<-EOF
    See the file resource's documentation for details.
    EOF

    validate do |value|
      unless value.is_a?(String)
        raise ArgumentError, _('Selrange must be a String')
      end
    end
  end

  newparam(:selrole) do
    desc <<-EOF
    See the file resource's documentation for details.
    EOF

    validate do |value|
      unless value.is_a?(String)
        raise ArgumentError, _('Selrole must be a String')
      end
    end
  end

  newparam(:seltype) do
    desc <<-EOF
    See the file resource's documentation for details.
    EOF

    validate do |value|
      unless value.is_a?(String)
        raise ArgumentError, _('Seltype must be a String')
      end
    end
  end

  newparam(:seluser) do
    desc <<-EOF
    See the file resource's documentation for details.
    EOF

    validate do |value|
      unless value.is_a?(String)
        raise ArgumentError, _('Seluser must be a String')
      end
    end
  end

  newparam(:show_diff, boolean: true, parent: Puppet::Parameter::Boolean) do
    desc <<-EOF
    See the file resource's documentation for details.
    EOF
  end

  newparam(:directory) do
    desc <<-EOF
    Directory in which zone files will be stored.
    EOF

    defaultto '/var/named/zones'
  end

  autorequire(:file) { || [value(:directory)] }
  autorequire(:file) { || [provider.filename] }
end