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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/zonesync/route53.rb', line 86

def add(record)
  add_with_duplicate_handling(record) do
    begin
      if record.type == "TXT"
        # Route53 requires all TXT records with the same name to be combined into a single record set
        existing_txt_records = records.select do |r|
          r.name == record.name && r.type == "TXT"
        end
        all_txt_records = existing_txt_records + [record]

        # Use UPSERT if records already exist, CREATE if they don't
        action = existing_txt_records.empty? ? "CREATE" : "UPSERT"
        change_records(action, all_txt_records)
      else
        change_record("CREATE", record)
      end
    rescue RuntimeError => e
      # Convert Route53-specific duplicate error to standard exception
      if e.message.include?("RRSet already exists")
        raise DuplicateRecordError.new(record, "Route53 duplicate record error")
      else
        # Re-raise other errors
        raise
      end
    end
  end
end

#change(old_record, new_record) ⇒ Object



80
81
82
83
# File 'lib/zonesync/route53.rb', line 80

def change(old_record, new_record)
  remove(old_record)
  add(new_record)
end

#readObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/zonesync/route53.rb', line 12

def read
  @read = T.let(@read, T.nilable(String))
  @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



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
72
73
74
75
76
77
# File 'lib/zonesync/route53.rb', line 24

def remove(record)
  if record.type == "TXT"
    # Route53 requires all TXT records with the same name to be managed together
    existing_txt_records = records.select do |r|
      r.name == record.name && r.type == "TXT"
    end

    if existing_txt_records.length == 1
      # Only one TXT record, delete it normally
      change_record("DELETE", record)
    else
      # Multiple TXT records - delete all, then recreate without the removed one
      remaining_txt_records = existing_txt_records.reject { |r| r == record }

      # Use change_records to handle both DELETE and CREATE in one request
      grouped = [
        [existing_txt_records, "DELETE"],
        [remaining_txt_records, "CREATE"]
      ]

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