class R53z::Cli

Public Class Methods

new(options:, args:) click to toggle source
# File lib/r53z/cli.rb, line 8
def initialize(options:, args:)
  section = options[:section] || 'default'
  config_file = options[:credentials]
  creds = R53z::Config.new(config_file)
  @client = R53z::Client.new(section, creds)

  # XXX Dispatch table seems smarter...can't figure out how to call methods based
  # directly on hash keys at the moment.
  if options[:export]
    unless options[:export].is_a? String
      exit_now! "Export must have a valid directory path for dump files."
    end
    unless Dir.exists?(File.expand_path(options[:export]))
      exit_now! "Directory " + options[:export] + " does not exist."
    end
    export(:options => options, :args => args)
  end

  if options[:restore]
    unless Dir.exists?(File.expand_path(options[:restore]))
      exit_now! "Restore requires a directory containing zone files and optionally one or more zones to restore."
    end
    restore(:options => options, :args => args)
  end

  if options[:list]
    list(options: options, args: args)
  end

  if options[:create]
    create(options)
  end

  if options[:delete]
    if args.empty?
      exit_now! "Delete requires one or more zone names."
    end
    args.each do |name|
      if @client.list(name: name).any?
        @client.delete(name)
      else
        exit_now! "Could not locate zone named " + name
      end
    end
  end

  if options['list-delegation-sets']
    delegation_sets(args)
  end

  if options['create-delegation-sets']
    create_delegation(args)
  end

  if options['delete-delegation-sets']
    if args.empty?
      exit_now! "Deleting delegation sets requires one or more delegation set IDs."
    end
    args.each do |id|
      @client.delete_delegation_set(id: id)
    end
  end

  if options['record-sets']
    if args.empty?
      exit_now! "List record sets requires one or more zone names."
    end
    record_sets(args)
  end

  if options['name-servers']
    dset = @client.get_delegation_set(options['name-servers'])
    puts JSON.pretty_generate(dset.delegation_set[:name_servers])
  end

  if options['list-by-id']
    if args.empty?
      exit_now! "List by ID requires one or more zone IDs."
    end
    list_by_id(args)
  end

  if options['list-records-by-id']
    if args.empty?
      exit_now! "List records by ID requies on or more zone IDs."
    end
    list_records_by_id(args)
  end
end

Public Instance Methods

add_record(options, args) click to toggle source
# File lib/r53z/cli.rb, line 169
def add_record(options, args)
  # Populate record set hash
end
create(options) click to toggle source
# File lib/r53z/cli.rb, line 155
def create(options)
  # Populate a zone hash
  zone_data = {:hosted_zone => { :hosted_zone_config => {}}, :delegation_set =>{}}
  zone_data[:hosted_zone][:name] = options[:create]
  if options[:comment]
    zone_data[:hosted_zone][:config] = {}
    zone_data[:hosted_zone][:config][:comment] = options[:comment]
  end
  if options['delegation-set']
    zone_data[:delegation_set][:id] = options['delegation-set']
  end
  @client.create(info: zone_data)
end
create_delegation(args) click to toggle source
# File lib/r53z/cli.rb, line 193
def create_delegation(args)
  # further validation
  zones = []
  # No zones specified, create a new unassociated delegation set
  if args.empty?
    @client.create_delegation_set
  end

  args.each do |name|
    # Make sure at least one specified zone exists
    if @client.list(name: name)
      zones.push(name)
    else
      puts "Couldn't locate zone " + name unless @client.list(name)
    end
    if zones.empty?
      exit_now! "Couldn't find any of the specified zones."
    end
    zones.each do |zone|
      @client.create_delegation_set(@client.get_zone_id(zone))
    end
  end
end
delegation_sets(args) click to toggle source
# File lib/r53z/cli.rb, line 173
def delegation_sets(args)
  # show them all
  if args.empty?
    sets = @client.list_delegation_sets
    sets.each do |set|
      puts JSON.pretty_generate(set.to_h)
    end
  else # only show specified zones
    args.each do |name|
      dset_id = @client.get_delegation_set_id(name)
      if dset_id
        dset = @client.get_delegation_set(dset_id)
        puts JSON.pretty_generate(dset.delegation_set.to_h)
      else
        exit_now!("Could not find a delegation set for " + name)
      end
    end
  end
end
export(options:, args:) click to toggle source
# File lib/r53z/cli.rb, line 98
def export(options:, args:)
  path = File.expand_path(options[:export])
  # If no zones, dump all zones
  zones = []
  # One zone, multiple, or all?
  if args.empty?
    @client.list(:delegation_set_id => options['delegation-set']).each do |zone|
      zones.push(zone[:name])
    end
  else
    if options['delegation-set']
      puts "--delegation-set is overridden when one or more zones are provided"
    end
    zones = args
  end

  zones.each do |name|
    @client.dump(path, name)
  end
end
list(options:, args:) click to toggle source
# File lib/r53z/cli.rb, line 140
def list(options:, args:)
  if args.any?
    args.each do |name|
      puts JSON.pretty_generate(
        @client.list(
          :name => name,
          :delegation_set_id => options['delegation-set']))
    end
  else
    puts JSON.pretty_generate(
      @client.list(:delegation_set_id => options['delegation-set'])
    )
  end
end
list_by_id(args) click to toggle source
# File lib/r53z/cli.rb, line 233
def list_by_id(args)
  args.each do |id|
    puts JSON.pretty_generate(@client.list_by_id(id).to_hash)
  end
end
list_records_by_id(args) click to toggle source
# File lib/r53z/cli.rb, line 239
def list_records_by_id(args)
  args.each do |id|
    puts JSON.pretty_generate(@client.list_records(id))
  end
end
record_sets(args) click to toggle source
# File lib/r53z/cli.rb, line 217
def record_sets(args)
  args.each do |name|
    zone_id = @client.get_zone_id(name)
    if zone_id
      sets = @client.list_records(@client.get_zone_id(name))
    else
      exit_now!("Could not locate zone " + name)
    end
    if sets
      puts JSON.pretty_generate(sets)
    else
      exit_now!("Could not locate record list for " + name)
    end
  end
end
restore(options:, args:) click to toggle source
# File lib/r53z/cli.rb, line 119
def restore(options:, args:)
  path = File.expand_path(options[:restore])
  # If no zones, restore all zones in directory
  zones = []
  if args.empty?
    # derive list of zones from files in path
    zones = Dir[File.join(path, "*.json")].reject {|n| n.match("zoneinfo")}
  else
    # restore the ones specified
    args.each do |zone|
      zones.push(zone)
    end
  end

  delegation = options['delegation-set'] or nil

  zones.each do |zone|
    @client.restore(path, zone, delegation)
  end
end