dnsmadeeasy 0.4.0 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +5 -10
  3. data/.gitignore +2 -0
  4. data/.rubocop.yml +5 -4
  5. data/.rubocop_todo.yml +251 -143
  6. data/CLAUDE.md +67 -0
  7. data/Gemfile +9 -1
  8. data/README.md +854 -0
  9. data/Rakefile +4 -4
  10. data/dnsmadeeasy.gemspec +56 -32
  11. data/docs/badges/coverage_badge.svg +21 -0
  12. data/docs/dry-cli-based-tools.webloc +8 -0
  13. data/docs/plan-zone-management.md +756 -0
  14. data/docs/plans/01-plan-ruby4-baseline.md +38 -0
  15. data/docs/plans/02-plan-dry-cli-launcher.md +44 -0
  16. data/docs/plans/03-plan-account-command.md +43 -0
  17. data/docs/plans/04-plan-zone-record-model.md +48 -0
  18. data/docs/plans/05-plan-zone-parser.md +41 -0
  19. data/docs/plans/06-plan-zone-formatter.md +43 -0
  20. data/docs/plans/07-plan-zone-export.md +58 -0
  21. data/docs/plans/08-plan-zone-diff.md +42 -0
  22. data/docs/plans/09-plan-zone-apply.md +69 -0
  23. data/docs/plans/10-plan-docs-cleanup.md +55 -0
  24. data/docs/plans/11-plan-zone-cli-arguments.md +114 -0
  25. data/docs/spec-zone-management.md +242 -0
  26. data/exe/dme +13 -2
  27. data/exe/dmez +6 -0
  28. data/lib/dme.rb +7 -2
  29. data/lib/dnsmadeeasy/api/client.rb +32 -26
  30. data/lib/dnsmadeeasy/cli/box_output.rb +9 -0
  31. data/lib/dnsmadeeasy/cli/commands/account.rb +222 -0
  32. data/lib/dnsmadeeasy/cli/commands/base.rb +119 -0
  33. data/lib/dnsmadeeasy/cli/commands/legacy_operation.rb +30 -0
  34. data/lib/dnsmadeeasy/cli/commands/version.rb +22 -0
  35. data/lib/dnsmadeeasy/cli/commands/zone.rb +392 -0
  36. data/lib/dnsmadeeasy/cli/commands.rb +19 -0
  37. data/lib/dnsmadeeasy/cli/input.rb +14 -0
  38. data/lib/dnsmadeeasy/cli/launcher.rb +53 -0
  39. data/lib/dnsmadeeasy/cli/message_helpers.rb +93 -0
  40. data/lib/dnsmadeeasy/cli/reported_error.rb +10 -0
  41. data/lib/dnsmadeeasy/credentials/api_keys.rb +11 -9
  42. data/lib/dnsmadeeasy/credentials.rb +0 -1
  43. data/lib/dnsmadeeasy/runner.rb +24 -24
  44. data/lib/dnsmadeeasy/types.rb +19 -0
  45. data/lib/dnsmadeeasy/version.rb +2 -1
  46. data/lib/dnsmadeeasy/zone/aname_flattener.rb +63 -0
  47. data/lib/dnsmadeeasy/zone/apply_executor.rb +189 -0
  48. data/lib/dnsmadeeasy/zone/apply_result.rb +22 -0
  49. data/lib/dnsmadeeasy/zone/diff.rb +152 -0
  50. data/lib/dnsmadeeasy/zone/file.rb +26 -0
  51. data/lib/dnsmadeeasy/zone/parser.rb +172 -0
  52. data/lib/dnsmadeeasy/zone/plan.rb +28 -0
  53. data/lib/dnsmadeeasy/zone/plan_action.rb +29 -0
  54. data/lib/dnsmadeeasy/zone/plan_renderer.rb +91 -0
  55. data/lib/dnsmadeeasy/zone/provider_record.rb +18 -0
  56. data/lib/dnsmadeeasy/zone/record.rb +44 -0
  57. data/lib/dnsmadeeasy/zone/record_set.rb +23 -0
  58. data/lib/dnsmadeeasy/zone/remote_adapter.rb +94 -0
  59. data/lib/dnsmadeeasy/zone/remote_records.rb +26 -0
  60. data/lib/dnsmadeeasy/zone/serializer.rb +119 -0
  61. data/lib/dnsmadeeasy.rb +61 -27
  62. metadata +212 -39
  63. data/.dme-help.png +0 -0
  64. data/README.adoc +0 -690
data/README.md ADDED
@@ -0,0 +1,854 @@
1
+ # DnsMadeEasy (`dmez`)
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/dnsmadeeasy.svg?icon=si%3Arubygems)](https://badge.fury.io/rb/dnsmadeeasy) ![coverage](docs/badges/coverage_badge.svg) ![Gem Total Downloads](https://img.shields.io/gem/dt/dnsmadeeasy?style=for-the-badge&logoSize=auto)
4
+
5
+ ## Ruby Client API Library Supporting Rest API SDK V2.0
6
+
7
+ ### Gem Version 1.0 Supporting Zone Exports, Formatting, Plan, Apply
8
+
9
+ > [!IMPORTANT]
10
+ >
11
+ > This gem is not backwards compatible with the previous version. It has been updated to work with DNS zone files and follow Terraform's pattern of the `read` → `plan` → `apply` loop. The old `dme` executable is deprecated in favor of `dmez`.
12
+
13
+ ______________________________________________________________________
14
+
15
+ This gem ships two things:
16
+
17
+ 1. The **`dmez` command line tool** — manage your DNS Made Easy zones as standard zone files with a Terraform-style `export` → `plan` → `apply` workflow, plus direct access to every account API operation.
18
+ 1. A **fully featured Ruby client** for the DNS Made Easy REST API V2.0.
19
+
20
+ DME is an **excellent** provider, and is highly recommended for their ease of use, very solid API, and great customer support. They also offer free DNS failover with business accounts, which is highly recommended for the arrays of load balancers in front of your app.
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'dnsmadeeasy'
28
+ ```
29
+
30
+ And then execute `bundle`, or install it yourself:
31
+
32
+ ```console
33
+ gem install dnsmadeeasy
34
+ ```
35
+
36
+ ## The `dmez` CLI
37
+
38
+ ```bash
39
+ $ dmez --help
40
+ Commands:
41
+ dmez account [ARGUMENT|SUBCOMMAND] # Execute DNS Made Easy account API operation
42
+ dmez version # Print version
43
+ dmez zone [SUBCOMMAND] # Zone file commands
44
+ ```
45
+
46
+ Every command exits `0` on success and `1` on failure, and **stdout carries only payload** (zone files, plan output) — all status boxes, warnings, and progress go to stderr. Piping and redirection are always safe, and every successful operation ends with a green summary box on stderr telling you what was done.
47
+
48
+ ### CLI Credentials
49
+
50
+ You can find your API Key and Secret on the [Account Settings Page](https://cp.dnsmadeeasy.com/account/info) of the DME UI. The CLI resolves credentials in this order:
51
+
52
+ 1. Explicit flags: `--api-key=KEY --api-secret=SECRET`
53
+ 1. An INI file passed via `--credentials=PATH`
54
+ 1. Environment variables `DNSMADEEASY_API_KEY` and `DNSMADEEASY_API_SECRET`
55
+ 1. A default INI file: `~/.dnsmadeeasy/credentials.ini` (or `./.dnsmadeeasy/credential.ini`)
56
+
57
+ The INI format is two lines:
58
+
59
+ ```ini
60
+ dns_dnsmadeeasy_api_key = 06cb6d10-63ca-013f-380b-66219a96f4e3
61
+ dns_dnsmadeeasy_secret_key = 14b9eb80-63ca-013f-380c-66219a96f4e3
62
+ ```
63
+
64
+ (The Ruby API additionally supports multi-account YAML files with optional encryption — see [Ruby API](#ruby-api) below.)
65
+
66
+ ### Zone Management: `dmez zone`
67
+
68
+ This is the flagship feature of version 1.0: your DME zones become plain text files you can diff, review, version-control, and apply — the same `read` → `plan` → `apply` loop you know from Terraform.
69
+
70
+ ```bash
71
+ $ dmez zone --help
72
+ Commands:
73
+ dmez zone apply DOMAIN FILE # Apply DNS changes for a zone file
74
+ dmez zone export DOMAIN # Export DNS Made Easy records as a canonical zone file
75
+ dmez zone fmt FILE # Format a DNS zone file
76
+ dmez zone plan DOMAIN FILE # Plan DNS changes for a zone file
77
+ dmez zone validate FILE # Validate a DNS zone file
78
+ ```
79
+
80
+ #### `dmez zone export`
81
+
82
+ Exports the live records of a domain as a canonical, normalized zone file:
83
+
84
+ ```bash
85
+ dmez zone export example.com --output=example.com.zone
86
+ ```
87
+
88
+ A real-world export looks like this:
89
+
90
+ ```dns
91
+ $ORIGIN example.com.
92
+ $TTL 60
93
+
94
+ @ 300 IN A 151.101.3.52
95
+ @ 300 IN A 151.101.67.52
96
+ @ 300 IN ANAME t.sni.global.fastly.net.
97
+ * 300 IN CNAME t.sni.global.fastly.net.
98
+ _acme-challenge IN CNAME validation-abc123.acme-validations.example.net.
99
+ dev IN A 127.0.0.1
100
+ mail._domainkey IN CNAME mail.domainkey.abc123.mailprovider.example.net.
101
+ ssh 300 IN A 203.0.113.22
102
+
103
+ @ IN MX 10 mail.protonmail.ch.
104
+ @ IN MX 20 mailsec.protonmail.ch.
105
+
106
+ _imaps._tcp 1800 IN SRV 0 1 993 imap.fastmail.com.
107
+ _submission._tcp 1800 IN SRV 0 1 587 smtp.fastmail.com.
108
+
109
+ @ IN TXT "v=spf1 include:_spf.protonmail.ch ~all"
110
+ @ IN TXT "google-site-verification=abc123def456"
111
+ _dmarc IN TXT "v=DMARC1; p=quarantine"
112
+ ```
113
+
114
+ Things to notice:
115
+
116
+ - **Fixed columns**: owner (30 chars), TTL (5, blank when it matches `$TTL`, right-aligned otherwise), class, and type — so even zones with long `_domainkey` owners stay perfectly aligned.
117
+ - **`$TTL` is derived from your records** — it is set to the most common record TTL, and only records that deviate carry an explicit TTL (the `300` and `1800` above). The export is TTL-lossless: re-parsing it yields exactly the TTLs the provider reported.
118
+ - **ANAME records are preserved as ANAME.** ANAME is not a standard DNS record type (it exists only inside providers), and DME's own zone export *flattens* ANAMEs into resolved A-record snapshots — which go stale as soon as the target rotates. `dmez` keeps them first-class so the file remains a faithful, apply-able representation of your account. If you need an RFC-portable file (for BIND, or to migrate providers), pass `--strict-rfc` to the `dmez zone export` command: ANAMEs are flattened into their currently resolved A records, and a warning is printed for each conversion — if and only if a conversion happened.
119
+ - **Apex NS records are omitted by default** since DME manages them; pass `--include-apex-ns` to include them.
120
+ - Records are sorted and grouped deterministically, so exports diff cleanly in git.
121
+
122
+ Options: `--format=rfc|json|yaml` (default `rfc`), `--output=FILE`, `--ttl=N` (default TTL for records the provider returns without one), `--include-apex-ns`, `--strict-rfc`.
123
+
124
+ #### `dmez zone validate`
125
+
126
+ Parses a zone file and reports whether it is usable, with parse errors when it is not:
127
+
128
+ ```bash
129
+ $ dmez zone validate example.com.zone
130
+ ╔ OK ════════════════════════════════════════╗
131
+ ║ Zone file is valid. ║
132
+ ║ Records: 18 ║
133
+ ╚════════════════════════════════════════════╝
134
+ ```
135
+
136
+ Any valid zone file parses as-is — including raw exports from DNS providers with fully-qualified owners, tab separation, per-record TTLs, and SOA records (SOA is ignored, since DME manages it). You do **not** need to run `fmt` first.
137
+
138
+ #### `dmez zone fmt`
139
+
140
+ Rewrites any valid zone file into the canonical format shown above (stdout):
141
+
142
+ ```bash
143
+ dmez zone fmt provider-export.zone > example.com.zone
144
+ ```
145
+
146
+ #### `dmez zone plan`
147
+
148
+ The heart of the workflow: compares a zone file (desired state) against the live records (actual state) and prints what would change — without changing anything.
149
+
150
+ ```bash
151
+ $ dmez zone plan example.com example.com.zone
152
+ No changes.
153
+ ```
154
+
155
+ When there are differences, the plan groups them into sections:
156
+
157
+ ```text
158
+ Create
159
+ - www CNAME @ (ttl=300)
160
+ Update
161
+ - send TXT v=spf1 include:example.net ~all (ttl=60) -> send TXT v=spf1 include:example.org ~all (ttl=60)
162
+ Skipped Creates
163
+ - @ NS ns0.dnsmadeeasy.com. (ttl=86400) (Apex NS records are managed by the DNS provider)
164
+ Skipped Deletes
165
+ - old CNAME retired.example.net. (ttl=300) (Delete skipped by default)
166
+ Manual Review
167
+ - @ A 151.101.131.52 (ttl=300) -> @ A 140.248.151.52 (ttl=300) (Multiple records share the same owner/type identity (desired: 5, remote: 4))
168
+ ```
169
+
170
+ - **Create** — records in the file but not in the account.
171
+ - **Update** — records whose value changed (matched by owner and type).
172
+ - **Skipped Creates** — apex NS records from the file; DME manages these and the API will not create them.
173
+ - **Skipped Deletes** — records in the account but not in the file. Deletes never happen by default; see `apply --delete-only`.
174
+ - **Manual Review** — multiple records share the same owner/type identity and cannot be paired one-to-one (for example five apex `A` records in the file versus four in the account). The plan refuses to guess.
175
+
176
+ Options: `--format=text|json`, `--diff-ttl`.
177
+
178
+ The domain is always the first argument, and the zone file's `$ORIGIN` is cross-checked against it (trailing dot and case are ignored): if the file says `$ORIGIN other.example.` while you passed `example.com`, the command fails before a single API call is made. Diffing the wrong domain against the wrong file should be an error, not a surprise apply.
179
+
180
+ #### TTL Handling
181
+
182
+ `zone plan` and `zone apply` **ignore TTL-only differences by default**: a record whose only deviation from the remote is its TTL is considered unchanged, and value updates preserve the remote TTL. Pass `--diff-ttl` to treat TTLs as part of the record — TTL-only differences then become updates, and applied records take the TTL from the zone file:
183
+
184
+ ```bash
185
+ $ dmez zone plan example.com example.com.zone --diff-ttl
186
+ Update
187
+ - click CNAME links.cdn.example.net. (ttl=120) -> click CNAME links.cdn.example.net. (ttl=300)
188
+ ```
189
+
190
+ #### `dmez zone apply`
191
+
192
+ Executes the plan against DNS Made Easy. You will be shown the number of actions and asked to confirm (skip the prompt with `--yes`):
193
+
194
+ ```bash
195
+ $ dmez zone apply example.com example.com.zone
196
+ Apply 3 action(s)? Type yes to continue:
197
+ yes
198
+ ╔ ✔ OK ══════════════════════════════════════╗
199
+ ║ Zone apply complete for example.com. ║
200
+ ║ Applied: 3 ║
201
+ ║ Failed: 0 ║
202
+ ║ Skipped: 2 ║
203
+ ╚════════════════════════════════════════════╝
204
+ ```
205
+
206
+ Three modes control the blast radius:
207
+
208
+ | Mode | Flag | Behavior |
209
+ | --------------- | --------------------- | -------------------------------------------------------------------------- |
210
+ | Merge (default) | `--merge` | Applies creates and updates; never deletes. |
211
+ | Add only | `--add-only`, `-a` | Only creates missing records. |
212
+ | Delete only | `--delete-only`, `-d` | Only deletes records absent from the file (SOA and apex NS are protected). |
213
+
214
+ Actions run concurrently (4 workers) with progress spinners on stderr, so stdout stays clean for scripting.
215
+
216
+ #### A Complete Workflow
217
+
218
+ Setting up transactional email for a subdomain, start to finish:
219
+
220
+ ```bash
221
+ # 1. Read: export current state
222
+ dmez zone export mail.example.com --output=mail.example.com.zone
223
+
224
+ # 2. Edit: add the records your email provider asks for
225
+ $EDITOR mail.example.com.zone
226
+
227
+ # 3. Validate and review the plan
228
+ dmez zone validate mail.example.com.zone
229
+ dmez zone plan mail.example.com mail.example.com.zone
230
+
231
+ # 4. Apply (add-only is the safest mode for additive changes)
232
+ dmez zone apply mail.example.com mail.example.com.zone --add-only --yes
233
+
234
+ # 5. Verify: re-running plan should now be a no-op
235
+ dmez zone plan mail.example.com mail.example.com.zone
236
+ # => No changes.
237
+ ```
238
+
239
+ The edited file from step 2 might look like:
240
+
241
+ ```dns
242
+ $ORIGIN mail.example.com.
243
+ $TTL 300
244
+
245
+ click IN CNAME links.mailer.example.net.
246
+
247
+ @ IN MX 10 inbound-smtp.us-east-1.amazonaws.com.
248
+ send IN MX 10 feedback-smtp.us-east-1.amazonses.com.
249
+
250
+ _dmarc IN TXT "v=DMARC1; p=none;"
251
+ mailer._domainkey IN TXT "p=MIGfMA0GCSqGSIb3DQE7abc123...AQAB"
252
+ send IN TXT "v=spf1 include:amazonses.com ~all"
253
+ ```
254
+
255
+ > [!NOTE]
256
+ > TXT and SPF values are stored unquoted internally and quoted exactly once in zone files — regardless of whether the DME API returns them with embedded quotes. You never need to worry about double-quoting.
257
+
258
+ ### Account Operations: `dmez account`
259
+
260
+ The `account` command exposes every operation of the underlying API client (the complete list of the legacy CLI):
261
+
262
+ ```bash
263
+ ❯ dmez account --list
264
+ all
265
+ base_url
266
+ create_a_record
267
+ create_aaaa_record
268
+ create_cname_record
269
+ create_domain
270
+ create_domains
271
+ create_httpred_record
272
+ create_mx_record
273
+ create_ns_record
274
+ create_ptr_record
275
+ create_record
276
+ create_secondary_domain
277
+ create_secondary_domains
278
+ create_secondary_ip_set
279
+ create_spf_record
280
+ create_srv_record
281
+ create_txt_record
282
+ delete_all_records
283
+ delete_domain
284
+ delete_record
285
+ delete_records
286
+ delete_secondary_domain
287
+ delete_secondary_ip_set
288
+ domain
289
+ domains
290
+ find_all
291
+ find_first
292
+ find_record_ids
293
+ get_id_by_domain
294
+ get_id_by_secondary_domain
295
+ records_for
296
+ secondary_domain
297
+ secondary_domains
298
+ secondary_ip_set
299
+ secondary_ip_sets
300
+ update_record
301
+ update_records
302
+ update_secondary_domains
303
+ update_secondary_ip_set
304
+ ```
305
+
306
+ Run `dmez account <operation> --help` for the signature of any operation, and pass `--format=json|json_pretty|yaml|pp` to control the output format:
307
+
308
+ ```bash
309
+ dmez account domains --format=json_pretty
310
+ dmez account records_for example.com
311
+ dmez account create_a_record example.com www 203.0.113.10
312
+ ```
313
+
314
+ <a name="ruby-api"></a>
315
+
316
+ ## Ruby API
317
+
318
+ **DnsMadeEasy** allows you to fetch, create, update DNS records from Ruby, as long as you know your API key and the secret.
319
+
320
+ ### Setting up Credentials
321
+
322
+ Once you have the key and the secret, you have several choices:
323
+
324
+ #### Credentials Methods for Both: CLI and Ruby API
325
+
326
+ > [!NOTE]
327
+ >
328
+ > Credentials used in these examples are random UUIDs generated specifically for these examples and are not actual valid API keys and a secret.
329
+
330
+ - Set environment variables `DNSMADEEASY_API_KEY` and `DNSMADEEASY_API_SECRET`
331
+
332
+ - Put a `credentials.ini` file in the `~/.dnsmadeeasy` folder in your home, or in the current directory. The file's format is very simple:
333
+
334
+ ```ini
335
+ dns_dnsmadeeasy_api_key = 06cb6d10-63ca-013f-380b-66219a96f4e3
336
+ dns_dnsmadeeasy_secret_key = 14b9eb80-63ca-013f-380c-66219a96f4e3
337
+ ```
338
+
339
+ - If you prefer the YAML format, place the credentials into the file `~/.dnsmadeeasy/credentials.yml`:
340
+
341
+ ```yaml
342
+ # file: ~/.dnsmadeeasy/credentials.yml
343
+ credentials:
344
+ api_key: 06cb6d10-63ca-013f-380b-66219a96f4e3
345
+ api_secret: 14b9eb80-63ca-013f-380c-66219a96f4e3
346
+ ```
347
+
348
+ - Or set environment variable `DNSMADEEASY_CREDENTIALS_FILE` to whatever path your credentials live. Either YAML or INI format will be supported.
349
+
350
+ #### Credentials Methods Ruby API Only
351
+
352
+ - You can directly instantiate a new instance of the `Client` class, by passing your API key and API secrets as arguments:
353
+
354
+ ```ruby
355
+ require 'dnsmadeeasy'
356
+ @client = DnsMadeEasy::Api::Client.new(api_key, api_secret)
357
+ ```
358
+
359
+ - Or, you can use the `DnsMadeEasy.configure` method to configure the key/secret pair, and then use `DnsMadeEasy` namespace to call the methods:
360
+
361
+ ```ruby
362
+ require 'dnsmadeeasy'
363
+
364
+ DnsMadeEasy.configure do |config|
365
+ config.api_key = 'XXXX'
366
+ config.api_secret = 'YYYY'
367
+ end
368
+
369
+ DnsMadeEasy.domains.data.first.name #=> 'moo.gamespot.com'
370
+ ```
371
+
372
+ ### Multi-Account Credentials File Format
373
+
374
+ Below you see two accounts, with production key and secret being encrypted. See [further below](#encryption) about encrypting your key and secrets.
375
+
376
+ ```yaml
377
+ accounts:
378
+ - name: development
379
+ default_account: true
380
+ credentials:
381
+ api_key: 12345678-a8f8-4466-ffff-2324aaaa9098
382
+ api_secret: 43009899-abcc-ffcc-eeee-09f809808098
383
+ - name: production
384
+ credentials:
385
+ api_key: "BAhTOh1TeW06OkRhdGE6OldyYXBwZXJT............"
386
+ api_secret: "BAhTOh1TeW06OkRhdGE6OldyYXBwZ............"
387
+ encryption_key: spec/fixtures/sym.key
388
+ ```
389
+
390
+ You can use the following method to access both simple and multi-account YAML configurations:
391
+
392
+ ```ruby
393
+ require 'dnsmadeeasy'
394
+ DnsMadeEasy.configure_from_file(file, account = nil, encryption_key = nil)
395
+
396
+ # for example:
397
+ DnsMadeEasy.configure_from_file('config/dme.yaml', 'production')
398
+ DnsMadeEasy.domains #=> [ ... ]
399
+
400
+ # or with encrypted key passed as an argument to decrypt YAML values:
401
+ DnsMadeEasy.configure_from_file(
402
+ 'config/dme.yaml',
403
+ 'production',
404
+ ENV['PRODUCTION_KEY'])
405
+ ```
406
+
407
+ Finally, you can use `DME.credentials_from_file` method that, unlike the method above, uses hash arguments:
408
+
409
+ ```ruby
410
+ @creds = DnsMadeEasy.credentials_from_file(file: 'my-creds.yml',
411
+ account: 'production',
412
+ encryption_key: 'MY_KEY')
413
+ @creds.api_key # => ...
414
+ @creds.api_secret # => ...
415
+ ```
416
+
417
+ Method above simply returns the credentials instance, but does not "save" it as the default credentials like `configure_from_file`. Therefore, if you need to access multiple accounts at the same time, this method will help you maintain multiple credentials at the same time.
418
+
419
+ Once you configure the keys, you can also use the shortcut module to save you some typing:
420
+
421
+ ```ruby
422
+ require 'dnsmadeeasy/dme'
423
+ DME.domains.data.first.name #=> 'moo.gamespot.com'
424
+ ```
425
+
426
+ This has the advantage of being much shorter, but might conflict with existing modules in your Ruby VM.
427
+ In this case, just do not require `dnsmadeeasy/dme` and only require `dnsmadeeasy`, and you'll be fine.
428
+ Otherwise, using `DME` is identical to using `DnsMadeEasy`, assuming you required `dnsmadeeasy/dme` file.
429
+
430
+ ### Which Namespace to Use? What is `DME` versus `DnsMadeEasy`?
431
+
432
+ Since `DnsMadeEasy` is a bit of a mouthful, we decided to offer (in addition to the standard `DnsMadeEasy` namespace) the abbreviated module `DME` that simply forwards all messages to the module `DnsMadeEasy`. If in your Ruby VM there is no conflicting top-level class `DME`, then you can `require 'dnsmadeeasy/dme'` to get all of the DnsMadeEasy client library functionality without having to type the full name once. You can even do `require 'dme'`.
433
+
434
+ Whenever you require `dme` you also import the `DnsMadeEasy` namespace. **The opposite is not true.**
435
+
436
+ So if you DO have a name clash with another top-level module `DME`, simply do `require 'dnsmadeeasy'` and none of the `DME` module namespace will be loaded.
437
+
438
+ In a nutshell you have three ways to access all methods provided by the [`DnsMadeEasy::Api::Client`](http://www.rubydoc.info/gems/dnsmadeeasy/DnsMadeEasy/Api/Client) class:
439
+
440
+ 1. Instantiate and use the client class directly,
441
+ 1. Use the top-level module `DnsMadeEasy` with `require 'dnsmadeeasy'`
442
+ 1. Use the shortened top-level module `DME` with `require 'dnsmadeeasy/dme'`
443
+
444
+ ### Examples
445
+
446
+ Whether or not you are accessing a single account or multiple, it is recommended that you save your credentials (the API key and the secret) encrypted in the above mentioned file `~/.dnsmadeeasy/credentials.yml` (or any file of you preference).
447
+
448
+ > [!WARNING]
449
+ >
450
+ > _**DO NOT check that file into your repo! If you use encryption, do not check in your key!**_
451
+
452
+ The examples that follow assume credentials have already been configured, and so we explore the API.
453
+
454
+ Using the `DME` module (or `DnsMadeEasy` if you prefer) you can access all of your records through the available API method calls, for example:
455
+
456
+ ```ruby
457
+ IRB > require 'dme' #=> true
458
+ # Or you can also do
459
+ IRB > require 'dnsmadeeasy/dme' #=> true
460
+ IRB > DME.domains.data.map(&:name)
461
+ ⤷ ["demo.gamespot.systems",
462
+ "dev.gamespot.systems",
463
+ "gamespot.live",
464
+ "gamespot.systems",
465
+ "prod.gamespot.systems"
466
+ ]
467
+
468
+ # These have been read from the file ~/.dnsmadeeasy/credentials.yml
469
+ IRB > DME.api_key
470
+ ⤷ "2062259f-f666b17-b1fa3b48-042ad4030"
471
+
472
+ IRB > DME.api_secret
473
+ ⤷ "2265bc3-e31ead-95b286312e-c215b6a0"
474
+
475
+ IRB > DME.domain('gamespot.live').delegateNameServers
476
+ ⤷ #<Hashie::Array ["ns-125-c.gandi.net.", "ns-129-a.gandi.net.", "ns-94-b.gandi.net."]>
477
+
478
+ # Let's inspect the Client — after all, all methods are simply delegated to it:
479
+ IRB > @client = DME.client
480
+ ⤷ #<DnsMadeEasy::Api::Client:0x00007fb6b416a4c8
481
+ @api_key="2062259f-f666b17-b1fa3b48-042ad4030",
482
+ @api_secret="2265bc3-e31ead-95b286312e-c215b6a0",
483
+ @options={},
484
+ @requests_remaining=149,
485
+ @request_limit=150,
486
+ @base_uri="https://api.dnsmadeeasy.com/V2.0">
487
+ ```
488
+
489
+ Next, let's fetch a particular domain, get it's records and compute the counts for each record type, such as 'A', 'NS', etc.
490
+
491
+ ```ruby
492
+ IRB > records = DME.records_for('gamespot.com')
493
+ IRB > [ records.totalPages, records.totalRecords ]
494
+ ⤷ [1, 33]
495
+ IRB > records.data.select{|f| f.type == 'A' }.map(&:name)
496
+ ⤷ ["www", "vpn-us-east1", "vpn-us-east2", "staging", "yourmom"]
497
+ IRB > types = records.data.map(&:type)
498
+ ⤷ [....]
499
+ IRB > require 'awesome_print'
500
+ IRB > ap Hash[types.group_by {|x| x}.map {|k,v| [k,v.count]}]
501
+ {
502
+ "MX" => 2,
503
+ "TXT" => 1,
504
+ "CNAME" => 3,
505
+ "NS" => 22,
506
+ "A" => 5
507
+ }
508
+ ```
509
+
510
+ ### Return Value Types
511
+
512
+ All public methods of this library return a Hash-like object, that is actually an instance of the class [`Hashie::Mash`](https://github.com/intridea/hashie). `Hashie::Mash` supports the very useful ability to reach deeply nested hash values via a chain of method calls instead of using a train of square brackets. You can always convert it to a regular hash either `to_hash` or `to_h` on an instance of a `Hashie::Mash` to get a pure hash representation.
513
+
514
+ > [!NOTE]
515
+ >
516
+ > `to_hash` converts the entire object to a regular hash, including the deeply nested hashes, while `to_h` only converts the primary object, but not the nested hashes. Here is an example below -- in the first instance where we call `to_h` we are still able to call `.value` on the nested object, because only the top-level `Mash` has been converted into a `Hash`. In the second example, this call fails, because this method does not exist, and the value must be accessed via the square brackets:
517
+
518
+ ```ruby
519
+ IRB > recs.to_h['data'].last.value
520
+ ⤷ "54.200.26.233"
521
+ IRB > recs.to_hash['data'].last.value
522
+ "NoMethodError: undefined method `value` for #<Hash:0x00007fe36fab0f68>"
523
+ IRB > recs.to_hash['data'].last['value']
524
+ ⤷ "54.200.26.233"
525
+ ```
526
+
527
+ For more information on the actual JSON API, please refer to the [following PDF document](http://www.dnsmadeeasy.com/integration/pdf/API-Docv2.pdf).
528
+
529
+ ### Available Actions
530
+
531
+ Here is the complete list of all methods supported by the `DnsMadeEasy::Api::Client`:
532
+
533
+ #### Domains
534
+
535
+ - `create_domain`
536
+ - `create_domains`
537
+ - `delete_domain`
538
+ - `domain`
539
+ - `domains`
540
+ - `get_id_by_domain`
541
+
542
+ #### Records
543
+
544
+ - `records_for`
545
+ - `all`
546
+ - `base_uri`
547
+ - `create_a_record`
548
+ - `create_aaaa_record`
549
+ - `create_cname_record`
550
+ - `create_httpred_record`
551
+ - `create_mx_record`
552
+ - `create_ns_record`
553
+ - `create_ptr_record`
554
+ - `create_record`
555
+ - `create_spf_record`
556
+ - `create_srv_record`
557
+ - `create_txt_record`
558
+ - `delete_all_records`
559
+ - `delete_record`
560
+ - `delete_records`
561
+ - `find_all`
562
+ - `find_first`
563
+ - `find_record_ids`
564
+
565
+ #### Secondary Domains
566
+
567
+ - `secondary_domain`
568
+ - `secondary_domains`
569
+ - `get_id_by_secondary_domain`
570
+ - `create_secondary_domain`
571
+ - `create_secondary_domains`
572
+ - `update_secondary_domains`
573
+ - `delete_secondary_domain`
574
+
575
+ #### Secondary IpSets
576
+
577
+ - `secondary_ip_set`
578
+ - `secondary_ip_sets`
579
+ - `create_secondary_ip_set`
580
+ - `update_secondary_ip_set`
581
+ - `delete_secondary_ip_set`
582
+
583
+ ### Managing Domains
584
+
585
+ > [!NOTE]
586
+ >
587
+ > Below we can be using `@client` instantiated with given key and secret, or `DME` or `DnsMadeEasy` module.
588
+
589
+ To retrieve all domains:
590
+
591
+ ```ruby
592
+ require 'dnsmadeeasy/dme'
593
+ DME.domains
594
+ ```
595
+
596
+ To retreive the id of a domain by the domain name:
597
+
598
+ ```ruby
599
+ DME.get_id_by_domain('test.io')
600
+ ```
601
+
602
+ To retrieve the full domain record by domain name:
603
+
604
+ ```ruby
605
+ DME.domain('test.io')
606
+ ```
607
+
608
+ To create a domain:
609
+
610
+ ```ruby
611
+ DME.create_domain('test.io')
612
+ # Multiple domains can be created by:
613
+ DME.create_domains(%w[test.io moo.re])
614
+ ```
615
+
616
+ To delete a domain:
617
+
618
+ ```ruby
619
+ DME.delete_domain ('test.io')
620
+ ```
621
+
622
+ ### Managing Secondary Domains
623
+
624
+ To retrieve all secondary domains:
625
+
626
+ ```ruby
627
+ DME.secondary_domains
628
+ ```
629
+
630
+ To retrieve secondary domain by id:
631
+
632
+ ```ruby
633
+ DME.secondary_domain(domain_id)
634
+ ```
635
+
636
+ To retrieve the id of a domain by the secondary domain name:
637
+
638
+ ```ruby
639
+ DME.get_id_by_secondary_domain('test.io')
640
+ ```
641
+
642
+ To create a secondary domain:
643
+
644
+ ```ruby
645
+ # IP_SET_ID is id of ip_set you want to associate domain with
646
+ DME.create_secondary_domain('test.io', IP_SET_ID)
647
+
648
+ # Multiple domains can be created by:
649
+ DME.create_secondary_domains(%w[test.io moo.re], IP_SET_ID)
650
+ ```
651
+
652
+ To update a secondary domain:
653
+
654
+ ```ruby
655
+ # IP_SET_ID is id of ip_set you want to associate
656
+ # DOMAIN_ID is id of domain
657
+ DME.update_secondary_domains([DOMAIN_ID], IP_SET_ID)
658
+ ```
659
+
660
+ To delete a secondary domain:
661
+
662
+ ```ruby
663
+ DME.delete_secondary_domain('test.io')
664
+ ```
665
+
666
+ ### Managing Secondary IpSets
667
+
668
+ To retrieve all secondary IpSets:
669
+
670
+ ```ruby
671
+ DME.secondary_ip_sets
672
+ ```
673
+
674
+ To retrieve single ipSet:
675
+
676
+ ```ruby
677
+ DME.secondary_ip_set(IP_SET_ID)
678
+ ```
679
+
680
+ To create an ipSet:
681
+
682
+ ```ruby
683
+ # IP_LIST is list of ips to be associated with this ip_set, like %w[8.8.8.8, 1.1.1.1]
684
+ DME.create_secondary_ip_set('ip-set-name', IP_LIST)
685
+ ```
686
+
687
+ To update an ipSet:
688
+
689
+ ```ruby
690
+ DME.update_secondary_ip_set(IP_SET_ID, 'ip-list-name', IP_LIST)
691
+ ```
692
+
693
+ To delete an ipSet:
694
+
695
+ ```ruby
696
+ DME.delete_secondary_ip_set(IP_SET_ID)
697
+ ```
698
+
699
+ ### Managing Records
700
+
701
+ To retrieve all records for a given domain name:
702
+
703
+ ```ruby
704
+ DME.all('test.io')
705
+ ```
706
+
707
+ To find the record id for a given domain, name, and type:
708
+
709
+ This finds all of the IDs matching 'woah.test.io' type 'A':
710
+
711
+ ```ruby
712
+ DME.find_record_ids ('test.io', 'woah', 'A')
713
+ # => [ 234234, 2342345 ]
714
+ ```
715
+
716
+ ```ruby
717
+ # To delete a record by domain name and record id (the record id can be retrieved from `find_record_id`:
718
+ DME.delete_record ('test.io', 123)
719
+ # To delete multiple records:
720
+ DME.delete_records ('test.io', [123, 143])
721
+ # To delete all records in the domain:
722
+ DME.delete_all_records ('test.io')
723
+ ```
724
+
725
+ To create records of various types:
726
+
727
+ ```ruby
728
+ # The generic method:
729
+ DME.create_record ('test.io', 'woah', 'A', '127.0.0.1', { 'ttl' => '60' })
730
+
731
+ # Specialized methods:
732
+ DME.create_a_record ('test.io', 'woah', '127.0.0.1', {})
733
+ DME.create_aaaa_record ('test.io', 'woah', '127.0.0.1', {})
734
+ DME.create_ptr_record ('test.io', 'woah', '127.0.0.1', {})
735
+ DME.create_txt_record ('test.io', 'woah', '127.0.0.1', {})
736
+ DME.create_cname_record ('test.io', 'woah', '127.0.0.1', {})
737
+ DME.create_ns_record ('test.io', 'woah', '127.0.0.1', {})
738
+ DME.create_spf_record ('test.io', 'woah', '127.0.0.1', {})
739
+ ```
740
+
741
+ #### Specialized Record Types
742
+
743
+ Below are the method calls for `MX`, `SRV`, and `HTTPRED` types:
744
+
745
+ ```ruby
746
+ # Arguments are: domain_name, name, priority, value, options = {}
747
+ DME.create_mx_record ('test.io', 'woah', 5, '127.0.0.1', {})
748
+ # Arguments are: domain_name, name, priority, weight, port, value, options = {}
749
+ DME.create_srv_record ('test.io', 'woah', 1, 5, 80, '127.0.0.1', {})
750
+ # Arguments are: domain_name, name, value, redirectType,
751
+ DME.create_httpred_record('test.io', 'woah', '127.0.0.1', 'STANDARD - 302',
752
+ # description, keywords, title, options = {}
753
+ 'a description', 'keywords', 'a title', {})
754
+ ```
755
+
756
+ To update a record:
757
+
758
+ ```ruby
759
+ DME.update_record('test.io', 123, 'woah', 'A', '127.0.1.1', { 'ttl' => '60' })
760
+ ```
761
+
762
+ To update several records:
763
+
764
+ ```ruby
765
+ DME.update_records('test.io',
766
+ [
767
+ { 'id' => 123,
768
+ 'name' => 'buddy',
769
+ 'type' => 'A',
770
+ 'value'=> '127.0.0.1'
771
+ }
772
+ ], { 'ttl' => '60' })
773
+ ```
774
+
775
+ To get the number of API requests remaining after a call:
776
+
777
+ ```ruby
778
+ DME.requests_remaining
779
+ #=> 19898
780
+ ```
781
+
782
+ > [!NOTE]
783
+ > Information is not available until an API call has been made.
784
+
785
+ To get the API request total limit after a call:
786
+
787
+ ```ruby
788
+ DME.request_limit
789
+ #=> 2342
790
+
791
+ ```
792
+
793
+ <a name="encryption"></a>
794
+
795
+ ### Encryption
796
+
797
+ It was mentioned above that the credentials YAML file may contain encrypted values. This facility is provided by the encryption gem [Sym](https://github.com/kigster/sym).
798
+
799
+ > [!IMPORTANT]
800
+ >
801
+ > There is a much better encryption facility called `sopsy` written in Rust. We highly recommend you use `sopsy` for encrypting your secrets, as it mints private keys on a Mac inside Appple Enclave hardware chip. For more information please see [sopsy's website](https://sopsy-cli.dev) or the [Github Project](https://github.com/kigster/sopsy).
802
+
803
+ ______________________________________________________________________
804
+
805
+ In order to encrypt your values, you need to perform the following steps:
806
+
807
+ ```bash
808
+ gem install sym
809
+
810
+ # let's generate a new key and save it to a file:
811
+ sym -g -o my.key
812
+
813
+ # if you are on Mac OS-X, you can import the key into the KeyChain.
814
+ # this creates an entry in the keychain named 'my.key' that can be used later.
815
+ sym -g -x my.key
816
+ ```
817
+
818
+ Once you have the key generated, first, **make sure to never commit this to any repo!**. You can use 1Password for it, or something like that.
819
+
820
+ Let's encrypt our actual API key:
821
+
822
+ ```bash
823
+ api_key="12345678-a8f8-4466-ffff-2324aaaa9098"
824
+ api_secret="43009899-abcc-ffcc-eeee-09f809808098"
825
+ sym -ck my.key -e -s "${api_key}"
826
+ # => prints the encrypted value
827
+
828
+ # On a mac, you can copy it to clipboard:
829
+ sym -ck my.key -e -s "${api_secret}" | pbcopy
830
+ ```
831
+
832
+ Now, you place the encrypted values in the YAML file, and you can save "my.key" as the value against `encryption_key:` at the same level as the `api_key` and `api_secret` in the YAML file. This value can either point to a file path, or be a keychain name, or even a name of an environment variable. For full details, please see [sym documentation](https://github.com/kigster/sym#using-sym-with-the-command-line).
833
+
834
+ ## Development
835
+
836
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
837
+
838
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
839
+
840
+ ## Acknowledgements
841
+
842
+ The current maintainer [Konstantin Gredeskoul](https://github.com/kigster) wishes to thank:
843
+
844
+ - *Arnoud Vermeer* for the original `dnsmadeeasy-rest-api` gem
845
+ - *Andre Arko, Paul Henry, James Hart* formerly of [Wanelo](wanelo.com) fame, for bringing the REST API gem up to the level.
846
+ - *Phil Cohen*, who graciously transferred the ownership of the name of this gem on RubyGems.org to the current maintainer.
847
+
848
+ ## Contributing
849
+
850
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/kigster/dnsmadeeasy>.
851
+
852
+ ## License
853
+
854
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).