Skip to content

Commit d8b95d2

Browse files
committed
Added domain record add functionality.
1 parent e769bb6 commit d8b95d2

6 files changed

Lines changed: 123 additions & 16 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ See the [command reference][3] for complete planned command structure.
2727
* Show DNS Records for a specific domain
2828
* Show DNS Records of a specific type (A, CNAME, TXT, NS, etc.) for a specific domain
2929
* Show DNS Records whose content matches a specific filter (e.g. \*spf\*)
30+
* Add DNS Records to a domain
3031

3132
---
3233
* Contacts: `(command: dnsimple contact)`

command-reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
|- delete
3939
--------------
4040
|- record
41-
|- list [-t --type <type>] [-f --filter <filter>]
42-
|- add
41+
|- list [-t --type <type>] [-f --filter <filter>] [domain]
42+
|- add [-r --recordname <recordname>] [-t --type <type>] [-c --content <content>] [domain]
4343
|- show
4444
|- update
4545
|- delete

lib/commands/domain/domain._js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ exports.init = function (cli) {
255255
return domainClient.getDomains(options, callback);
256256
};
257257

258+
domain.doRecordAdd = function (options, callback) {
259+
var domainClient = new DomainClient(cli, options.subscription);
260+
return domainClient.addRecord(options, callback);
261+
};
262+
258263
domain.doRecordsGet = function (options, callback) {
259264
var domainClient = new DomainClient(cli, options.subscription);
260265
return domainClient.getRecords(options, callback);

lib/commands/domain/domain.record._js

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ exports.init = function (cli) {
6666
row.cell($('Id'), item.id);
6767
//}
6868
row.cell($('Type'), item.record_type);
69-
row.cell($('Name'), item.name);
69+
row.cell($('Name'), item.name || context.domain.name);
7070
row.cell($('TTL'), item.ttl);
7171
row.cell($('Content'), item.content.length > 50 ? item.content.substr(0,50) + '...' : item.content);
7272
});
@@ -76,6 +76,90 @@ exports.init = function (cli) {
7676
});
7777
};
7878

79+
domainRecords.addCommand = function (recordname, type, content, name, options, _) {
80+
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+
}
119+
content = cli.interaction.promptIfNotGiven($('Record Content: '), content, _);
120+
121+
var context = {
122+
subscription: profile.current.getSubscription(options.subscription).id,
123+
domain: {
124+
name: name
125+
},
126+
record: {}
127+
};
128+
129+
var recordName = options.recordname || '';
130+
131+
var r = {
132+
name: recordName,
133+
record_type: type,
134+
content: content
135+
};
136+
137+
if (options.ttl) {
138+
r['ttl'] = options.ttl;
139+
}
140+
141+
if (options.priority) {
142+
r['prio'] = options.priority;
143+
}
144+
145+
context.record = r;
146+
domain.lookupDomainName(context, _);
147+
148+
var record = domain.doRecordAdd(context, _);
149+
150+
var format = [
151+
[$('Id'), 'id'],
152+
[$('Name'), 'name'],
153+
[$('Type'), 'record_type'],
154+
[$('TTL'), 'ttl'],
155+
[$('Content'), 'content'],
156+
];
157+
158+
log.info('Successfully added record:');
159+
log.report(format, record);
160+
161+
},
162+
79163
domainRecords.command('list [name]')
80164
.usage('[options] [name]')
81165
.description($('Show your domain dns records'))
@@ -84,4 +168,14 @@ exports.init = function (cli) {
84168
.option('-d --details', $('Show extra information about the records'))
85169
.execute(domainRecords.listCommand);
86170

171+
domainRecords.command('add [recordname] [type] [content] [name]')
172+
.usage('[options] <recordname> <type> <content> [name]')
173+
.description($('Add a dns record to your domain'))
174+
.option('-r --recordname <recordname>', $('Record name. Use an empty string to create a record for the root domain.'))
175+
.option('-t --type <type>', $('Type of record to add (A, CNAME, MX, etc.)'))
176+
.option('-c --content <content>', $('Record content.'))
177+
.option('-ttl --ttl <ttl>', $('Record TTL.'))
178+
.option('-p --priority <priority>', $('Record Priority.'))
179+
.execute(domainRecords.addCommand);
180+
87181
};

lib/commands/domain/domains/domainclient._js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,25 @@ __.extend(DomainClient.prototype, {
116116
}
117117
},
118118

119+
addRecord: function (context, _) {
120+
var self = this;
121+
var progress;
122+
123+
var dns = self.createDnsimpleClient(context.subscription).create(_);
124+
var domainName = context.domain.name;
125+
var r = context.record;
126+
127+
progress = self.cli.interaction.progress(util.format($('Adding record to domain %s'), domainName));
128+
try {
129+
var record = dns.dns.add(domainName, r, _);
130+
131+
return record;
132+
}
133+
finally {
134+
progress.end();
135+
}
136+
},
137+
119138
getRecords: function (context, _) {
120139
var self = this;
121140
var progress;
@@ -127,18 +146,6 @@ __.extend(DomainClient.prototype, {
127146
try {
128147
var records = dns.dns.list(domainName, _);
129148

130-
// self.cli.output.json('verbose', domains);
131-
//cacheUtils.saveDomains(context.subscription, domains, _);
132-
// return domains.sort(function(a, b){
133-
// var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase();
134-
// if (nameA < nameB) { //sort string ascending
135-
// return -1;
136-
// }
137-
// if (nameA > nameB) {
138-
// return 1;
139-
// }
140-
// return 0; //default return value (no sorting)
141-
// });
142149
return records;
143150
}
144151
finally {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"contributors": [
55
"Anderly, Adam <adam@anderly.com>"
66
],
7-
"version": "0.2.2",
7+
"version": "0.2.3",
88
"description": "DNSimple Command Line tool",
99
"tags": [
1010
"dnsimple",

0 commit comments

Comments
 (0)