Class: DnsMadeEasy::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsmadeeasy/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, sandbox = false) ⇒ Api

Initialize the class with the api key, the secret key, and an optional flag to run the queries in DNSMadeEasy’s sandbox mode



62
63
64
65
66
# File 'lib/dnsmadeeasy/api.rb', line 62

def initialize(api_key, secret_key, sandbox=false)
  @api_key = api_key
  @secret_key = secret_key
  @sandbox = sandbox
end

Instance Method Details

#create_domain!(domain) ⇒ Object

Create a new domain entry.

Returns a hash of the newly created domain entry, e.g.

{ :name => "sigfig.com", :nameServer => ["ns66.dnsmadeeasy.com", "ns99.dnsmadeeasy.com"], :gtdEnabled => true }


96
97
98
99
100
101
# File 'lib/dnsmadeeasy/api.rb', line 96

def create_domain!(domain)
  execute_with_caution :domain => domain do
    response = RestClient.put request_url(:path => "#{domain}"), {}, headers
    return JSON.parse(response, :symbolize_names => true)
  end
end

#create_record!(domain, record) ⇒ Object

Create a new dns record.

Record should be a hash with the following values:

:name        => Hostname. E.g. www, or empty string for root domain
:type        => Record type. E.g. A, CNAME, MX, TXT, SRV, NS, AAAA, HTTPRED, PTR
:data        => Public name. E.g. "66.88.99.44" or "ec2-gibberish.amazonaws.com."
:gtdLocation => Global Traffic Director location. Values: DEFAULT, US_EAST, US_WEST, EUROPE (optional, defaults to DEFAULT)
:ttl         => Time to live. The amount of time a record will be cached before being refreshed. E.g. 300

Here’s a list of possible data values, with the record type they are used with:

  • A: [host IP]

  • AAAA: [IPv6 host IP]

  • CNAME: [target name]

  • HTTPRED: [redirection URL]

  • MX: [priority] [target name]

  • NS: [name server]

  • PTR: [target name]

  • SRV: [priority] [weight] [port] [target name]

  • TXT: [text value]

N.B. for CNAME, MX, NS, PTR, and SRV records, the domain name of the dns record is automatically appended to the given data unless the data ends with a “.” So, to map, say mail to mail.google.com, use “mail.google.com.” as the data value. By the same logic, to map “www.sigfig.com” to, say, “server1.sigfig.com”, just use “server1” as the data value or “server1.sigfig.com.”; don’t give “server1.sigfig.com” because that’ll essentially resolve to server1.sigfig.com.sigfig.com.

Returns a hash of the newly created dns entry, e.g.

{ :name => "www", :type => "CNAME", :data => "sigfig.com.", :ttl => 7200, id => 23456, :gtdLocation => "DEFAULT" }


174
175
176
177
178
179
180
181
182
183
# File 'lib/dnsmadeeasy/api.rb', line 174

def create_record!(domain, record)
  validate_record(record)
  request_data = {
    :gtdLocation => "DEFAULT"
  }.merge(record)
  execute_with_caution :domain => domain do
    response = RestClient.post request_url(:path => "#{domain}/records"), request_data.to_json, headers(:content_type => "application/json")
    return JSON.parse(response, :symbolize_names => true)
  end
end

#delete_domain!(domain) ⇒ Object

Delete a domain entry.

Returns a hash of the deleted domain entry, e.g.

{ :nameServer => "sigfig.com", :nameServer => ["ns66.dnsmadeeasy.com", "ns99.dnsmadeeasy.com"], :gtdEnabled => true }


107
108
109
110
111
112
113
# File 'lib/dnsmadeeasy/api.rb', line 107

def delete_domain!(domain)
  execute_with_caution :domain => domain do
    description = describe_domain(domain)
    response = RestClient.delete request_url(:path => "#{domain}"), headers
    return description
  end
end

#delete_domains!Object

Delete all domains in the account.

Returns an array of domains deleted, e.g.

["sigfig.com", "wikinvest.com"]


84
85
86
87
88
89
90
# File 'lib/dnsmadeeasy/api.rb', line 84

def delete_domains!
  execute_with_caution do
    domains = list_domains
    response = RestClient.delete request_url, headers
    return domains
  end
end

#delete_record!(domain, record_id) ⇒ Object

Delete a dns record.

Returns a hash of the deleted dns entry, e.g.

{  :name => "", :type => "A", "data" => "127.0.0.1", :ttl => 7200, :id => 12345, :gtdLocation => "DEFAULT" }


189
190
191
192
193
194
195
# File 'lib/dnsmadeeasy/api.rb', line 189

def delete_record!(domain, record_id)
  execute_with_caution :domain => domain, :record => record_id do
    record = describe_record(domain, record_id)
    response = RestClient.delete request_url(:path => "#{domain}/records/#{record_id}"), headers
    return record
  end
end

#delete_records!(domain, filter = {}) ⇒ Object

Delete multiple records for a domain.

Refer to list_records for available filters.

N.B. Given that there are limits to the number of api requests, the atomicity of this action is not guaranteed. This means that it is possible under certain circumstances that not all records that match the given criteria will be deleted.

Returns an array of hashes, each hash representing a deleted dns entry, e.g.

[
  { :name => "", :type => "A", "data" => "127.0.0.1", :ttl => 7200, :id => 12345, :gtdLocation => "DEFAULT" },
  { :name => "www", :type => "CNAME", :data => "sigfig.com.", :ttl => 7200, :id => 23456, :gtdLocation => "DEFAULT" }
]


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/dnsmadeeasy/api.rb', line 210

def delete_records!(domain, filter={})
  record_list = list_records(domain, filter)
  unless record_list.empty?
    if record_list.size == 1
      delete_record!(domain, record_list[0][:id])
    elsif requests_remaining >= record_list.size
      record_list.each do |record|
        delete_record!(domain, record[:id])
      end
    else
      raise DnsMadeEasy::BadRequestError, "Not enough requests remaining to complete the operation"
    end
  end
  return record_list
end

#describe_domain(domain) ⇒ Object

Describe a domain entry.

Returns a hash of the domain entry, e.g.

{ :name => "sigfig.com", :nameServer => ["ns66.dnsmadeeasy.com", "ns99.dnsmadeeasy.com"], :gtdEnabled => true }


119
120
121
122
123
124
# File 'lib/dnsmadeeasy/api.rb', line 119

def describe_domain(domain)
  execute_with_caution :domain => domain do
    response = RestClient.get request_url(:path => "#{domain}"), headers
    return JSON.parse(response, :symbolize_names => true)
  end
end

#describe_record(domain, record_id) ⇒ Object

Describe an existing dns record.

Returns a hash of the dns entry.

{ :name => "www", :type => "CNAME", :data => "sigfig.com.", :ttl => 7200, :id => 23456, :gtdLocation => "DEFAULT" }


245
246
247
248
249
250
# File 'lib/dnsmadeeasy/api.rb', line 245

def describe_record(domain, record_id)
  execute_with_caution :domain => domain, :record => record_id do
    response = RestClient.get request_url(:path => "#{domain}/records/#{record_id}"), headers
    return JSON.parse(response, :symbolize_names => true)
  end
end

#list_domainsObject

List all domains in the account.

Returns an array of domain names in the account, e.g.

["sigfig.com", "wikinvest.com"]


72
73
74
75
76
77
78
# File 'lib/dnsmadeeasy/api.rb', line 72

def list_domains
  execute_with_caution do
    response = RestClient.get request_url, headers
    response_hash = JSON.parse(response, :symbolize_names => true)
    return response_hash.has_key?(:list) ? response_hash[:list] : []
  end
end

#list_records(domain, filter = {}) ⇒ Object

List records for a domain.

The filter parameter allows you to list a list a subset of records filtered by a criteria. Available filters:

  • { :name => “www” }: Lists all entries for host “www”

  • { :type => :A }: Lists all A records

  • { :gtdLocation => :EUROPE } List all entries for EUROPE Traffic Director

Returns an array of hashes, each hash representing a dns entry, e.g.

[
  { :name => "", :type => "A", "data" => "127.0.0.1", :ttl => 7200, :id => 12345, :gtdLocation => "DEFAULT" },
  { :name => "www", :type => "CNAME", :data => "sigfig.com.", :ttl => 7200, id => 23456, :gtdLocation => "DEFAULT" }
]


138
139
140
141
142
143
# File 'lib/dnsmadeeasy/api.rb', line 138

def list_records(domain, filter={})
  execute_with_caution :domain => domain do
    response = RestClient.get request_url(:path => "#{domain}/records", :query => filter), headers
    return JSON.parse(response, :symbolize_names => true)
  end
end

#requests_remainingObject

Return the number of api requests remaining.



253
254
255
256
257
258
259
260
261
262
# File 'lib/dnsmadeeasy/api.rb', line 253

def requests_remaining
  begin
    execute_with_caution do
      response = RestClient.get request_url, headers
      return response.headers[:x_dnsme_requestsremaining].to_i if response.headers.has_key?(:x_dnsme_requestsremaining)
    end
  rescue DnsMadeEasy::BadRequestError => e
    return 0
  end
end

#update_record!(domain, record_id, record) ⇒ Object

Update an existing dns record.

Refer to create_record! documentation for record format. The params not specified remain unchanged

Returns a hash of the newly updated dns entry, e.g.

{ :name => "www", :type => "CNAME", :data => "sigfig.com.", :ttl => 7200, :id => 23456, :gtdLocation => "DEFAULT" }


232
233
234
235
236
237
238
239
# File 'lib/dnsmadeeasy/api.rb', line 232

def update_record!(domain, record_id, record)
  existing_record = describe_record(domain, record_id)
  record = existing_record.merge(record)
  execute_with_caution :domain => domain, :record => record_id do
    response = RestClient.put request_url(:path => "#{domain}/records/#{record_id}"), record.to_json, headers(:content_type => "application/json")
    return describe_record(domain, record_id)
  end
end