Class: AwsUtils::Route53ListResourceRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/awsutils/r53ls.rb

Instance Method Summary collapse

Constructor Details

#initializeRoute53ListResourceRecord

Returns a new instance of Route53ListResourceRecord.



32
33
34
# File 'lib/awsutils/r53ls.rb', line 32

def initialize
  @opts = parse_opts
end

Instance Method Details

#apex?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/awsutils/r53ls.rb', line 28

def apex?
  @opts[:name].split('.')[-3].nil? ? true : false
end

#connectionObject



8
9
10
# File 'lib/awsutils/r53ls.rb', line 8

def connection
  @connection ||= Fog::DNS::AWS.new
end

#display_record(record) ⇒ Object

def initialize( args )



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/awsutils/r53ls.rb', line 36

def display_record(record)
  if @opts[:format] == 'json'
    puts JSON.pretty_generate(zone_to_json([record]).first)
  else
    puts 'Name: ' + record.name
    puts 'Type: ' + record.type
    puts 'TTL: ' + record.ttl
    puts record.value.count < 2 ? 'Value:' : 'Values:'
    record.value.each { |rr| puts "  #{rr}" }
  end
end

#list_allObject



76
77
78
79
80
81
82
# File 'lib/awsutils/r53ls.rb', line 76

def list_all
  if @opts[:format] == 'json'
    puts JSON.pretty_generate(zone_to_json(zone.records))
  else
    print_table
  end
end

#parse_optsObject

def connection



12
13
14
15
16
17
18
# File 'lib/awsutils/r53ls.rb', line 12

def parse_opts
  opts = Trollop.options do
    opt :format, 'Output format', default: 'table'
  end
  opts[:name] = ARGV.last
  opts
end


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/awsutils/r53ls.rb', line 64

def print_table
  zone.records.each do |r|
    printf(
      "%-40s%-8s%-8s%-40s\n",
      r.name,
      r.type,
      r.ttl,
      r.value.join(' ')
    )
  end
end

#record_by_nameObject



48
49
50
51
# File 'lib/awsutils/r53ls.rb', line 48

def record_by_name
  name = @opts[:name].split('.').join('.') + '.'
  zone.records.find { |r| r.name == name }
end

#runObject



84
85
86
87
88
89
90
# File 'lib/awsutils/r53ls.rb', line 84

def run
  if apex?
    list_all
  else
    display_record(record_by_name)
  end
end

#zoneObject

def parse_opts( args )



20
21
22
# File 'lib/awsutils/r53ls.rb', line 20

def zone
  @zone ||= connection.zones.all('domain' => zone_name).first
end

#zone_nameObject



24
25
26
# File 'lib/awsutils/r53ls.rb', line 24

def zone_name
  @zone_name ||= @opts[:name].split('.')[-2..-1].join('.') + '.'
end

#zone_to_json(zone_records) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/awsutils/r53ls.rb', line 53

def zone_to_json(zone_records)
  zone_records.map do |r|
    {
      'name' => r.name,
      'type' => r.type,
      'ttl' => r.ttl,
      'value' => r.value
    }
  end
end