From 2b552360e0ab2fa5068b1590cd02ce5da19290d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81rgio=20Ramos?= Date: Thu, 3 Aug 2017 13:41:14 +0100 Subject: [PATCH] fix(portal-api): update import logic to assign dg.services --- packages/portal-api/lib/data/index.js | 103 +++++++++++++++++--------- 1 file changed, 68 insertions(+), 35 deletions(-) diff --git a/packages/portal-api/lib/data/index.js b/packages/portal-api/lib/data/index.js index 4484aa8f..ba2ca9b5 100644 --- a/packages/portal-api/lib/data/index.js +++ b/packages/portal-api/lib/data/index.js @@ -2038,7 +2038,7 @@ class Data extends EventEmitter { this._triton.stopMachine(instance.machine_id, next); }, inputs: instances - }, (err, results) => { + }, (err) => { if (err) { return cb(err); } @@ -2113,7 +2113,7 @@ class Data extends EventEmitter { this._triton.rebootMachine(instance.machine_id, next); }, inputs: instances - }, (err, results) => { + }, (err) => { if (err) { return cb(err); } @@ -2338,34 +2338,78 @@ class Data extends EventEmitter { }); }, {}); + const handleNewInstances = ({ id }, next) => { + return (err, result) => { + if (err) { + return next(err); + } + + this._server.log(['debug'], `-> created Instances ${Util.inspect(result.successes)}`); + + this.updateService({ + id, + instances: result.successes + }, next); + }; + }; + + const handleNewService = ({ instances, deploymentGroupId }, next) => { + return (err, service) => { + if (err) { + return next(err); + } + + VAsync.forEachParallel({ + inputs: instances, + func: (instance, next) => { + return this.createInstance(Object.assign(instance, { + deploymentGroupId, + serviceId: service.id + }), next); + } + }, handleNewInstances(service, next)); + }; + }; + const createService = (deploymentGroupId) => { - return (serviceId, next) => { - const service = services[serviceId]; + return (serviceIndex, next) => { + const service = services[serviceIndex]; this._server.log(['debug'], `-> creating Service ${Util.inspect(service)}`); - VAsync.forEachParallel({ - inputs: service.instances, - func: (instance, next) => { - return this.createInstance(Object.assign(instance, { - deploymentGroupId - }), next); - } - }, (err, results) => { - if (err) { - return cb(err); - } - - this._server.log(['debug'], `-> created Instances ${Util.inspect(results.successes)}`); - - this.createService(Object.assign(service, { - instances: results.successes, - deploymentGroupId - }), next); - }); + this.createService(Object.assign(service, { + deploymentGroupId + }), handleNewService({ + instances: service.instances, + deploymentGroupId + }, next)); }; }; + const handleNewServices = (deploymentGroupId) => { + return (err, result) => { + if (err) { + return cb(err); + } + + this.updateDeploymentGroup({ + id: deploymentGroupId, + services: result.successes + }, cb); + }; + }; + + const handleNewDeploymentGroup = (err, dg) => { + if (err) { + return cb(err); + } + + VAsync.forEachParallel({ + inputs: Object.keys(services), + func: createService(dg.id) + }, handleNewServices(dg.id)); + }; + const deploymentGroup = { name: tags[DEPLOYMENT_GROUP], slug: ParamCase(tags[DEPLOYMENT_GROUP]), @@ -2375,18 +2419,7 @@ class Data extends EventEmitter { this._server.log(['debug'], `-> creating DeploymentGroup ${Util.inspect(deploymentGroup)}`); - this.createDeploymentGroup(deploymentGroup, (err, dg) => { - if (err) { - return cb(err); - } - - VAsync.forEachParallel({ - inputs: Object.keys(services), - func: createService(dg.id) - }, (err) => { - return cb(err, dg); - }); - }); + this.createDeploymentGroup(deploymentGroup, handleNewDeploymentGroup); } }