Class: DnsMadeEasy

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret, sandbox = false, options = {}) ⇒ DnsMadeEasy



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dnsmadeeasy-rest-api.rb', line 13

def initialize(api_key, api_secret, sandbox = false, options = {})
  fail 'api_key is undefined' unless api_key
  fail 'api_secret is undefined' unless api_secret

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

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

Instance Attribute Details

#base_uriObject

Returns the value of attribute base_uri.



9
10
11
# File 'lib/dnsmadeeasy-rest-api.rb', line 9

def base_uri
  @base_uri
end

#request_limitObject (readonly)

Returns the value of attribute request_limit.



11
12
13
# File 'lib/dnsmadeeasy-rest-api.rb', line 11

def request_limit
  @request_limit
end

#requests_remainingObject (readonly)

Returns the value of attribute requests_remaining.



10
11
12
# File 'lib/dnsmadeeasy-rest-api.rb', line 10

def requests_remaining
  @requests_remaining
end

Instance Method Details

#create_a_record(domain_name, name, value, options = {}) ⇒ Object



98
99
100
101
# File 'lib/dnsmadeeasy-rest-api.rb', line 98

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 = {}) ⇒ Object



103
104
105
106
# File 'lib/dnsmadeeasy-rest-api.rb', line 103

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 = {}) ⇒ Object



118
119
120
121
# File 'lib/dnsmadeeasy-rest-api.rb', line 118

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) ⇒ Object



54
55
56
# File 'lib/dnsmadeeasy-rest-api.rb', line 54

def create_domain(domain_name)
  create_domains([domain_name])
end

#create_domains(names) ⇒ Object



50
51
52
# File 'lib/dnsmadeeasy-rest-api.rb', line 50

def create_domains(names)
  post('/dns/managed/', names: names)
end

#create_httpred_record(domain_name, name, value, redirectType = 'STANDARD - 302', description = '', keywords = '', title = '', options = {}) ⇒ Object



143
144
145
146
# File 'lib/dnsmadeeasy-rest-api.rb', line 143

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_mx_record(domain_name, name, priority, value, options = {}) ⇒ Object



132
133
134
135
136
# File 'lib/dnsmadeeasy-rest-api.rb', line 132

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 = {}) ⇒ Object



123
124
125
126
# File 'lib/dnsmadeeasy-rest-api.rb', line 123

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 = {}) ⇒ Object



108
109
110
111
# File 'lib/dnsmadeeasy-rest-api.rb', line 108

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 = {}) ⇒ Object



93
94
95
96
# File 'lib/dnsmadeeasy-rest-api.rb', line 93

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_spf_record(domain_name, name, value, options = {}) ⇒ Object



128
129
130
# File 'lib/dnsmadeeasy-rest-api.rb', line 128

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 = {}) ⇒ Object



138
139
140
141
# File 'lib/dnsmadeeasy-rest-api.rb', line 138

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 = {}) ⇒ Object



113
114
115
116
# File 'lib/dnsmadeeasy-rest-api.rb', line 113

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) ⇒ Object



88
89
90
91
# File 'lib/dnsmadeeasy-rest-api.rb', line 88

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) ⇒ Object



46
47
48
# File 'lib/dnsmadeeasy-rest-api.rb', line 46

def delete_domain(domain_name)
  delete "/dns/managed/#{get_id_by_domain(domain_name)}"
end

#delete_record(domain_name, record_id) ⇒ Object



77
78
79
# File 'lib/dnsmadeeasy-rest-api.rb', line 77

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 = []) ⇒ Object



81
82
83
84
85
86
# File 'lib/dnsmadeeasy-rest-api.rb', line 81

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

#disable_failover(record_id, options = {}) ⇒ Object



209
210
211
# File 'lib/dnsmadeeasy-rest-api.rb', line 209

def disable_failover(record_id, options={})
  put "/monitor/#{record_id}", { 'failover' => false, 'monitor' => false }.merge(options)
end

#domain(domain_name) ⇒ Object



42
43
44
# File 'lib/dnsmadeeasy-rest-api.rb', line 42

def domain(domain_name)
  get "/dns/managed/#{get_id_by_domain(domain_name)}"
end

#domainsObject



38
39
40
# File 'lib/dnsmadeeasy-rest-api.rb', line 38

def domains
  get '/dns/managed/'
end

#find(domain_name, name, type) ⇒ Object



66
67
68
69
# File 'lib/dnsmadeeasy-rest-api.rb', line 66

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) ⇒ Object



71
72
73
74
75
# File 'lib/dnsmadeeasy-rest-api.rb', line 71

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) ⇒ Object


————- FAILOVER ————-




171
172
173
# File 'lib/dnsmadeeasy-rest-api.rb', line 171

def get_failover_config(record_id)
  get "/monitor/#{record_id}"
end

#get_id_by_domain(domain_name) ⇒ Object


————- DOMAINS ————-




34
35
36
# File 'lib/dnsmadeeasy-rest-api.rb', line 34

def get_id_by_domain(domain_name)
  get("/dns/managed/id/#{domain_name}")['id']
end

#records_for(domain_name) ⇒ Object


————- RECORDS ————-




62
63
64
# File 'lib/dnsmadeeasy-rest-api.rb', line 62

def records_for(domain_name)
  get "/dns/managed/#{get_id_by_domain(domain_name)}/records"
end

#update_failover_config(record_id, ips, desc, protocol = 'TCP', options = {}) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/dnsmadeeasy-rest-api.rb', line 175

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_record(domain, record_id, name, type, value, options = {}) ⇒ Object



148
149
150
151
# File 'lib/dnsmadeeasy-rest-api.rb', line 148

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 = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dnsmadeeasy-rest-api.rb', line 153

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