Files
cloudron-box/src/dns/caas.js
T

132 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-09-13 16:50:32 +02:00
/* jslint node:true */
'use strict';
exports = module.exports = {
2015-10-30 13:16:07 -07:00
add: add,
2015-10-30 13:30:19 -07:00
del: del,
2015-10-30 13:45:07 -07:00
update: update,
2015-10-29 16:41:04 -07:00
getChangeStatus: getChangeStatus,
2015-10-30 13:17:33 -07:00
get: get
2015-09-13 16:50:32 +02:00
};
var assert = require('assert'),
2015-10-28 16:02:06 -07:00
config = require('../config.js'),
debug = require('debug')('box:dns/caas'),
SubdomainError = require('../subdomains.js').SubdomainError,
2015-09-13 16:50:32 +02:00
superagent = require('superagent'),
2015-10-30 13:45:07 -07:00
util = require('util'),
_ = require('underscore');
2015-09-13 16:50:32 +02:00
2015-10-30 13:16:07 -07:00
function add(zoneName, subdomain, type, values, callback) {
2015-09-13 16:50:32 +02:00
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
2015-10-30 13:16:07 -07:00
assert(util.isArray(values));
2015-09-13 16:50:32 +02:00
assert.strictEqual(typeof callback, 'function');
var fqdn = subdomain !== '' && type === 'TXT' ? subdomain + '.' + config.fqdn() : config.appFqdn(subdomain);
2015-10-30 13:16:07 -07:00
debug('add: %s for zone %s of type %s with values %j', subdomain, zoneName, type, values);
2015-09-13 16:50:32 +02:00
2015-09-13 17:00:04 +02:00
var data = {
type: type,
2015-10-30 13:16:07 -07:00
values: values
2015-09-13 17:00:04 +02:00
};
2015-09-13 16:50:32 +02:00
superagent
.post(config.apiServerOrigin() + '/api/v1/domains/' + fqdn)
2015-09-13 16:50:32 +02:00
.query({ token: config.token() })
2015-09-13 17:00:04 +02:00
.send(data)
2015-09-13 16:50:32 +02:00
.end(function (error, result) {
if (error) return callback(error);
2015-09-15 18:27:09 -07:00
if (result.status === 420) return callback(new SubdomainError(SubdomainError.STILL_BUSY));
2015-09-13 16:50:32 +02:00
if (result.status !== 201) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('%s %j', result.status, result.body)));
return callback(null, result.body.changeId);
});
}
2015-10-30 13:17:33 -07:00
function get(zoneName, subdomain, type, callback) {
2015-10-29 14:15:54 -07:00
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
assert.strictEqual(typeof callback, 'function');
var fqdn = subdomain !== '' && type === 'TXT' ? subdomain + '.' + config.fqdn() : config.appFqdn(subdomain);
2015-10-30 13:30:19 -07:00
debug('get: zoneName: %s subdomain: %s type: %s fqdn: %s', zoneName, subdomain, type, fqdn);
2015-10-29 14:15:54 -07:00
superagent
.get(config.apiServerOrigin() + '/api/v1/domains/' + fqdn)
.query({ token: config.token(), type: type })
.end(function (error, result) {
if (error) return callback(error);
if (result.status !== 200) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('%s %j', result.status, result.body)));
return callback(null, result.body.values);
});
}
2015-10-30 13:45:07 -07:00
function update(zoneName, subdomain, type, values, callback) {
2015-10-29 14:15:54 -07:00
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
2015-10-30 13:45:07 -07:00
assert(util.isArray(values));
2015-10-29 14:15:54 -07:00
assert.strictEqual(typeof callback, 'function');
2015-10-30 13:45:07 -07:00
get(zoneName, subdomain, type, function (error, result) {
2015-10-29 16:26:56 -07:00
if (error) return callback(error);
2015-10-30 13:45:07 -07:00
if (_.isEqual(values, result)) return callback();
2015-10-29 14:15:54 -07:00
2015-10-30 13:45:07 -07:00
add(zoneName, subdomain, type, values, callback);
2015-10-29 14:15:54 -07:00
});
}
2015-10-30 13:30:19 -07:00
function del(zoneName, subdomain, type, values, callback) {
2015-09-13 16:50:32 +02:00
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
2015-10-30 13:30:19 -07:00
assert(util.isArray(values));
2015-09-13 16:50:32 +02:00
assert.strictEqual(typeof callback, 'function');
2015-10-30 13:30:19 -07:00
debug('add: %s for zone %s of type %s with values %j', subdomain, zoneName, type, values);
2015-09-13 16:50:32 +02:00
2015-09-13 18:10:45 +02:00
var data = {
type: type,
2015-10-30 13:30:19 -07:00
values: values
2015-09-13 18:10:45 +02:00
};
2015-09-13 16:50:32 +02:00
superagent
.del(config.apiServerOrigin() + '/api/v1/domains/' + config.appFqdn(subdomain))
.query({ token: config.token() })
2015-09-13 18:10:45 +02:00
.send(data)
2015-09-13 16:50:32 +02:00
.end(function (error, result) {
if (error) return callback(error);
2015-09-15 18:27:09 -07:00
if (result.status === 420) return callback(new SubdomainError(SubdomainError.STILL_BUSY));
if (result.status === 404) return callback(new SubdomainError(SubdomainError.NOT_FOUND));
2015-09-13 16:50:32 +02:00
if (result.status !== 204) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('%s %j', result.status, result.body)));
return callback(null);
});
}
function getChangeStatus(changeId, callback) {
assert.strictEqual(typeof changeId, 'string');
assert.strictEqual(typeof callback, 'function');
if (changeId === '') return callback(null, 'INSYNC');
superagent
.get(config.apiServerOrigin() + '/api/v1/domains/' + config.fqdn() + '/status/' + changeId)
.query({ token: config.token() })
.end(function (error, result) {
if (error) return callback(error);
if (result.status !== 200) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('%s %j', result.status, result.body)));
return callback(null, result.body.status);
});
}