Class: Encryptbot::Services::Cloudflare

Inherits:
Object
  • Object
show all
Defined in:
lib/encryptbot/services/cloudflare.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain, dns_entry) ⇒ Cloudflare

Returns a new instance of Cloudflare.



11
12
13
14
15
16
17
# File 'lib/encryptbot/services/cloudflare.rb', line 11

def initialize(domain, dns_entry)
  @domain = domain.to_s.gsub("*.", "") # cleanup wildcard by removing *. infront
  @api_key = Encryptbot.configuration.cloudflare_api_key
  @api_email = Encryptbot.configuration.cloudflare_email
  @dns_entry = dns_entry # {content: "txt-record-content", type: "TXT", name: "_acme-challenge.domain.com"}
  @dns_record = "#{dns_entry[:name]}.#{@domain}"
end

Instance Attribute Details

#api_emailObject

Returns the value of attribute api_email.



9
10
11
# File 'lib/encryptbot/services/cloudflare.rb', line 9

def api_email
  @api_email
end

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/encryptbot/services/cloudflare.rb', line 9

def api_key
  @api_key
end

#dns_entryObject

Returns the value of attribute dns_entry.



9
10
11
# File 'lib/encryptbot/services/cloudflare.rb', line 9

def dns_entry
  @dns_entry
end

#dns_recordObject

Returns the value of attribute dns_record.



9
10
11
# File 'lib/encryptbot/services/cloudflare.rb', line 9

def dns_record
  @dns_record
end

#dns_record_idObject

Returns the value of attribute dns_record_id.



9
10
11
# File 'lib/encryptbot/services/cloudflare.rb', line 9

def dns_record_id
  @dns_record_id
end

#domainObject

Returns the value of attribute domain.



9
10
11
# File 'lib/encryptbot/services/cloudflare.rb', line 9

def domain
  @domain
end

#zone_idObject

Returns the value of attribute zone_id.



9
10
11
# File 'lib/encryptbot/services/cloudflare.rb', line 9

def zone_id
  @zone_id
end

Instance Method Details

#add_challengeObject



19
20
21
22
23
24
25
26
# File 'lib/encryptbot/services/cloudflare.rb', line 19

def add_challenge
  begin
    get_zone_id
    setup_dns_record
  rescue => e
    raise Encryptbot::Error::CloudflareDNSError, e
  end
end

#add_dns_recordObject



53
54
55
56
57
58
59
60
61
# File 'lib/encryptbot/services/cloudflare.rb', line 53

def add_dns_record
  response = post("/zones/#{@zone_id}/dns_records", {
    type: @dns_entry[:type],
    name: @dns_record,
    content: @dns_entry[:content],
    ttl: 120
  })
  response["success"]
end

#find_dns_recordObject



46
47
48
49
50
51
# File 'lib/encryptbot/services/cloudflare.rb', line 46

def find_dns_record
  response = get("/zones/#{@zone_id}/dns_records?name=#{@dns_record}&type=#{@dns_entry[:type]}")
  if response["result"].any?
    @dns_record_id = response["result"].first["id"]
  end
end

#get_zone_idObject



28
29
30
31
32
33
# File 'lib/encryptbot/services/cloudflare.rb', line 28

def get_zone_id
  response = get("/zones?name=#{@domain}")
  if response["result"].any?
    @zone_id = response["result"].first["id"]
  end
end

#setup_dns_recordObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/encryptbot/services/cloudflare.rb', line 35

def setup_dns_record
  find_dns_record
  return false if @zone_id.nil?

  if @dns_record_id
    update_dns_record
  else
    add_dns_record
  end
end

#update_dns_recordObject



63
64
65
66
67
68
69
70
71
# File 'lib/encryptbot/services/cloudflare.rb', line 63

def update_dns_record
  response = put("/zones/#{@zone_id}/dns_records/#{@dns_record_id}", {
    type: @dns_entry[:type],
    name: @dns_record,
    content: @dns_entry[:content],
    ttl: 120
  })
  response["success"]
end