Fix setup and restore to have a task style API

This commit is contained in:
Girish Ramakrishnan
2018-12-15 15:27:16 -08:00
parent 1fd6c363ba
commit a961407379
10 changed files with 152 additions and 100 deletions
+3 -12
View File
@@ -11,9 +11,8 @@ exports = module.exports = {
checkForUpdates: checkForUpdates,
getLogs: getLogs,
getLogStream: getLogStream,
getStatus: getStatus,
setDashboardDomain: setDashboardDomain,
setDashboardDns: setDashboardDns,
prepareDashboardDomain: prepareDashboardDomain,
renewCerts: renewCerts
};
@@ -187,10 +186,10 @@ function setDashboardDomain(req, res, next) {
});
}
function setDashboardDns(req, res, next) {
function prepareDashboardDomain(req, res, next) {
if (!req.body.domain || typeof req.body.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));
cloudron.setDashboardDns(req.body.domain, auditSource(req), function (error, taskId) {
cloudron.prepareDashboardDomain(req.body.domain, auditSource(req), function (error, taskId) {
if (error && error.reason === CloudronError.BAD_FIELD) return next(new HttpError(404, error.message));
if (error) return next(new HttpError(500, error));
@@ -198,14 +197,6 @@ function setDashboardDns(req, res, next) {
});
}
function getStatus(req, res, next) {
cloudron.getStatus(function (error, status) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, status));
});
}
function renewCerts(req, res, next) {
cloudron.renewCerts({ domain: req.body.domain || null }, auditSource(req), function (error, taskId) {
if (error && error.reason === CloudronError.NOT_FOUND) return next(new HttpError(404, error.message));
+10 -1
View File
@@ -5,7 +5,8 @@ exports = module.exports = {
setupTokenAuth: setupTokenAuth,
setup: setup,
activate: activate,
restore: restore
restore: restore,
getStatus: getStatus
};
var assert = require('assert'),
@@ -156,3 +157,11 @@ function restore(req, res, next) {
next(new HttpSuccess(200));
});
}
function getStatus(req, res, next) {
provision.getStatus(function (error, status) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, status));
});
}
+16 -1
View File
@@ -48,7 +48,7 @@ function setup(done) {
function dnsSetup(callback) {
superagent.post(SERVER_URL + '/api/v1/cloudron/setup')
.send({ dnsConfig: { provider: ADMIN_DOMAIN.provider, domain: ADMIN_DOMAIN.domain, config: ADMIN_DOMAIN.config } })
.send({ dnsConfig: { provider: ADMIN_DOMAIN.provider, domain: ADMIN_DOMAIN.domain, config: ADMIN_DOMAIN.config, tlsConfig: { provider: 'fallback' } } })
.end(function (error, result) {
expect(result).to.be.ok();
expect(result.statusCode).to.eql(200);
@@ -57,6 +57,21 @@ function setup(done) {
});
},
function waitForSetup(done) {
async.retry({ times: 5, interval: 4000 }, function (retryCallback) {
superagent.get(SERVER_URL + '/api/v1/cloudron/status')
.end(function (error, result) {
if (!result || result.statusCode !== 200) return retryCallback(new Error('Bad result'));
console.dir(result.body);
if (!result.body.setup.active && result.body.setup.errorMessage === '' && result.body.adminFqdn) return retryCallback();
retryCallback(new Error('Not done yet: ' + JSON.stringify(result.body)));
});
}, done);
},
function createAdmin(callback) {
superagent.post(SERVER_URL + '/api/v1/cloudron/activate')
.query({ setupToken: 'somesetuptoken' })
+17 -4
View File
@@ -34,6 +34,19 @@ function cleanup(done) {
], done);
}
function waitForSetup(done) {
async.retry({ times: 5, interval: 4000 }, function (retryCallback) {
superagent.get(SERVER_URL + '/api/v1/cloudron/status')
.end(function (error, result) {
if (!result || result.statusCode !== 200) return retryCallback(new Error('Bad result'));
if (!result.body.setup.active && result.body.setup.errorMessage === '' && result.body.adminFqdn) return retryCallback();
retryCallback(new Error('Not done yet: ' + JSON.stringify(result.body)));
});
}, done);
}
describe('REST API', function () {
before(setup);
after(cleanup);
@@ -128,23 +141,23 @@ describe('REST API', function () {
it('dns setup succeeds', function (done) {
superagent.post(SERVER_URL + '/api/v1/cloudron/setup')
.send({ dnsConfig: { provider: 'noop', domain: DOMAIN, adminFqdn: 'my.' + DOMAIN, config: {} } })
.send({ dnsConfig: { provider: 'noop', domain: DOMAIN, adminFqdn: 'my.' + DOMAIN, config: {}, tlsConfig: { provider: 'fallback' } } })
.end(function (error, result) {
expect(result).to.be.ok();
expect(result.statusCode).to.eql(200);
done();
waitForSetup(done);
});
});
it('dns setup twice succeeds', function (done) {
superagent.post(SERVER_URL + '/api/v1/cloudron/setup')
.send({ dnsConfig: { provider: 'noop', domain: DOMAIN, DOMAIN, config: {} } })
.send({ dnsConfig: { provider: 'noop', domain: DOMAIN, DOMAIN, config: {} }, tlsConfig: { provider: 'fallback' } })
.end(function (error, result) {
expect(result).to.be.ok();
expect(result.statusCode).to.eql(200);
done();
waitForSetup(done);
});
});