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

128 lines
5.3 KiB
JavaScript
Raw Normal View History

2015-09-13 16:50:32 +02:00
'use strict';
exports = module.exports = {
upsert: upsert,
2016-09-15 11:57:25 +02:00
get: get,
del: del,
waitForDns: require('./waitfordns.js'),
verifyDnsConfig: verifyDnsConfig
2015-09-13 16:50:32 +02:00
};
var assert = require('assert'),
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'),
util = require('util');
2015-09-13 16:50:32 +02:00
2015-11-08 23:14:39 -08:00
function add(dnsConfig, zoneName, subdomain, type, values, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
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-11-08 23:54:47 -08:00
.query({ token: dnsConfig.token })
2015-09-13 17:00:04 +02:00
.send(data)
.timeout(30 * 1000)
2015-09-13 16:50:32 +02:00
.end(function (error, result) {
if (error && !error.response) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('Network error %s', error.message)));
if (result.statusCode === 400) return callback(new SubdomainError(SubdomainError.BAD_FIELD, result.body.message));
if (result.statusCode === 420) return callback(new SubdomainError(SubdomainError.STILL_BUSY));
if (result.statusCode !== 201) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('%s %j', result.statusCode, result.body)));
2015-09-13 16:50:32 +02:00
return callback(null, result.body.changeId);
});
}
2015-11-08 23:14:39 -08:00
function get(dnsConfig, zoneName, subdomain, type, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
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)
2015-11-08 23:54:47 -08:00
.query({ token: dnsConfig.token, type: type })
.timeout(30 * 1000)
2015-10-29 14:15:54 -07:00
.end(function (error, result) {
if (error && !error.response) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('Network error %s', error.message)));
if (result.statusCode !== 200) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('%s %j', result.statusCode, result.body)));
2015-10-29 14:15:54 -07:00
return callback(null, result.body.values);
});
}
function upsert(dnsConfig, zoneName, subdomain, type, values, callback) {
2015-11-08 23:14:39 -08:00
assert.strictEqual(typeof dnsConfig, 'object');
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');
add(dnsConfig, zoneName, subdomain, type, values, callback);
2015-10-29 14:15:54 -07:00
}
2015-11-08 23:14:39 -08:00
function del(dnsConfig, zoneName, subdomain, type, values, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
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');
2016-09-04 18:00:28 -07:00
debug('del: %s for zone %s of type %s with values %j', subdomain, zoneName, type, values);
2015-09-13 16:50:32 +02:00
var data = {
type: type,
2015-10-30 13:30:19 -07:00
values: values
};
2015-09-13 16:50:32 +02:00
superagent
.del(config.apiServerOrigin() + '/api/v1/domains/' + config.appFqdn(subdomain))
2015-11-08 23:54:47 -08:00
.query({ token: dnsConfig.token })
.send(data)
.timeout(30 * 1000)
2015-09-13 16:50:32 +02:00
.end(function (error, result) {
if (error && !error.response) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('Network error %s', error.message)));
if (result.statusCode === 400) return callback(new SubdomainError(SubdomainError.BAD_FIELD, result.body.message));
if (result.statusCode === 420) return callback(new SubdomainError(SubdomainError.STILL_BUSY));
if (result.statusCode === 404) return callback(new SubdomainError(SubdomainError.NOT_FOUND));
if (result.statusCode !== 204) return callback(new SubdomainError(SubdomainError.EXTERNAL_ERROR, util.format('%s %j', result.statusCode, result.body)));
2015-09-13 16:50:32 +02:00
return callback(null);
});
}
function verifyDnsConfig(dnsConfig, domain, zoneName, ip, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof ip, 'string');
assert.strictEqual(typeof callback, 'function');
var credentials = {
provider: dnsConfig.provider
};
return callback(null, credentials);
}