-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute53.rb
71 lines (59 loc) · 1.55 KB
/
route53.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'aws-sdk'
require 'yaml'
CONFIG = YAML.load(File.read('config.yml'))
RECORDS_LIMIT = 1_000
# Class describes the service for retrieving list of records
# for desired DNS zone.
# Example:
# service = ShowRecordsService.new('google.com')
# service.call('MX')
class ShowRecordsService
def initialize(required_zone, region = 'us-west-2')
@required_zone = required_zone
@region = region
end
def call(record_type = 'A')
target_zones.each do |zone|
zone_records = client.list_resource_record_sets(
hosted_zone_id: zone.id,
max_items: RECORDS_LIMIT
).resource_record_sets
print_records(zone, records_by_type(zone_records, record_type))
end
end
def records_by_type(records, type)
records.select { |record| record.type == type }
end
def target_zones
all_zones.select { |zone| zone.name.match @required_zone }
end
def all_zones
client.list_hosted_zones.hosted_zones
end
private
def client
@client ||= Aws::Route53::Client.new(
credentials: credentials,
region: @region
)
end
def credentials
@credentials ||= Aws::Credentials.new(
CONFIG['aws']['key'],
CONFIG['aws']['secret']
)
end
def print_records(zone, records)
puts "=== Zone '#{zone.name}' (ID: #{zone.id}) ==="
records.each do |record|
puts [
record.name,
record.type,
record.resource_records.map(&:value).join(' '),
record.ttl
].join(',')
end
end
end
# Run the service.
ShowRecordsService.new('syseng-interview.amplify').call