Class: Zonesync::Route53

Inherits:
Provider show all
Defined in:
lib/zonesync/route53.rb

Instance Attribute Summary

Attributes inherited from Provider

#config

Instance Method Summary collapse

Methods inherited from Provider

#add_with_duplicate_handling, #diff, #diff!, #diffable_records, from, #initialize, #manifest, #records, #write

Constructor Details

This class inherits a constructor from Zonesync::Provider

Instance Method Details

#add(record) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/zonesync/route53.rb', line 85

def add(record)
  add_with_duplicate_handling(record) do
    begin
      # Route53 requires all records with the same name/type to be in a single record set
      existing_records = records.select do |r|
        r.name == record.name && r.type == record.type
      end
      all_records = (existing_records + [record]).uniq

      action = existing_records.empty? ? "CREATE" : "UPSERT"
      change_records(action, all_records)
      invalidate_cache!
    rescue RuntimeError => e
      if e.message.include?("RRSet already exists")
        raise DuplicateRecordError.new(record, "Route53 duplicate record error")
      else
        raise
      end
    end
  end
end

#change(old_record, new_record) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zonesync/route53.rb', line 70

def change(old_record, new_record)
  new_set = if old_record.manifest? || old_record.checksum?
    # our metadata records are singletons, always replace
    [new_record]
  else
    existing_records = records.select do |r|
      r.name == old_record.name && r.type == old_record.type
    end
    (existing_records.reject { |r| r.identical_to?(old_record) } + [new_record]).uniq
  end

  change_records("UPSERT", new_set)
  invalidate_cache!
end

#readObject



10
11
12
13
14
15
16
17
18
# File 'lib/zonesync/route53.rb', line 10

def read
  @read ||= begin
    doc = REXML::Document.new(http.get(""))
    records = doc.elements.collect("*/ResourceRecordSets/ResourceRecordSet") do |record_set|
      to_records(record_set)
    end.flatten.sort
    records.map(&:to_s).join("\n") + "\n"
  end
end

#remove(record) ⇒ Object



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
# File 'lib/zonesync/route53.rb', line 20

def remove(record)
  # Route53 requires all records with the same name/type to be managed together as a record set
  existing_records = records.select do |r|
    r.name == record.name && r.type == record.type
  end

  if existing_records.length == 1
    change_record("DELETE", record)
  else
    remaining_records = existing_records.reject { |r| r == record }

    grouped = [
      [existing_records, "DELETE"],
      *(remaining_records.any? ? [[remaining_records, "CREATE"]] : [])
    ]

    http.post("", ERB.new("      <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n      <ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\">\n        <ChangeBatch>\n          <Changes>\n            <%- grouped.each do |records_list, action| -%>\n            <%- records_grouped = records_list.group_by { |r| [r.name, r.type, r.ttl] } -%>\n            <%- records_grouped.each do |(name, type, ttl), group_records| -%>\n            <Change>\n              <Action><%= action %></Action>\n              <ResourceRecordSet>\n                <Name><%= name %></Name>\n                <Type><%= type %></Type>\n                <TTL><%= ttl %></TTL>\n                <ResourceRecords>\n                  <%- group_records.each do |group_record| -%>\n                  <ResourceRecord>\n                    <Value><%= rdata_for_api(group_record) %></Value>\n                  </ResourceRecord>\n                  <%- end -%>\n                </ResourceRecords>\n              </ResourceRecordSet>\n            </Change>\n            <%- end -%>\n            <%- end -%>\n          </Changes>\n        </ChangeBatch>\n      </ChangeResourceRecordSetsRequest>\n    XML\n\n    invalidate_cache!\n  end\nend\n", trim_mode: "-").result(binding))