class DnsMadeEasy

A class to interact with the DNSMadeEasy REST API v2.0

Attributes

base_uri[RW]
request_limit[R]
requests_remaining[R]

Public Class Methods

new(api_key, api_secret, sandbox = false, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 15
def initialize(api_key, api_secret, sandbox = false, options = {})
  raise 'api_key is undefined' unless api_key
  raise 'api_secret is undefined' unless api_secret

  @api_key = api_key
  @api_secret = api_secret
  @options = options
  @requests_remaining = -1
  @request_limit = -1

  self.base_uri = if sandbox
                    'https://api.sandbox.dnsmadeeasy.com/V2.0'
                  else
                    'https://api.dnsmadeeasy.com/V2.0'
                  end
end

Public Instance Methods

create_a_record(domain_name, name, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 149
def create_a_record(domain_name, name, value, options = {})
  # TODO: match IPv4 for value
  create_record domain_name, name, 'A', value, options
end
create_aaaa_record(domain_name, name, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 154
def create_aaaa_record(domain_name, name, value, options = {})
  # TODO: match IPv6 for value
  create_record domain_name, name, 'AAAA', value, options
end
create_cname_record(domain_name, name, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 169
def create_cname_record(domain_name, name, value, options = {})
  # TODO: match CNAME value
  create_record domain_name, name, 'CNAME', value, options
end
create_domain(domain_name) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 56
def create_domain(domain_name)
  create_domains([domain_name])
end
create_domains(names) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 52
def create_domains(names)
  post('/dns/managed/', names: names)
end
create_httpred_record(domain_name, name, value, redirectType = 'STANDARD - 302', description = '', keywords = '', title = '', options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 194
def create_httpred_record(domain_name, name, value, redirectType = 'STANDARD - 302', description = '', keywords = '', title = '', options = {})
  options.merge!('redirectType' => redirectType, 'description' => description, 'keywords' => keywords, 'title' => title)
  create_record domain_name, name, 'HTTPRED', value, options
end
create_ip_set(name, ips = []) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 100
def create_ip_set(name, ips = [])
  post('/dns/secondary/ipSet', 'name' => name, 'ips' => ips)
end
create_mx_record(domain_name, name, priority, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 183
def create_mx_record(domain_name, name, priority, value, options = {})
  options.merge!('mxLevel' => priority)

  create_record domain_name, name, 'MX', value, options
end
create_ns_record(domain_name, name, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 174
def create_ns_record(domain_name, name, value, options = {})
  # TODO: match domainname for value
  create_record domain_name, name, 'NS', value, options
end
create_ptr_record(domain_name, name, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 159
def create_ptr_record(domain_name, name, value, options = {})
  # TODO: match PTR value
  create_record domain_name, name, 'PTR', value, options
end
create_record(domain_name, name, type, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 144
def create_record(domain_name, name, type, value, options = {})
  body = { 'name' => name, 'type' => type, 'value' => value, 'ttl' => 3600, 'gtdLocation' => 'DEFAULT' }
  post "/dns/managed/#{get_id_by_domain(domain_name)}/records/", body.merge(options)
end
create_secondary(names = [], ipSetId) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 76
def create_secondary(names = [], ipSetId)
  post('/dns/secondary/', "names": names, 'ipSetId' => ipSetId)
end
create_spf_record(domain_name, name, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 179
def create_spf_record(domain_name, name, value, options = {})
  create_record domain_name, name, 'SPF', value, options
end
create_srv_record(domain_name, name, priority, weight, port, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 189
def create_srv_record(domain_name, name, priority, weight, port, value, options = {})
  options.merge!('priority' => priority, 'weight' => weight, 'port' => port)
  create_record domain_name, name, 'SRV', value, options
end
create_txt_record(domain_name, name, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 164
def create_txt_record(domain_name, name, value, options = {})
  # TODO: match TXT value
  create_record domain_name, name, 'TXT', value, options
end
delete_all_records(domain_name) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 139
def delete_all_records(domain_name)
  domain_id = get_id_by_domain(domain_name)
  delete "/dns/managed/#{domain_id}/records"
end
delete_domain(domain_name) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 48
def delete_domain(domain_name)
  delete "/dns/managed/#{get_id_by_domain(domain_name)}"
end
delete_ip_set(id) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 104
def delete_ip_set(id)
  delete("/dns/secondary/ipSet/#{id}")
end
delete_record(domain_name, record_id) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 127
def delete_record(domain_name, record_id)
  delete "/dns/managed/#{get_id_by_domain(domain_name)}/records/#{record_id}/"
end
delete_records(domain_name, ids = []) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 131
def delete_records(domain_name, ids = [])
  return if ids.empty?

  domain_id = get_id_by_domain(domain_name)

  delete "/dns/managed/#{domain_id}/records?ids=#{ids.join(',')}"
end
delete_secondary(id) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 80
def delete_secondary(id)
  delete("/dns/secondary/#{id}")
end
disable_failover(record_id, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 260
def disable_failover(record_id, options = {})
  put "/monitor/#{record_id}", { 'failover' => false, 'monitor' => false }.merge(options)
end
domain(domain_name) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 44
def domain(domain_name)
  get "/dns/managed/#{get_id_by_domain(domain_name)}"
end
domains() click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 40
def domains
  get '/dns/managed/'
end
find(domain_name, name, type) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 116
def find(domain_name, name, type)
  records = records_for(domain_name)
  records['data'].detect { |r| r['name'] == name && r['type'] == type }
end
find_record_id(domain_name, name, type) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 121
def find_record_id(domain_name, name, type)
  records = records_for(domain_name)

  records['data'].select { |r| r['name'] == name && r['type'] == type }.map { |r| r['id'] }
end
get_failover_config(record_id) click to toggle source

————- FAILOVER ————-


# File lib/dnsmadeeasy-rest-api.rb, line 222
def get_failover_config(record_id)
  get "/monitor/#{record_id}"
end
get_id_by_domain(domain_name) click to toggle source

————- DOMAINS ————-


# File lib/dnsmadeeasy-rest-api.rb, line 36
def get_id_by_domain(domain_name)
  get("/dns/managed/id/#{domain_name}")['id']
end
ip_set(id) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 92
def ip_set(id)
  get "/dns/secondary/ipSet/#{id}"
end
ip_sets() click to toggle source

————- IP SETS ————-


# File lib/dnsmadeeasy-rest-api.rb, line 88
def ip_sets
  get '/dns/secondary/ipSet'
end
records_for(domain_name) click to toggle source

————- RECORDS ————-


# File lib/dnsmadeeasy-rest-api.rb, line 112
def records_for(domain_name)
  get "/dns/managed/#{get_id_by_domain(domain_name)}/records"
end
secondary(id) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 68
def secondary(id)
  get "/dns/secondary/#{id}"
end
secondarys() click to toggle source

——– SECONDARY DOMAINS ——–


# File lib/dnsmadeeasy-rest-api.rb, line 64
def secondarys
  get '/dns/secondary'
end
update_failover_config(record_id, ips, desc, protocol = 'TCP', options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 226
def update_failover_config(record_id, ips, desc, protocol = 'TCP', options = {})
  protocolIds = {
    'TCP' => 1,
    'UDP' => 2,
    'HTTP' => 3,
    'DNS' => 4,
    'SMTP' => 5,
    'HTTPS' => 6
  }

  body = {
    'protocolId' => protocolIds[protocol],
    'port' => 80,
    'systemDescription' => desc,
    'sensitivity' => 5,
    'failover' => true,
    'monitor' => false,
    'maxEmails' => 1,
    'autoFailover' => false,
    'source' => 1
  }

  body = body.merge(options)

  ip_config = {}
  (0..ips.length - 1).each do |idx|
    ip_config["ip#{idx + 1}"] = ips[idx]
  end

  body = body.merge(ip_config)

  put "/monitor/#{record_id}", body
end
update_ip_set(id, name, ips = []) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 96
def update_ip_set(id, name, ips = [])
  put("/dns/secondary/ipSet/#{id}", 'name' => name, 'id' => id, 'ips' => ips)
end
update_record(domain, record_id, name, type, value, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 199
def update_record(domain, record_id, name, type, value, options = {})
  body = { 'name' => name, 'type' => type, 'value' => value, 'ttl' => 3600, 'gtdLocation' => 'DEFAULT', 'id' => record_id }
  put "/dns/managed/#{get_id_by_domain(domain)}/records/#{record_id}/", body.merge(options)
end
update_records(domain, records, options = {}) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 204
def update_records(domain, records, options = {})
  body = records.map do |record|
    {
      'id' => record['id'],
      'name' => record['name'],
      'type' => record['type'],
      'value' => record['value'],
      'gtdLocation' => record['gtdLocation'],
      'ttl' => record['ttl']
    }.merge(options)
  end
  put "/dns/managed/#{get_id_by_domain(domain)}/records/updateMulti/", body
end
update_secondary(ids = [], ipSetId) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 72
def update_secondary(ids = [], ipSetId)
  put('/dns/secondary/', 'ids' => ids, 'ipSetId' => ipSetId)
end

Private Instance Methods

delete(path, body = nil) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 272
def delete(path, body = nil)
  request(path) do |uri|
    req = Net::HTTP::Delete.new(uri)
    req.body = body.to_json if body
    req
  end
end
get(path) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 266
def get(path)
  request(path) do |uri|
    Net::HTTP::Get.new(uri)
  end
end
post(path, body) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 288
def post(path, body)
  request(path) do |uri|
    req = Net::HTTP::Post.new(uri)
    req.body = body.to_json
    req
  end
end
put(path, body = nil) click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 280
def put(path, body = nil)
  request(path) do |uri|
    req = Net::HTTP::Put.new(uri)
    req.body = body.to_json if body
    req
  end
end
request(path) { |uri| ... } click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 296
def request(path)
  uri = URI("#{base_uri}#{path}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @options.key?(:ssl_verify_none)
  http.open_timeout = @options[:open_timeout] if @options.key?(:open_timeout)
  http.read_timeout = @options[:read_timeout] if @options.key?(:read_timeout)

  request = yield(uri)

  request_headers.each do |key, value|
    request[key] = value
  end

  response = http.request(request)
  response.value # raise Net::HTTPServerException unless response was 2xx

  response.each_header do |header, value|
    @requests_remaining = value.to_i if header == 'x-dnsme-requestsremaining'
    @request_limit = value.to_i if header == 'x-dnsme-requestlimit'
  end

  unparsed_json = response.body.to_s.empty? ? '{}' : response.body

  JSON.parse(unparsed_json)
end
request_headers() click to toggle source
# File lib/dnsmadeeasy-rest-api.rb, line 324
def request_headers
  request_date = Time.now.httpdate
  hmac = OpenSSL::HMAC.hexdigest('sha1', @api_secret, request_date)
  {
    'Accept' => 'application/json',
    'x-dnsme-apiKey' => @api_key,
    'x-dnsme-requestDate' => request_date,
    'x-dnsme-hmac' => hmac
  }
end