Skip to content

Commit 16185c8

Browse files
committed
Added domain record update and domain record delete functionality
1 parent 92a9b23 commit 16185c8

6 files changed

Lines changed: 215 additions & 71 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ See the [command reference][3] for complete planned command structure.
2929
* Show DNS Records whose content matches a specific filter (e.g. \*spf\*)
3030
* Add DNS Records to a domain
3131
* Show details for a domain DNS Record
32+
* Update DNS Records for a domain
33+
* Delete DNS Records for a domain
3234

3335
---
3436
* Contacts: `(command: dnsimple contact)`

command-reference.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
|- record
4141
|- list [-t --type <type>] [-f --filter <filter>] [domain]
4242
|- add [-r --recordname <recordname>] [-t --type <type>] [-c --content <content>] [domain]
43-
|- show [-i --id recordid] [domain]
44-
|- update
45-
|- delete
43+
|- show [-i --id recordid] [domain]
44+
|- update [-i --id recordid] [-r --recordname <recordname>] [-t --type <type>] [-c --content <content>] [domain]
45+
|- delete [-i --id recordid] [domain]
4646
--------------
4747
|- ns (name server)
4848
|- update

lib/commands/domain/domain._js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,16 @@ exports.init = function (cli) {
260260
return domainClient.addRecord(options, callback);
261261
};
262262

263+
domain.doRecordUpdate = function (options, callback) {
264+
var domainClient = new DomainClient(cli, options.subscription);
265+
return domainClient.updateRecord(options, callback);
266+
};
267+
268+
domain.doRecordDelete = function (options, callback) {
269+
var domainClient = new DomainClient(cli, options.subscription);
270+
return domainClient.deleteRecord(options, callback);
271+
};
272+
263273
domain.doRecordGet = function (options, callback) {
264274
var domainClient = new DomainClient(cli, options.subscription);
265275
return domainClient.getRecord(options, callback);

lib/commands/domain/domain.record._js

Lines changed: 138 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,48 @@ exports.init = function (cli) {
3030
var domainRecords = domain.category('record')
3131
.description($('Commands to manage your domain dns records'));
3232

33+
function promptForAndNormalizeRecordType(type, _) {
34+
type = cli.interaction.chooseIfNotGiven($('Record Type: '), $('Getting types'), type,
35+
function (cb) {
36+
cb(null, [ 'A', 'ALIAS', 'CNAME', 'MX', 'SPF', 'URL', 'TXT', 'NS', 'SRV', 'NAPTR', 'PTR', 'AAAA', 'SSHFP', 'HINFO', 'POOL' ]);
37+
}, _);
38+
39+
if (utils.ignoreCaseEquals(type, 'A')) {
40+
type = 'A';
41+
} else if (utils.ignoreCaseEquals(type, 'ALIAS')) {
42+
type = 'ALIAS';
43+
} else if (utils.ignoreCaseEquals(type, 'CNAME')) {
44+
type = 'CNAME';
45+
} else if (utils.ignoreCaseEquals(type, 'MX')) {
46+
type = 'MX';
47+
} else if (utils.ignoreCaseEquals(type, 'SPF')) {
48+
type = 'SPF';
49+
} else if (utils.ignoreCaseEquals(type, 'URL')) {
50+
type = 'URL';
51+
} else if (utils.ignoreCaseEquals(type, 'TXT')) {
52+
type = 'TXT';
53+
} else if (utils.ignoreCaseEquals(type, 'NS')) {
54+
type = 'NS';
55+
} else if (utils.ignoreCaseEquals(type, 'SRV')) {
56+
type = 'SRV';
57+
} else if (utils.ignoreCaseEquals(type, 'NAPTR')) {
58+
type = 'NAPTR';
59+
} else if (utils.ignoreCaseEquals(type, 'PTR')) {
60+
type = 'PTR';
61+
} else if (utils.ignoreCaseEquals(type, 'AAAA')) {
62+
type = 'AAAA';
63+
} else if (utils.ignoreCaseEquals(type, 'SSHFP')) {
64+
type = 'SSHFP';
65+
} else if (utils.ignoreCaseEquals(type, 'HINFO')) {
66+
type = 'HINFO';
67+
} else if (utils.ignoreCaseEquals(type, 'POOL')) {
68+
type = 'POOL';
69+
} else {
70+
throw new Error($('Invalid record type. Valid types are: A, ALIAS, CNAME, MX, SPF, URL, TXT, NS, SRV, NAPTR, PTR, AAAA, SSHFP, HINFO, POOL'));
71+
}
72+
return type;
73+
}
74+
3375
domainRecords.listCommand = function (name, options, _) {
3476
var context = {
3577
subscription: profile.current.getSubscription(options.subscription).id,
@@ -78,44 +120,7 @@ exports.init = function (cli) {
78120

79121
domainRecords.addCommand = function (recordname, type, content, name, options, _) {
80122
recordname = cli.interaction.promptIfNotGiven($('Record Name (Leave blank to create a record for the root domain.): '), recordname, _);
81-
type = cli.interaction.chooseIfNotGiven($('Record Type: '), $('Getting types'), type,
82-
function (cb) {
83-
cb(null, [ 'A', 'ALIAS', 'CNAME', 'MX', 'SPF', 'URL', 'TXT', 'NS', 'SRV', 'NAPTR', 'PTR', 'AAAA', 'SSHFP', 'HINFO', 'POOL' ]);
84-
}, _);
85-
86-
if (utils.ignoreCaseEquals(type, 'A')) {
87-
type = 'A';
88-
} else if (utils.ignoreCaseEquals(type, 'ALIAS')) {
89-
type = 'ALIAS';
90-
} else if (utils.ignoreCaseEquals(type, 'CNAME')) {
91-
type = 'CNAME';
92-
} else if (utils.ignoreCaseEquals(type, 'MX')) {
93-
type = 'MX';
94-
} else if (utils.ignoreCaseEquals(type, 'SPF')) {
95-
type = 'SPF';
96-
} else if (utils.ignoreCaseEquals(type, 'URL')) {
97-
type = 'URL';
98-
} else if (utils.ignoreCaseEquals(type, 'TXT')) {
99-
type = 'TXT';
100-
} else if (utils.ignoreCaseEquals(type, 'NS')) {
101-
type = 'NS';
102-
} else if (utils.ignoreCaseEquals(type, 'SRV')) {
103-
type = 'SRV';
104-
} else if (utils.ignoreCaseEquals(type, 'NAPTR')) {
105-
type = 'NAPTR';
106-
} else if (utils.ignoreCaseEquals(type, 'PTR')) {
107-
type = 'PTR';
108-
} else if (utils.ignoreCaseEquals(type, 'AAAA')) {
109-
type = 'AAAA';
110-
} else if (utils.ignoreCaseEquals(type, 'SSHFP')) {
111-
type = 'SSHFP';
112-
} else if (utils.ignoreCaseEquals(type, 'HINFO')) {
113-
type = 'HINFO';
114-
} else if (utils.ignoreCaseEquals(type, 'POOL')) {
115-
type = 'POOL';
116-
} else {
117-
throw new Error($('Invalid record type. Valid types are: A, ALIAS, CNAME, MX, SPF, URL, TXT, NS, SRV, NAPTR, PTR, AAAA, SSHFP, HINFO, POOL'));
118-
}
123+
type = promptForAndNormalizeRecordType(type, _);
119124
content = cli.interaction.promptIfNotGiven($('Record Content: '), content, _);
120125

121126
var context = {
@@ -126,7 +131,7 @@ exports.init = function (cli) {
126131
record: {}
127132
};
128133

129-
var recordName = options.recordname || '';
134+
var recordName = recordname || '';
130135

131136
var r = {
132137
name: recordName,
@@ -195,6 +200,78 @@ exports.init = function (cli) {
195200

196201
},
197202

203+
domainRecords.updateCommand = function (recordid, recordname, type, content, name, options, _) {
204+
recordname = cli.interaction.promptIfNotGiven($('Record Name (Leave blank to create a record for the root domain.): '), recordname, _);
205+
type = promptForAndNormalizeRecordType(type, _);
206+
content = cli.interaction.promptIfNotGiven($('Record Content: '), content, _);
207+
208+
var context = {
209+
subscription: profile.current.getSubscription(options.subscription).id,
210+
domain: {
211+
name: name
212+
},
213+
record: {}
214+
};
215+
216+
var recordName = recordname || '';
217+
218+
var r = {
219+
id: recordid,
220+
name: recordName,
221+
record_type: type,
222+
content: content
223+
};
224+
225+
if (options.ttl) {
226+
r['ttl'] = options.ttl;
227+
}
228+
229+
if (options.priority) {
230+
r['prio'] = options.priority;
231+
}
232+
233+
context.record = r;
234+
domain.lookupDomainName(context, _);
235+
236+
var record = domain.doRecordUpdate(context, _);
237+
238+
var format = [
239+
[$('Id'), 'id'],
240+
[$('Name'), 'name'],
241+
[$('Type'), 'record_type'],
242+
[$('TTL'), 'ttl'],
243+
[$('Content'), 'content'],
244+
];
245+
246+
log.info('Successfully updated record:');
247+
log.report(format, record);
248+
249+
},
250+
251+
domainRecords.deleteCommand = function (recordid, name, options, _) {
252+
recordid = cli.interaction.promptIfNotGiven($('Record ID: '), recordid, _);
253+
254+
var context = {
255+
subscription: profile.current.getSubscription(options.subscription).id,
256+
domain: {
257+
name: name
258+
},
259+
record: {
260+
id: recordid
261+
}
262+
};
263+
264+
domain.lookupDomainName(context, _);
265+
266+
var shouldDelete = options.quiet || cli.interaction.confirm($('This will permanently remove the dns record and cannot be undone. Are you sure? '), _);
267+
if (!shouldDelete) {
268+
return;
269+
}
270+
271+
domain.doRecordDelete(context, _);
272+
273+
},
274+
198275
domainRecords.command('list [name]')
199276
.usage('[options] [name]')
200277
.description($('Show your domain dns records'))
@@ -203,20 +280,38 @@ exports.init = function (cli) {
203280
.option('-d --details', $('Show extra information about the records'))
204281
.execute(domainRecords.listCommand);
205282

283+
domainRecords.command('show [recordid] [name]')
284+
.usage('[options] <recordid> [name]')
285+
.description($('Show a dns record for a domain'))
286+
.option('-i --id <recordid>', $('The record id. Use dnsimple domain record list [domain] to see dns records and ids.'))
287+
.execute(domainRecords.showCommand);
288+
206289
domainRecords.command('add [recordname] [type] [content] [name]')
207290
.usage('[options] <recordname> <type> <content> [name]')
208-
.description($('Add a dns record to your domain'))
291+
.description($('Add a dns record for a domain'))
209292
.option('-r --recordname <recordname>', $('Record name. Use an empty string to create a record for the root domain.'))
210293
.option('-t --type <type>', $('Type of record to add (A, CNAME, MX, etc.)'))
211294
.option('-c --content <content>', $('Record content.'))
212295
.option('-ttl --ttl <ttl>', $('Record TTL.'))
213296
.option('-p --priority <priority>', $('Record Priority.'))
214297
.execute(domainRecords.addCommand);
215298

216-
domainRecords.command('show [recordid] [name]')
299+
domainRecords.command('update [recordid] [recordname] [type] [content] [name]')
300+
.usage('[options] <recordid> <recordname> <type> <content> [name]')
301+
.description($('Update a dns record for a domain'))
302+
.option('-i --id <recordid>', $('The record id. Use dnsimple domain record list [domain] to see dns records and ids.'))
303+
.option('-r --recordname <recordname>', $('Record name. Use an empty string to create a record for the root domain.'))
304+
.option('-t --type <type>', $('Type of record to add (A, CNAME, MX, etc.)'))
305+
.option('-c --content <content>', $('Record content.'))
306+
.option('-ttl --ttl <ttl>', $('Record TTL.'))
307+
.option('-p --priority <priority>', $('Record Priority.'))
308+
.execute(domainRecords.updateCommand);
309+
310+
domainRecords.command('delete [recordid] [name]')
217311
.usage('[options] <recordid> [name]')
218-
.description($('Show a dns record for a domain'))
312+
.description($('Delete a dns record for a domain'))
219313
.option('-i --id <recordid>', $('The record id. Use dnsimple domain record list [domain] to see dns records and ids.'))
220-
.execute(domainRecords.showCommand);
314+
.option('-q --quiet', $('quiet mode, do not ask for delete confirmation'))
315+
.execute(domainRecords.deleteCommand);
221316

222317
};

0 commit comments

Comments
 (0)