Class: Zonesync::Cloudflare

Inherits:
Provider show all
Defined in:
lib/zonesync/cloudflare.rb,
lib/zonesync/cloudflare/proxied_support.rb

Defined Under Namespace

Modules: ProxiedSupport

Instance Attribute Summary

Attributes inherited from Provider

#config

Instance Method Summary collapse

Methods inherited from Provider

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

Constructor Details

This class inherits a constructor from Zonesync::Provider

Instance Method Details

#add(record) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zonesync/cloudflare.rb', line 39

def add(record)
  add_with_duplicate_handling(record) do
    begin
      http.post("", to_hash(record))
    rescue RuntimeError => e
      # Convert CloudFlare-specific duplicate error to standard exception
      if e.message.include?('"code":81058') && e.message.include?("An identical record already exists")
        raise DuplicateRecordError.new(record, "CloudFlare error 81058")
      else
        # Re-raise other errors
        raise
      end
    end
  end
end

#allObject



55
56
57
58
59
60
# File 'lib/zonesync/cloudflare.rb', line 55

def all
  response = http.get("")
  response["result"].reduce({}) do |map, attrs|
    map.merge(to_record(attrs) => attrs["id"])
  end
end

#change(old_record, new_record) ⇒ Object



27
28
29
30
# File 'lib/zonesync/cloudflare.rb', line 27

def change(old_record, new_record)
  id = find_record_id(old_record)
  http.patch("/#{id}", to_hash(new_record))
end

#diff(other) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/zonesync/cloudflare.rb', line 14

def diff(other)
  source_records = other.diffable_records.map { |r| r.extend(ProxiedSupport) }
  Diff.new(
    from: diffable_records,
    to: source_records,
  )
end

#find_record_id(record) ⇒ Object

Raises:

  • (KeyError)


32
33
34
35
36
37
# File 'lib/zonesync/cloudflare.rb', line 32

def find_record_id(record)
  all.each do |existing, id|
    return id if existing.identical_to?(record)
  end
  raise KeyError, "record not found: #{record.inspect}"
end

#readObject



9
10
11
12
# File 'lib/zonesync/cloudflare.rb', line 9

def read
  records = [fake_soa.extend(ProxiedSupport)] + all.keys
  records.map(&:to_s).join("\n") + "\n"
end

#remove(record) ⇒ Object



22
23
24
25
# File 'lib/zonesync/cloudflare.rb', line 22

def remove(record)
  id = find_record_id(record)
  http.delete("/#{id}")
end