Return port bindings as part of app object

This commit is contained in:
Girish Ramakrishnan
2014-08-22 23:55:50 -07:00
parent 119917529f
commit 72c06d5774
2 changed files with 37 additions and 16 deletions
+30 -11
View File
@@ -39,17 +39,38 @@ exports = module.exports = {
RSTATE_NOT_RESPONDING: 'not_responding'
};
function postProcess(result) {
assert(result.manifestJson === null || typeof result.manifestJson === 'string');
result.manifest = safe.JSON.parse(result.manifestJson);
delete result.manifestJson;
assert(result.hostPorts === null || typeof result.hostPorts === 'string');
assert(result.containerPorts === null || typeof result.containerPorts === 'string');
result.portBindings = [ ];
var hostPorts = result.hostPorts === null ? [ ] : result.hostPorts.split(',');
var containerPorts = result.containerPorts === null ? [ ] : result.containerPorts.split(',');
delete result.hostPorts;
delete result.containerPorts;
for (var i = 0; i < hostPorts.length; i++) {
result.portBindings.push({ hostPort: hostPorts[i], containerPort: containerPorts[i] });
}
}
function get(id, callback) {
assert(typeof id === 'string');
assert(typeof callback === 'function');
database.get('SELECT * FROM apps WHERE id = ?', [ id ], function (error, result) {
database.get('SELECT apps.*, GROUP_CONCAT(appPortBindings.hostPort) AS hostPorts, GROUP_CONCAT(appPortBindings.containerPort) AS containerPorts'
+ ' FROM apps LEFT OUTER JOIN appPortBindings WHERE apps.id = ? GROUP BY appPortBindings.appId', [ id ], function (error, result) {
if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error));
if (typeof result === 'undefined') return callback(new DatabaseError(DatabaseError.NOT_FOUND));
result.manifest = safe.JSON.parse(result.manifestJson);
delete result.manifestJson;
postProcess(result);
callback(null, result);
});
@@ -59,28 +80,26 @@ function getBySubdomain(subdomain, callback) {
assert(typeof subdomain === 'string');
assert(typeof callback === 'function');
database.get('SELECT * FROM apps WHERE location = ?', [ subdomain ], function (error, result) {
database.get('SELECT apps.*, GROUP_CONCAT(appPortBindings.hostPort) AS hostPorts, GROUP_CONCAT(appPortBindings.containerPort) AS containerPorts'
+ ' FROM apps LEFT OUTER JOIN appPortBindings WHERE location = ? GROUP BY appPortBindings.appId', [ subdomain ], function (error, result) {
if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error));
if (typeof result === 'undefined') return callback(new DatabaseError(DatabaseError.NOT_FOUND));
result.manifest = safe.JSON.parse(result.manifestJson);
delete result.manifestJson;
postProcess(result);
callback(null, result);
});
}
function getAll(callback) {
database.all('SELECT * FROM apps', function (error, results) {
database.all('SELECT apps.*, GROUP_CONCAT(appPortBindings.hostPort) AS hostPorts, GROUP_CONCAT(appPortBindings.containerPort) AS containerPorts'
+ ' FROM apps LEFT OUTER JOIN appPortBindings ON apps.id = appPortBindings.appId GROUP BY appPortBindings.appId', function (error, results) {
if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error));
if (typeof results === 'undefined') results = [ ];
results.forEach(function (result) {
result.manifest = safe.JSON.parse(result.manifestJson);
delete result.manifestJson;
});
results.forEach(postProcess);
callback(null, results);
});
+7 -5
View File
@@ -224,7 +224,8 @@ describe('database', function () {
location: 'some-location-0',
manifest: null,
httpPort: null,
containerId: null
containerId: null,
portBindings: [ { containerPort: 1234, hostPort: 5678 } ]
};
var APP_1 = {
id: 'appid-1',
@@ -233,7 +234,8 @@ describe('database', function () {
location: 'some-location-1',
manifest: null,
httpPort: null,
containerId: null
containerId: null,
portBindings: [ ]
};
it('add fails due to missing arguments', function () {
@@ -242,7 +244,7 @@ describe('database', function () {
});
it('add succeeds', function (done) {
appdb.add(APP_0.id, APP_0.installationState, APP_0.location, [ { containerPort: 1234, hostPort: 5678 } ], function (error) {
appdb.add(APP_0.id, APP_0.installationState, APP_0.location, APP_0.portBindings, function (error) {
expect(error).to.be(null);
done();
});
@@ -319,8 +321,8 @@ describe('database', function () {
expect(error).to.be(null);
expect(result).to.be.an(Array);
expect(result.length).to.be(2);
expect(result[0]).to.be.eql(APP_0);
expect(result[1]).to.be.eql(APP_1);
expect(result[1]).to.be.eql(APP_0);
expect(result[0]).to.be.eql(APP_1);
done();
});
});