Class: Roadworker::Route53Wrapper::ResourceRecordSetWrapper

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/roadworker/route53-wrapper.rb

Overview

ResourceRecordSetCollectionWrapper

Instance Method Summary collapse

Methods included from Log

#log

Constructor Details

#initialize(resource_record_set, hosted_zone, options) ⇒ ResourceRecordSetWrapper

Returns a new instance of ResourceRecordSetWrapper.



206
207
208
209
210
# File 'lib/roadworker/route53-wrapper.rb', line 206

def initialize(resource_record_set, hosted_zone, options)
  @resource_record_set = resource_record_set
  @hosted_zone = hosted_zone
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



387
388
389
# File 'lib/roadworker/route53-wrapper.rb', line 387

def method_missing(method_name, *args)
  @resource_record_set.send(method_name, *args)
end

Instance Method Details

#deleteObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/roadworker/route53-wrapper.rb', line 319

def delete
  if self.type =~ /\A(SOA|NS)\z/i
    hz_name = (@hosted_zone.name || @options.hosted_zone_name).downcase.sub(/\.\z/, '')
    rrs_name = @resource_record_set.name.downcase.sub(/\.\z/, '')
    return if hz_name == rrs_name
  end

  log(:info, 'Delete ResourceRecordSet', :red) do
    log_id = [self.name, self.type].join(' ')
    rrset_setid = self.set_identifier
    rrset_setid ? (log_id + " (#{rrset_setid})") : log_id
  end

  unless @options.dry_run
    @options.route53.change_resource_record_sets(
      hosted_zone_id: @hosted_zone.id,
      change_batch: {
        changes: [
          {
            action: 'DELETE',
            resource_record_set: @resource_record_set,
          },
        ],
      },
    )
    @options.updated = true
  end
end

#dns_nameObject



353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/roadworker/route53-wrapper.rb', line 353

def dns_name
  alias_target = @resource_record_set.alias_target || {}
  dns_name = alias_target[:dns_name]

  if dns_name
    [
      dns_name,
      Aws::Route53.normalize_dns_name_options(alias_target),
    ]
  else
    nil
  end
end

#dns_name=(value) ⇒ Object



367
368
369
370
371
372
373
374
# File 'lib/roadworker/route53-wrapper.rb', line 367

def dns_name=(value)
  if value
    dns_name, dns_name_opts = value
    @resource_record_set.alias_target = Aws::Route53.dns_name_to_alias_target(dns_name, dns_name_opts, @hosted_zone.id, @hosted_zone.name || @options.hosted_zone_name)
  else
    @resource_record_set.alias_target = nil
  end
end

#eql?(expected_record) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/roadworker/route53-wrapper.rb', line 212

def eql?(expected_record)
  Route53Wrapper::RRSET_ATTRS_WITH_TYPE.all? do |attribute|
    expected = expected_record.public_send(attribute)
    expected = expected.sort_by {|i| i.to_s } if expected.kind_of?(Array)
    expected = nil if expected.kind_of?(Array) && expected.empty?
    actual = self.public_send(attribute)
    actual = actual.sort_by {|i| i.to_s } if actual.kind_of?(Array)
    actual = nil if actual.kind_of?(Array) && actual.empty?

    if attribute == :geo_location and actual
      actual = Hash[actual.each_pair.select {|k, v| not v.nil? }]
    end

    if !expected and !actual
      true
    elsif expected and actual
      case attribute
      when :health_check
        if actual[:alarm_identifier]
          actual[:alarm_identifier] = actual[:alarm_identifier].to_h
        end
      when :dns_name
        expected[0] = expected[0].downcase.sub(/\.\z/, '')
        actual[0] = actual[0].downcase.sub(/\.\z/, '')

        if expected[0] !~ /\Adualstack\./i and actual[0] =~ /\Adualstack\./i
          log(:warn, "`dualstack` prefix is used in the actual DNS name", :yellow) do
            log_id = [self.name, self.type].join(' ')
            rrset_setid = self.set_identifier
            rrset_setid ? (log_id + " (#{rrset_setid})") : log_id
          end

          actual[0].sub!(/\Adualstack\./i, '')
        end
      end

      (expected == actual)
    else
      false
    end
  end
end

#health_checkObject



376
377
378
# File 'lib/roadworker/route53-wrapper.rb', line 376

def health_check
  @options.health_checks[@resource_record_set.health_check_id]
end

#health_check=(check) ⇒ Object



380
381
382
383
# File 'lib/roadworker/route53-wrapper.rb', line 380

def health_check=(check)
  health_check_id = check ? @options.health_checks.find_or_create(check) : nil
  @resource_record_set.health_check_id = health_check_id
end

#nameObject



348
349
350
351
# File 'lib/roadworker/route53-wrapper.rb', line 348

def name
  value = @resource_record_set.name
  value ? value.gsub("\\052", '*') : value
end

#update(expected_record) ⇒ Object



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
# File 'lib/roadworker/route53-wrapper.rb', line 255

def update(expected_record)
  log_id_proc = proc do
    log_id = [self.name, self.type].join(' ')
    rrset_setid = self.set_identifier
    rrset_setid ? (log_id + " (#{rrset_setid})") : log_id
  end

  log(:info, 'Update ResourceRecordSet', :green, &log_id_proc)

  resource_record_set_prev = @resource_record_set.dup
  Route53Wrapper::RRSET_ATTRS_WITH_TYPE.each do |attribute|
    expected = expected_record.send(attribute)
    expected = expected.sort_by {|i| i.to_s } if expected.kind_of?(Array)
    expected = nil if expected.kind_of?(Array) && expected.empty?
    actual = self.send(attribute)
    actual = actual.sort_by {|i| i.to_s } if actual.kind_of?(Array)
    actual = nil if actual.kind_of?(Array) && actual.empty?

    # XXX: Fix for diff
    if attribute == :health_check and actual
      if (actual[:child_health_checks] || []).empty?
        actual[:child_health_checks] = []
      end

      if (actual[:regions] || []).empty?
        actual[:regions] = []
      end
    end

    if (expected and !actual) or (!expected and actual)
      log(:info, "  #{attribute}:\n".green + Roadworker::Utils.diff(actual, expected, :color => @options.color, :indent => '    '), false)
      unless @options.dry_run
        self.send(:"#{attribute}=", expected)
      end
    elsif expected and actual
      if expected != actual
        log(:info, "  #{attribute}:\n".green + Roadworker::Utils.diff(actual, expected, :color => @options.color, :indent => '    '), false)
        unless @options.dry_run
          self.send(:"#{attribute}=", expected)
        end
      end
    end
  end

  unless @options.dry_run
    @options.route53.change_resource_record_sets(
      hosted_zone_id: @hosted_zone.id,
      change_batch: {
        changes: [
          {
            action: 'DELETE',
            resource_record_set: resource_record_set_prev,
          },
          {
            action: 'CREATE',
            resource_record_set: @resource_record_set,
          },
        ],
      },
    )
    @options.updated = true
  end
end