feat(portal-data): expose version() in DeploymentGroup

This commit is contained in:
Sérgio Ramos 2017-06-27 16:15:27 +01:00 committed by Judit Greskovits
parent 05bc971a58
commit 3f1625aa5a
2 changed files with 69 additions and 39 deletions

View File

@ -254,12 +254,41 @@ module.exports = class Data extends EventEmitter {
}); });
} }
_getDeploymentGroupVersion (deploymentGroup) {
const getServices = (args) => {
args = args || {};
args.deploymentGroupId = deploymentGroup.id;
return new Promise((resolve, reject) => {
this.getServices(args, resolveCb(resolve, reject));
});
};
const getVersion = (args) => {
args = args || {};
args.id = deploymentGroup.version_id;
return new Promise((resolve, reject) => {
this.getVersion(args, resolveCb(resolve, reject));
});
};
return Object.assign(deploymentGroup, {
services: getServices,
version: getVersion
});
}
getDeploymentGroups ({ ids, name, slug }, cb) { getDeploymentGroups ({ ids, name, slug }, cb) {
const finish = (err, deploymentGroups) => { const finish = (err, deploymentGroups) => {
if (err) { if (err) {
return cb(err); return cb(err);
} }
if (!deploymentGroups || !deploymentGroups.length) {
return cb(null, []);
}
const getServices = (deploymentGroupId) => { const getServices = (deploymentGroupId) => {
return (args) => { return (args) => {
args = args || {}; args = args || {};
@ -271,14 +300,9 @@ module.exports = class Data extends EventEmitter {
}; };
}; };
// todo getVersion
// todo getHistory // todo getHistory
const convertedGroups = deploymentGroups ? deploymentGroups.map((deploymentGroup) => { cb(null, deploymentGroups.map((dg) => { return Transform.fromDeploymentGroup(this._getDeploymentGroupVersion(dg)); }));
return Transform.fromDeploymentGroup(deploymentGroup, getServices(deploymentGroup.id));
}) : [];
cb(null, convertedGroups);
}; };
if (ids) { if (ids) {
@ -308,30 +332,29 @@ module.exports = class Data extends EventEmitter {
return cb(null, {}); return cb(null, {});
} }
const deploymentGroup = deploymentGroups[0];
const getServices = (args) => {
args = args || {};
args.deploymentGroupId = deploymentGroup.id;
return new Promise((resolve, reject) => {
this.getServices(args, internals.resolveCb(resolve, reject));
});
};
// todo getVersion
// todo getHistory // todo getHistory
cb(err, Transform.fromDeploymentGroup(deploymentGroup, getServices)); cb(null, Transform.fromDeploymentGroup(this._getDeploymentGroupVersion(deploymentGroups[0])));
}); });
} }
_versionManifest (version) {
return Object.assign(version, {
manifest: (args) => {
return new Promise((resolve, reject) => {
return this.getManifest({
id: version.manifest_id
}, resolveCb(resolve, reject));
});
}
});
}
// versions // versions
createVersion (clientVersion, cb) { createVersion (clientVersion, cb) {
Hoek.assert(clientVersion, 'version is required'); Hoek.assert(clientVersion, 'version is required');
Hoek.assert(clientVersion.manifestId, 'manifestId is required'); Hoek.assert(clientVersion.manifest, 'manifest is required');
Hoek.assert(clientVersion.deploymentGroupId, 'deploymentGroupId is required'); Hoek.assert(clientVersion.deploymentGroupId, 'deploymentGroupId is required');
console.log(`-> creating new Version for DeploymentGroup ${clientVersion.deploymentGroupId}`); console.log(`-> creating new Version for DeploymentGroup ${clientVersion.deploymentGroupId}`);
@ -362,7 +385,7 @@ module.exports = class Data extends EventEmitter {
} }
version.id = key; version.id = key;
cb(null, Transform.fromVersion(version)); cb(null, Transform.fromVersion(this._versionManifest(version)));
}); });
}); });
} }
@ -373,7 +396,11 @@ module.exports = class Data extends EventEmitter {
return cb(err); return cb(err);
} }
cb(null, versions && versions.length ? Transform.fromVersion(versions[0]) : {}); if (!versions || !versions.length) {
return cb(null, null);
}
cb(null, Transform.fromVersion(this._versionManifest(versions[0])));
}); });
} }
@ -384,7 +411,11 @@ module.exports = class Data extends EventEmitter {
return cb(err); return cb(err);
} }
cb(null, Transform.fromVersion(version)); if (!version) {
return cb(null, null);
}
cb(null, Transform.fromVersion(this._versionManifest(version)));
}); });
} }
@ -395,7 +426,7 @@ module.exports = class Data extends EventEmitter {
} }
versions = versions || []; versions = versions || [];
cb(null, versions.map(Transform.fromVersion)); cb(null, versions.map((version) => { return Transform.fromVersion(this._versionManifest(version)); }));
}; };
// ensure the data is in sync // ensure the data is in sync
@ -498,7 +529,7 @@ module.exports = class Data extends EventEmitter {
const clientVersion = { const clientVersion = {
deploymentGroupId: deployment_group.id, deploymentGroupId: deployment_group.id,
manifestId: manifest.id, manifest,
plan: version.plan, plan: version.plan,
scale: version.service_scales.map((scale) => { scale: version.service_scales.map((scale) => {
if (scale.service_name !== service.name) { if (scale.service_name !== service.name) {
@ -557,7 +588,7 @@ module.exports = class Data extends EventEmitter {
console.log('-> provision request received'); console.log('-> provision request received');
const provision = ({ deploymentGroup, manifestId, newVersion }) => { const provision = ({ deploymentGroup, manifest, newVersion }) => {
let isHandled = false; let isHandled = false;
console.log(`-> requesting docker-compose provision for DeploymentGroup ${deploymentGroup.name}`); console.log(`-> requesting docker-compose provision for DeploymentGroup ${deploymentGroup.name}`);
@ -584,7 +615,6 @@ module.exports = class Data extends EventEmitter {
// return the new set of service ids // return the new set of service ids
this.provisionServices({ this.provisionServices({
deploymentGroup, deploymentGroup,
manifestId,
provisionRes provisionRes
}, (err, newServiceIds) => { }, (err, newServiceIds) => {
if (err) { if (err) {
@ -605,7 +635,7 @@ module.exports = class Data extends EventEmitter {
// create new version // create new version
this.updateVersion({ this.updateVersion({
id: newVersion.id, id: newVersion.id,
manifestId, manifest,
newServiceIds, newServiceIds,
plan: { plan: {
running: true, running: true,
@ -624,10 +654,10 @@ module.exports = class Data extends EventEmitter {
}); });
}; };
const createVersion = ({ deploymentGroup, currentVersion, manifestId }) => { const createVersion = ({ deploymentGroup, currentVersion, manifest }) => {
// create new version // create new version
this.createVersion({ this.createVersion({
manifestId, manifest,
deploymentGroupId: deploymentGroup.id, deploymentGroupId: deploymentGroup.id,
scale: currentVersion.scale, scale: currentVersion.scale,
plan: { plan: {
@ -643,7 +673,7 @@ module.exports = class Data extends EventEmitter {
console.log('newVersion', newVersion); console.log('newVersion', newVersion);
setImmediate(() => { setImmediate(() => {
provision({ deploymentGroup, manifestId, newVersion }); provision({ deploymentGroup, manifest, newVersion });
}); });
cb(null, newVersion); cb(null, newVersion);
@ -675,7 +705,7 @@ module.exports = class Data extends EventEmitter {
console.log(`-> detected first provision for DeploymentGroup ${deploymentGroup.id}`); console.log(`-> detected first provision for DeploymentGroup ${deploymentGroup.id}`);
return createVersion({ return createVersion({
deploymentGroup, deploymentGroup,
manifestId, manifest: { id: manifestId },
currentVersion: {} currentVersion: {}
}); });
} }
@ -692,7 +722,7 @@ module.exports = class Data extends EventEmitter {
return createVersion({ return createVersion({
deploymentGroup, deploymentGroup,
manifestId, manifest: { id: manifestId },
currentVersion currentVersion
}); });
}); });
@ -725,7 +755,7 @@ module.exports = class Data extends EventEmitter {
// services // services
provisionServices ({ deploymentGroup, manifestId, provisionRes }, cb) { provisionServices ({ deploymentGroup, provisionRes }, cb) {
// 1. get current set of services // 1. get current set of services
// 2. compare names and hashes // 2. compare names and hashes
// 3. if name doesn't exist anymore, disable service // 3. if name doesn't exist anymore, disable service

View File

@ -23,13 +23,13 @@ exports.toPortal = function (clientPortal) {
}; };
exports.fromDeploymentGroup = function (deploymentGroup, services) { exports.fromDeploymentGroup = function (deploymentGroup) {
return { return {
id: deploymentGroup.id, id: deploymentGroup.id,
name: deploymentGroup.name, name: deploymentGroup.name,
slug: deploymentGroup.slug, slug: deploymentGroup.slug,
services, services: deploymentGroup.services,
version: deploymentGroup.version_id, version: deploymentGroup.version,
history: deploymentGroup.history_version_ids || [] history: deploymentGroup.history_version_ids || []
}; };
}; };
@ -84,7 +84,7 @@ exports.toVersion = function (clientVersion) {
return { return {
id: clientVersion.id, id: clientVersion.id,
created: clientVersion.created || Date.now(), created: clientVersion.created || Date.now(),
manifest_id: clientVersion.manifestId, manifest_id: (clientVersion.manifest || {}).id,
service_scales: clientVersion.scale ? clientVersion.scale.map(exports.toScale) : [], service_scales: clientVersion.scale ? clientVersion.scale.map(exports.toScale) : [],
plan: exports.toPlan(clientVersion.plan || {}) plan: exports.toPlan(clientVersion.plan || {})
}; };
@ -94,7 +94,7 @@ exports.fromVersion = function (version) {
return { return {
id: version.id, id: version.id,
created: version.created, created: version.created,
manifestId: version.manifest_id, manifest: version.manifest,
scale: version.service_scales ? version.service_scales.map(exports.fromScale) : [], scale: version.service_scales ? version.service_scales.map(exports.fromScale) : [],
plan: exports.fromPlan(version.plan || {}) plan: exports.fromPlan(version.plan || {})
}; };