diff --git a/src/cloudron.js b/src/cloudron.js index 5281bb854..8e0aa4acf 100644 --- a/src/cloudron.js +++ b/src/cloudron.js @@ -140,47 +140,23 @@ function getDisks(callback) { function getConfig(callback) { assert.strictEqual(typeof callback, 'function'); - // result to not depend on the appstore - const BOX_AND_USER_TEMPLATE = { - box: { - region: null, - size: null, - plan: 'Custom Plan' - }, - user: { - billing: false, - currency: '' - } - }; + settings.getCloudronName(function (error, cloudronName) { + if (error) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, error)); - caas.getBoxAndUserDetails(function (error, result) { - if (error) debug('Failed to fetch cloudron details.', error.reason, error.message); - - result = _.extend(BOX_AND_USER_TEMPLATE, result || {}); - - settings.getCloudronName(function (error, cloudronName) { - if (error) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, error)); - - callback(null, { - apiServerOrigin: config.apiServerOrigin(), - webServerOrigin: config.webServerOrigin(), - adminDomain: config.adminDomain(), - adminLocation: config.adminLocation(), - adminFqdn: config.adminFqdn(), - mailFqdn: config.mailFqdn(), - version: config.version(), - update: updateChecker.getUpdateInfo(), - progress: progress.getAll(), - isDemo: config.isDemo(), - region: result.box.region, - size: result.box.size, - billing: !!result.user.billing, - plan: result.box.plan, - currency: result.user.currency, - memory: os.totalmem(), - provider: config.provider(), - cloudronName: cloudronName - }); + callback(null, { + apiServerOrigin: config.apiServerOrigin(), + webServerOrigin: config.webServerOrigin(), + adminDomain: config.adminDomain(), + adminLocation: config.adminLocation(), + adminFqdn: config.adminFqdn(), + mailFqdn: config.mailFqdn(), + version: config.version(), + update: updateChecker.getUpdateInfo(), + progress: progress.getAll(), + isDemo: config.isDemo(), + memory: os.totalmem(), + provider: config.provider(), + cloudronName: cloudronName }); }); } diff --git a/src/routes/caas.js b/src/routes/caas.js index 3850994f8..7cac8d5a3 100644 --- a/src/routes/caas.js +++ b/src/routes/caas.js @@ -1,6 +1,7 @@ 'use strict'; exports = module.exports = { + getConfig: getConfig, changePlan: changePlan }; @@ -12,8 +13,25 @@ var caas = require('../caas.js'), HttpSuccess = require('connect-lastmile').HttpSuccess, _ = require('underscore'); +function getConfig(req, res, next) { + if (config.provider() !== 'caas') return next(new HttpError(422, 'Cannot use this API with this provider')); + + caas.getBoxAndUserDetails(function (error, result) { + if (error) return next(new HttpError(500, error)); + + // the result is { box: { region, size, plan }, user: { billing, currency } } + next(new HttpSuccess(200, { + region: result.box.region, + size: result.box.size, + billing: !!result.user.billing, + plan: result.box.plan, + currency: result.user.currency + })); + }); +} + function changePlan(req, res, next) { - if (config.provider() !== 'caas') return next(new HttpError(422, 'Cannot use migrate API with this provider')); + if (config.provider() !== 'caas') return next(new HttpError(422, 'Cannot use this API with this provider')); if ('size' in req.body && typeof req.body.size !== 'string') return next(new HttpError(400, 'size must be string')); if ('region' in req.body && typeof req.body.region !== 'string') return next(new HttpError(400, 'region must be string')); diff --git a/src/routes/test/caas-test.js b/src/routes/test/caas-test.js index 111e00d3d..1e5fc63f0 100644 --- a/src/routes/test/caas-test.js +++ b/src/routes/test/caas-test.js @@ -154,21 +154,12 @@ describe('Caas', function () { .get('/api/v1/boxes/BOX_ID?token=ACCESS_TOKEN2') .reply(200, { box: { region: 'sfo', size: '1gb' }, user: { }}); - superagent.get(SERVER_URL + '/api/v1/cloudron/config') + superagent.get(SERVER_URL + '/api/v1/caas/config') .query({ access_token: token }) .end(function (error, result) { expect(result.statusCode).to.equal(200); - expect(result.body.apiServerOrigin).to.eql('http://localhost:6060'); - expect(result.body.webServerOrigin).to.eql(null); - expect(result.body.adminFqdn).to.eql(config.adminFqdn()); - expect(result.body.progress).to.be.an('object'); - expect(result.body.update).to.be.an('object'); - expect(result.body.version).to.eql(config.version()); expect(result.body.size).to.eql('1gb'); expect(result.body.region).to.eql('sfo'); - expect(result.body.memory).to.eql(os.totalmem()); - expect(result.body.cloudronName).to.be.a('string'); - expect(result.body.provider).to.be.a('string'); expect(scope.isDone()).to.be.ok(); diff --git a/src/routes/test/cloudron-test.js b/src/routes/test/cloudron-test.js index 568790d01..47e11138c 100644 --- a/src/routes/test/cloudron-test.js +++ b/src/routes/test/cloudron-test.js @@ -194,8 +194,6 @@ describe('Cloudron', function () { expect(result.body.progress).to.be.an('object'); expect(result.body.update).to.be.an('object'); expect(result.body.version).to.eql(config.version()); - expect(result.body.size).to.eql(null); - expect(result.body.region).to.eql(null); expect(result.body.memory).to.eql(os.totalmem()); expect(result.body.cloudronName).to.be.a('string'); diff --git a/src/server.js b/src/server.js index bbc6f1250..989a03301 100644 --- a/src/server.js +++ b/src/server.js @@ -269,6 +269,7 @@ function initializeExpressSync() { router.del ('/api/v1/domains/:domain', domainsManageScope, routes.users.verifyPassword, routes.domains.del); // caas routes + router.get('/api/v1/caas/config', cloudronScope, routes.caas.getConfig); router.post('/api/v1/caas/change_plan', cloudronScope, routes.users.verifyPassword, routes.caas.changePlan); // disable server socket "idle" timeout. we use the timeout middleware to handle timeouts on a route level