Permalink
Browse files

update cloudflare API to v4, will break tests

  • Loading branch information...
Raekye committed Jul 1, 2018
1 parent 5a2445d commit 5e71bd62078f851f473605f0e7473014b685fff2
Showing with 67 additions and 48 deletions.
  1. +1 −0 README.md
  2. +62 −46 app/models/cloud_flare/client.rb
  3. +1 −1 app/models/server.rb
  4. +2 −1 config/application.rb
  5. +1 −0 env.sh.template
@@ -69,6 +69,7 @@ The following instructions were made for Fedora 20, but the steps should be simi
- `USER_SERVERS_DOMAIN`: subdomain for user servers (e.g. `gamocosm.com`)
- `CLOUDFLARE_API_TOKEN`: hmmm
- `CLOUDFLARE_EMAIL`: hmmm
- `CLOUDFLARE_ZONE`: TODO explain how to get this
- `DEVISE_SECRET_KEY`: only tests, production
- `SECRET_KEY`: only production
- `DEVELOPER_EMAILS`: comma separated list of emails to send exceptions to
@@ -1,28 +1,29 @@
class CloudFlare::Client

CLOUDFLARE_API_URL = 'https://www.cloudflare.com/api_json.html'
CLOUDFLARE_API_URL = 'https://api.cloudflare.com/client/v4/'
HTTP_REQUEST_TIMEOUT = 4

def initialize(api_email, api_token, domain)
def initialize(api_email, api_token, domain, zone)
@api_email = api_email
@api_token = api_token
@domain = domain
@zone = zone
@conn = Faraday.new(url: CLOUDFLARE_API_URL) do |conn|
conn.response :json
conn.adapter Faraday.default_adapter
end
end

def dns_list
def dns_list(name)
silence do
response = do_request(:rec_load_all, {})
if response.error?
return response
res = do_get("zones/#{@zone}/dns_records", name == nil ? {} : { name: "#{name}.#{Gamocosm::USER_SERVERS_DOMAIN}" })
if res.error?
return res
end
records = []
response['recs']['objs'].each do |x|
res.each do |x|
if x['type'] == 'A'
records.push({ id: x['rec_id'].to_i, name: x['display_name'], content: x['content']})
records.push({ id: x['id'], name: x['name'], content: x['content']})
end
end
return records
@@ -31,64 +32,79 @@ def dns_list

def dns_add(name, ip)
silence do
res = do_request(:rec_new, { type: 'A', name: name, content: ip, ttl: 120 })
return res.error? ? res : nil
end
end

def dns_update(name, ip)
silence do
records = self.dns_list
if records.error?
return records
end
x = records.index { |x| x[:name] == name }
if x.nil?
return self.dns_add(name, ip)
end
res = do_request(:rec_edit, { id: records[x][:id], type: 'A', name: name, content: ip, ttl: 120 })
self.dns_delete(name)
res = do_post("zones/#{@zone}/dns_records", { type: 'A', name: name, content: ip, ttl: 120 })
return res.error? ? res : nil
end
end

def dns_delete(name)
silence do
records = self.dns_list
records = self.dns_list(name)
if records.error?
return records
end
x = records.index { |x| x[:name] == name }
if x.nil?
return nil
errors = []
records.each do |x|
res = do_delete("zones/#{@zone}/dns_records/#{x[:id]}")
if res.error?
errors.push(res)
end
end
res = do_request(:rec_delete, { id: records[x][:id] })
return res.error? ? res : nil
return errors.count == 0 ? nil : ("CloudFlare API error: had #{errors.count} errors deleting DNS records.".error! errors)
end
end

private
def do_request(action, data)
return do_post(make_post_params(action, data))
def update_headers(opts)
opts['X-Auth-Key'] = @api_token
opts['X-Auth-Email'] = @api_email
opts['Content-Type'] = 'application/json'
end

def make_post_params(a, opts)
return {
tkn: @api_token,
email: @api_email,
a: a.to_s,
z: @domain
}.merge(opts)
def do_get(action, params)
begin
res = @conn.get do |req|
req.url action, params
update_headers(req.headers)
req.options.timeout = HTTP_REQUEST_TIMEOUT
end
return parse_response(res)
rescue Faraday::Error => e
msg = "CloudFlare API network exception: #{e}."
Rails.logger.error msg
Rails.logger.error e.backtrace.join("\n")
return msg.error! e
end
end

def do_post(params)
def do_post(action, params)
begin
res = @conn.post do |req|
req.params = params
req.url action
update_headers(req.headers)
req.options.timeout = HTTP_REQUEST_TIMEOUT
req.body = params.to_json
end
return parse_response(res)
rescue Faraday::Error => e
msg = "CloudFlare API network exception: #{e}."
Rails.logger.error msg
Rails.logger.error e.backtrace.join("\n")
return msg.error! e
end
end

def do_delete(action)
begin
res = @conn.delete do |req|
req.url action
update_headers(req.headers)
req.options.timeout = HTTP_REQUEST_TIMEOUT
end
return parse_response(res)
rescue Faraday::Error => e
msg = "CloudFlare API network exception: #{e}"
msg = "CloudFlare API network exception: #{e}."
Rails.logger.error msg
Rails.logger.error e.backtrace.join("\n")
return msg.error! e
@@ -97,16 +113,16 @@ def do_post(params)

def parse_response(res)
if res.status != 200
msg = "CloudFlare API error: HTTP response code #{res.status}, #{res.body}"
msg = "CloudFlare API error: HTTP response code #{res.status}, #{res.body}."
Rails.logger.error msg
return msg.error! res
end
if res.body['result'] != 'success'
msg = "CloudFlare API error: response result #{res.body['result']}, #{res.body}"
if res.body['success'] != true
msg = "CloudFlare API error: response result #{res.body}."
Rails.logger.error msg
return msg.error! res
end
return res.body['response']
return res.body['result']
end

end
@@ -138,7 +138,7 @@ def refresh_domain
if ip_address.error?
return ip_address
end
return Gamocosm.cloudflare.dns_update(domain, ip_address)
return Gamocosm.cloudflare.dns_add(domain, ip_address)
end

def remove_domain
@@ -16,6 +16,7 @@ module Gamocosm
USER_SERVERS_DOMAIN = ENV['USER_SERVERS_DOMAIN']
CLOUDFLARE_API_TOKEN = ENV['CLOUDFLARE_API_TOKEN']
CLOUDFLARE_EMAIL = ENV['CLOUDFLARE_EMAIL']
CLOUDFLARE_ZONE = ENV['CLOUDFLARE_ZONE']

MINECRAFT_FLAVOURS = YAML.load_file(File.expand_path('config/minecraft_flavours.yml', Rails.root)).inject({}, &lambda do |a, x|
x.second['versions'].each do |v|
@@ -51,7 +52,7 @@ def self.digital_ocean
@cloudflare = nil
def self.cloudflare
if @cloudflare.nil?
@cloudflare = CloudFlare::Client.new(CLOUDFLARE_EMAIL, CLOUDFLARE_API_TOKEN, USER_SERVERS_DOMAIN)
@cloudflare = CloudFlare::Client.new(CLOUDFLARE_EMAIL, CLOUDFLARE_API_TOKEN, USER_SERVERS_DOMAIN, CLOUDFLARE_ZONE)
end
return @cloudflare
end
@@ -18,6 +18,7 @@ export MAIL_SERVER_PASSWORD=
export USER_SERVERS_DOMAIN=
export CLOUDFLARE_API_TOKEN=
export CLOUDFLARE_EMAIL=
export CLOUDFLARE_ZONE=
# Test, and production
export DEVISE_SECRET_KEY=1234abc
# Production only

0 comments on commit 5e71bd6

Please sign in to comment.