class Awful::Route53

Public Instance Methods

alias(name, target) click to toggle source
# File lib/awful/route53.rb, line 127
def alias(name, target)
  if options[:hosted_zone_id]
    dns_name = target
    hosted_zone_id = options[:hosted_zone_id]
  else
    dns_name, hosted_zone_id = send("get_#{options[:resource]}_dns", target)
  end
  action = options[:delete] ? 'DELETE' : 'UPSERT'
  params = {
    hosted_zone_id: get_zone_by_name(get_domain(name)),
    change_batch: {
      changes: [
        {
          action: action,
          resource_record_set: {
            name:           name,
            type:           options[:type],
            set_identifier: options[:set_identifier],
            weight:         options[:weight],
            alias_target: {
              hosted_zone_id:         hosted_zone_id,
              dns_name:               dns_name,
              evaluate_target_health: false
            }
          }
        }
      ]
    }
  }
  route53.change_resource_record_sets(params).output do |response|
    puts YAML.dump(stringify_keys(response.change_info.to_hash))
  end
end
change(id) click to toggle source
# File lib/awful/route53.rb, line 190
def change(id)
  route53.get_change(id: id).change_info.output do |info|
    puts YAML.dump(stringify_keys(info.to_hash))
  end
end
cname(name, target) click to toggle source
# File lib/awful/route53.rb, line 163
def cname(name, target)
  params = {
    hosted_zone_id: get_zone_by_name(get_domain(name)),
    change_batch: {
      changes: [
        {
          action: 'UPSERT',
          resource_record_set: {
            name: name,
            type: 'CNAME',
            resource_records: [
              {
                value: target
              }
            ],
            ttl: options[:ttl]
          }
        }
      ]
    }
  }
  route53.change_resource_record_sets(params).output do |response|
    puts YAML.dump(stringify_keys(response.change_info.to_hash))
  end
end
dump(name) click to toggle source
# File lib/awful/route53.rb, line 95
def dump(name)
  zone = name.split('.').last(2).join('.')
  name += '.' unless name.end_with?('.') # output will have dot at end
  params = {
    hosted_zone_id:    get_zone_by_name(zone),
    start_record_name: name,
    start_record_type: options[:type],
    max_items:         options[:max_items]
  }
  route53.list_resource_record_sets(params).resource_record_sets.select do |record|
    record.name == name
  end.output do |records|
    records.each do |record|
      puts YAML.dump(stringify_keys(record.to_hash))
    end
  end
end
get_domain(name) click to toggle source

extract domain part of dns name

# File lib/awful/route53.rb, line 15
def get_domain(name)
  name.split('.').last(2).join('.')
end
get_elb_dns(name) click to toggle source

return dns name and hosted zone id

# File lib/awful/route53.rb, line 41
def get_elb_dns(name)
  desc = elb.describe_load_balancers(load_balancer_names: [name]).load_balancer_descriptions[0]
  ['dualstack.' + desc.dns_name, desc.canonical_hosted_zone_name_id]
end
get_zone_by_name(name) click to toggle source

get hosted zone id from domain name

# File lib/awful/route53.rb, line 20
def get_zone_by_name(name)
  if name.match(/^Z[A-Z0-9]{13}$/) # id is 14 char upcase string starting with Z
    name
  else
    matches = route53.list_hosted_zones.hosted_zones.select do |zone|
      zone.name.match(name)
    end
    case matches.size
    when 0
      say "no hosted zones matching #{name}", :red
      exit
    when 1
      matches.first.id
    else
      say "ambiguous hosted zone, matches: #{matches.map(&:name).join(', ')}", :yellow
      exit
    end
  end
end
ls(name = /./) click to toggle source
# File lib/awful/route53.rb, line 49
def ls(name = /./)
  route53.list_hosted_zones.hosted_zones.select do |zone|
    zone.name.match(name)
  end.output do |list|
    if options[:long]
      print_table list.map { |z| [z.name, z.id, z.resource_record_set_count, z.config.comment] }
    else
      puts list.map(&:name)
    end
  end
end
records(zone) click to toggle source
# File lib/awful/route53.rb, line 66
def records(zone)
  ## match on given record types
  include_type = options[:type] ? ->(type) { options[:type].include?(type) } : ->(_) { true }

  ## match on given record names
  names = options.fetch('name', []).map { |name| name.gsub(/([^\.])$/, '\1.') } # append . to names if missing
  include_name = options[:name] ? ->(name) { names.include?(name) } : ->(_) { true }

  route53.list_resource_record_sets(
    hosted_zone_id: get_zone_by_name(zone),
    max_items:      options[:max_items]
  ).resource_record_sets.select do |rrset|
    include_type.call(rrset.type) and include_name.call(rrset.name)
  end.output do |records|
    if options[:long]
      print_table records.map { |r|
        dns_name = r.alias_target.nil? ? [] : ['ALIAS ' + r.alias_target.dns_name]
        values = r.resource_records.map(&:value)
        [r.name, r.type, (dns_name + values).join(', ')]
      }
    else
      puts records.map(&:name)
    end
  end
end
route53() click to toggle source
# File lib/awful/route53.rb, line 10
def route53
  @route53 ||= Aws::Route53::Client.new
end