2015-08-26 16:14:51 -07:00
|
|
|
/* jslint node:true */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2015-10-28 16:02:06 -07:00
|
|
|
caas = require('./dns/caas.js'),
|
2015-08-26 16:14:51 -07:00
|
|
|
config = require('./config.js'),
|
2015-10-30 11:43:35 -07:00
|
|
|
route53 = require('./dns/route53.js'),
|
2015-10-30 13:16:07 -07:00
|
|
|
SubdomainError = require('./subdomainerror.js'),
|
|
|
|
|
util = require('util');
|
2015-08-26 16:14:51 -07:00
|
|
|
|
|
|
|
|
module.exports = exports = {
|
|
|
|
|
add: add,
|
|
|
|
|
remove: remove,
|
2015-10-29 14:16:09 -07:00
|
|
|
status: status,
|
2015-10-30 13:47:10 -07:00
|
|
|
update: update, // unlike add, this fetches latest value, compares and adds if necessary. atomicity depends on backend
|
2015-10-29 16:41:04 -07:00
|
|
|
get: get
|
2015-08-26 16:14:51 -07:00
|
|
|
};
|
|
|
|
|
|
2015-10-28 16:02:06 -07:00
|
|
|
// choose which subdomain backend we use for test purpose we use route53
|
2015-09-13 21:57:06 +02:00
|
|
|
function api() {
|
2015-10-28 16:02:06 -07:00
|
|
|
return config.isCustomDomain() || config.TEST ? route53 : caas;
|
2015-09-13 21:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
2015-10-30 13:16:07 -07:00
|
|
|
function add(subdomain, type, values, callback) {
|
|
|
|
|
assert.strictEqual(typeof subdomain, 'string');
|
|
|
|
|
assert.strictEqual(typeof type, 'string');
|
|
|
|
|
assert(util.isArray(values));
|
2015-08-26 16:14:51 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-10-30 13:16:07 -07:00
|
|
|
api().add(config.zoneName(), subdomain, type, values, function (error, changeId) {
|
2015-08-26 16:14:51 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
callback(null, changeId);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 16:41:04 -07:00
|
|
|
function get(subdomain, type, callback) {
|
|
|
|
|
assert.strictEqual(typeof subdomain, 'string');
|
|
|
|
|
assert.strictEqual(typeof type, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-10-30 13:32:37 -07:00
|
|
|
api().get(config.zoneName(), subdomain, type, function (error, values) {
|
2015-10-29 16:41:04 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
callback(null, values);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 13:45:07 -07:00
|
|
|
function update(subdomain, type, values, callback) {
|
|
|
|
|
assert.strictEqual(typeof subdomain, 'string');
|
|
|
|
|
assert.strictEqual(typeof type, 'string');
|
|
|
|
|
assert(util.isArray(values));
|
2015-10-29 14:16:09 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-10-30 13:45:07 -07:00
|
|
|
api().update(config.zoneName(), subdomain, type, values, function (error) {
|
2015-10-29 14:16:09 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 13:30:19 -07:00
|
|
|
function remove(subdomain, type, values, callback) {
|
|
|
|
|
assert.strictEqual(typeof subdomain, 'string');
|
|
|
|
|
assert.strictEqual(typeof type, 'string');
|
|
|
|
|
assert(util.isArray(values));
|
2015-10-30 13:45:07 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
2015-08-26 16:14:51 -07:00
|
|
|
|
2015-10-30 13:30:19 -07:00
|
|
|
api().del(config.zoneName(), subdomain, type, values, function (error) {
|
2015-08-26 16:14:51 -07:00
|
|
|
if (error && error.reason !== SubdomainError.NOT_FOUND) return callback(error);
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function status(changeId, callback) {
|
|
|
|
|
assert.strictEqual(typeof changeId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-09-13 21:57:06 +02:00
|
|
|
api().getChangeStatus(changeId, function (error, status) {
|
2015-08-26 16:14:51 -07:00
|
|
|
if (error) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, error));
|
|
|
|
|
callback(null, status === 'INSYNC' ? 'done' : 'pending');
|
|
|
|
|
});
|
|
|
|
|
}
|