class Roadworker::Batch

Attributes

dry_run[R]
health_checks[R]
hosted_zone[R]
logger[R]
operations[R]

Public Class Methods

new(hosted_zone, dry_run:, logger:, health_checks:) click to toggle source

@param [Roadworker::Route53Wrapper::HostedzoneWrapper] hosted_zone @param [Roadworker::HealthCheck] health_checks

# File lib/roadworker/batch.rb, line 7
def initialize(hosted_zone, dry_run:, logger:, health_checks:)
  @hosted_zone = hosted_zone
  @dry_run = dry_run
  @logger = logger
  @health_checks = health_checks

  @operations = []
end

Public Instance Methods

create(rrset) click to toggle source

@param [OpenStruct] rrset Roadworker::DSL::ResourceRecordSet#result

# File lib/roadworker/batch.rb, line 21
def create(rrset)
  add_operation Create, rrset
end
delete(rrset) click to toggle source

@param [Roadworker::Route53Wrapper::ResourceRecordSetWrapper] rrset

# File lib/roadworker/batch.rb, line 31
def delete(rrset)
  add_operation Delete, rrset
end
inspect() click to toggle source
# File lib/roadworker/batch.rb, line 48
def inspect
  "#<#{self.class.name}: #{operations.size} operations>"
end
request!(route53) click to toggle source

@param [Aws::Route53::Client] route53 @return [Boolean] updated

# File lib/roadworker/batch.rb, line 37
def request!(route53)
  sorted_operations = operations.sort_by(&:sort_key)

  batches = slice_operations(sorted_operations)
  batches.each_with_index do |batch, i|
    dispatch_batch!(route53, batch, i, batches.size)
  end

  sorted_operations.any? { |op| !op.changes.empty? }
end
to_s() click to toggle source
# File lib/roadworker/batch.rb, line 52
def to_s
  inspect
end
update(rrset) click to toggle source

@param [OpenStruct] rrset Roadworker::DSL::ResourceRecordSet#result

# File lib/roadworker/batch.rb, line 26
def update(rrset)
  add_operation Update, rrset
end

Private Instance Methods

add_operation(klass, rrset) click to toggle source
# File lib/roadworker/batch.rb, line 99
def add_operation(klass, rrset)
  assert_record_name rrset
  operations << klass.new(hosted_zone, rrset, health_checks: health_checks, dry_run: dry_run, logger: logger)
  self
end
assert_record_name(record) click to toggle source
# File lib/roadworker/batch.rb, line 105
def assert_record_name(record)
  unless record.name.downcase.sub(/\.$/,'').end_with?(hosted_zone.name.sub(/\.$/,''))
    raise ArgumentError, "#{record.name.inspect} isn't under hosted zone name #{hosted_zone.name.inspect}" 
  end
end
dispatch_batch!(route53, batch, i, total) click to toggle source
# File lib/roadworker/batch.rb, line 58
def dispatch_batch!(route53, batch, i, total)
  changes = batch.flat_map(&:changes)
  return if changes.empty?

  page = total > 1 ? " | #{i+1}/#{total}" : nil
  log(:info, "=== Change batch: #{hosted_zone.name} | #{hosted_zone.id}#{hosted_zone.vpcs.empty? ? '' : ' - private'}#{page}", :bold)
  batch.each do |operation|
    operation.diff!()
  end

  if dry_run
    log(:info, "---", :bold, dry_run: false)
  else
    change = route53.change_resource_record_sets(
      hosted_zone_id: hosted_zone.id,
      change_batch: {
        changes: changes,
      },
    )
    log(:info, "--> Change submitted: #{change.change_info.id}", :bold)
  end
  log(:info, "", :bold, dry_run: false)
end
slice_operations(ops) click to toggle source

Slice operations to batches, per 32,000 characters in “Value” or per 1,000 operations.

# File lib/roadworker/batch.rb, line 83
def slice_operations(ops)
  total_value_size = 0
  total_ops = 0
  ops.slice_before do |op|
    total_value_size += op.value_size
    total_ops += op.op_size
    if total_value_size > 32000 || total_ops > 1000
      total_value_size = op.value_size
      total_ops = op.op_size
      true
    else
      false
    end
  end.to_a
end