Class: RecordStore::Provider::Cloudflare

Inherits:
RecordStore::Provider show all
Defined in:
lib/record_store/provider/cloudflare.rb

Class Method Summary collapse

Methods inherited from RecordStore::Provider

build_zone, empty_non_terminal_over_wildcard?, freezable?, provider_for, thawable?, to_s

Class Method Details

.apply_changeset(changeset, stdout = $stdout) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/record_store/provider/cloudflare.rb', line 69

def apply_changeset(changeset, stdout = $stdout)
  deletes = []
  patches = []
  posts = []
  puts = []

  changeset.changes.each do |change|
    case change.type
    when :removal
      stdout.puts "Removing #{change.record}..."
      deletes << { id: change.record.id }
    when :addition
      stdout.puts "Creating #{change.record}..."
      posts << build_api_body(change.record)
    when :update
      stdout.puts "Updating record with ID #{change.id} to #{change.record}..."
      patches << build_api_body(change.record).merge(id: change.id)
    else
      raise ArgumentError, "Unknown change type #{change.type.inspect}"
    end
  end

  zone_id = zone_name_to_id(changeset.zone)
  api_body = {
    deletes: deletes,
    patches: patches,
    posts: posts,
    puts: puts
  }

  retry_on_connection_errors do
    response = client.post("/client/v4/zones/#{zone_id}/dns_records/batch", api_body)
    unless response.success
      error_message = response.errors.map { |error| error['message'] }.join(', ')
      raise RecordStore::Provider::Error, "Cloudflare API error: #{error_message}"
    end
  end
end

.record_typesObject



6
7
8
# File 'lib/record_store/provider/cloudflare.rb', line 6

def record_types
  super | Set.new(%w(PTR))
end

.retrieve_current_records(zone:, stdout: $stdout) ⇒ Object

Returns: an array of ‘Record` for each record in the provider’s zone



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/record_store/provider/cloudflare.rb', line 20

def retrieve_current_records(zone:, stdout: $stdout)
  zone_id = zone_name_to_id(zone)

  all_records = []
  page = 1
  per_page = 1000

  loop do
    response = nil
    retry_on_connection_errors do
      response = client.get("/client/v4/zones/#{zone_id}/dns_records", page: page, per_page: per_page)
    end

    records = response.result_raw || []
    all_records.concat(records)

    break if page * per_page >= response.result_info_raw['total_count']

    page += 1
  end

  all_records.map { |api_body| build_from_api(api_body) }
end

.supports_alias?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/record_store/provider/cloudflare.rb', line 10

def supports_alias?
  true
end

.supports_spf?Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/record_store/provider/cloudflare.rb', line 14

def supports_spf?
  # New as Cloudflare doesn't support the now deprecated spf type
  false
end

.zonesObject

Returns an array of the zones managed by provider as strings Cloudflare returns zones across all accounts accessible by the API token Can implement filtering in request if needed



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/record_store/provider/cloudflare.rb', line 47

def zones
  all_zones = []
  page = 1
  per_page = 50

  loop do
    response = nil
    retry_on_connection_errors do
      response = client.get('/client/v4/zones', page: page, per_page: per_page)
    end

    zones = response.result_raw || []
    all_zones.concat(zones)

    break if page * per_page >= response.result_info_raw['total_count']

    page += 1
  end

  all_zones.map { |zone| zone['name'] }
end