From 9296d51075e0127a5e1db648b2d914af1d5a6124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81rgio=20Ramos?= Date: Mon, 17 Jul 2017 15:19:31 +0100 Subject: [PATCH] feat(portal-api): support deleteDeploymentGroup --- packages/portal-api/lib/data/index.js | 94 ++++++++++++++++++++++- packages/portal-api/lib/data/transform.js | 7 +- packages/portal-api/lib/resolvers.js | 1 + packages/portal-api/lib/watch/machines.js | 11 ++- 4 files changed, 108 insertions(+), 5 deletions(-) diff --git a/packages/portal-api/lib/data/index.js b/packages/portal-api/lib/data/index.js index aad9d650..cf345ad5 100644 --- a/packages/portal-api/lib/data/index.js +++ b/packages/portal-api/lib/data/index.js @@ -391,6 +391,92 @@ class Data extends EventEmitter { }); } + deleteDeploymentGroup ({ id }, cb) { + // dg, services, instances, versions, manifests + + const remove = (err, result) => { + if (err) { + return cb(err); + } + + const res = ForceArray(result.successes).reduce((acc, res) => { + return Object.assign(acc, res); + }, {}); + + VAsync.parallel({ + funcs: [ + (cb) => { + this._db.deployment_groups.remove({ id }, cb); + }, + (cb) => { + VAsync.forEachParallel({ + inputs: res.services, + func: ({ id }, next) => { + this._db.services.remove({ id }, next); + } + }); + }, + (cb) => { + VAsync.forEachParallel({ + inputs: res.instances, + func: ({ id }, next) => { + this._db.instances.remove({ id }, next); + } + }); + }, + (cb) => { + VAsync.forEachParallel({ + inputs: res.versions, + func: ({ id }, next) => { + this._db.versions.remove({ id }, next); + } + }); + }, + (cb) => { + VAsync.forEachParallel({ + inputs: res.manifests, + func: ({ id }, next) => { + this._db.manifests.remove({ id }, next); + } + }); + } + ] + }, (err) => { + cb(err, res.cb); + }); + }; + + VAsync.parallel({ + funcs: [ + (cb) => { + this.getDeploymentGroup({ id }, (err, dg) => { + cb(err, { dg }); + }); + }, + (cb) => { + this.getServices({ deploymentGroupId: id }, (err, services) => { + cb(err, { services }); + }); + }, + (cb) => { + this.getInstances({ deploymentGroupId: id }, (err, instances) => { + cb(err, { instances }); + }); + }, + (cb) => { + this.getVersions({ deploymentGroupId: id }, (err, versions) => { + cb(err, { versions }); + }); + }, + (cb) => { + this.getManifests({ deploymentGroupId: id }, (err, manifests) => { + cb(err, { manifests }); + }); + } + ] + }, remove); + } + // versions _versionFns (version) { @@ -1235,7 +1321,9 @@ class Data extends EventEmitter { ctx.config = config; - this.createManifest(clientManifest, handleNewManifest); + this.createManifest(Object.assign(clientManifest, { + deploymentGroupId: ctx.currentDeploymentGroup.id + }), handleNewManifest); }; // 1. check if deployment group exists @@ -2201,7 +2289,9 @@ class Data extends EventEmitter { VAsync.forEachParallel({ inputs: service.instances, func: (instance, next) => { - return this.createInstance(instance, next); + return this.createInstance(Object.assign(instance, { + deploymentGroupId + }), next); } }, (err, results) => { if (err) { diff --git a/packages/portal-api/lib/data/transform.js b/packages/portal-api/lib/data/transform.js index 0ab98409..6c612dc8 100644 --- a/packages/portal-api/lib/data/transform.js +++ b/packages/portal-api/lib/data/transform.js @@ -80,7 +80,6 @@ exports.fromService = function ({ service, instances, packages }) { }; exports.toService = function (clientService) { - // wat?? return clean({ id: clientService.id, version_hash: clientService.hash, @@ -106,6 +105,7 @@ exports.toVersion = function (clientVersion) { id: clientVersion.id, created: clientVersion.created || Date.now(), manifest_id: (clientVersion.manifest || {}).id, + deployment_group_id: clientVersion.deploymentGroupId, service_scales: clientVersion.scale ? clientVersion.scale : undefined, plan: clientVersion.plan ? clientVersion.plan : undefined, error: clientVersion.version @@ -116,6 +116,7 @@ exports.fromVersion = function (version) { return { id: version.id, created: version.created, + deploymentGroupId: version.deployment_group_id, manifest: version.manifest, scale: version.service_scales, plan: version.plan, @@ -171,6 +172,8 @@ exports.fromInstance = function (instance) { id: instance.id, name: instance.name, machineId: instance.machine_id, + serviceId: instance.service_id, + deploymentGroupId: instance.deployment_group_id, status: instance.status, healthy: instance.healthy, watchers: instance.watchers, @@ -184,6 +187,8 @@ exports.toInstance = function (clientInstance) { id: clientInstance.id, name: clientInstance.name, machine_id: clientInstance.machineId, + deployment_group_id: clientInstance.deploymentGroupId, + service_id: clientInstance.serviceId, status: clientInstance.status, healthy: clientInstance.healthy, watchers: clientInstance.watchers, diff --git a/packages/portal-api/lib/resolvers.js b/packages/portal-api/lib/resolvers.js index 7f1f47d0..fa309847 100644 --- a/packages/portal-api/lib/resolvers.js +++ b/packages/portal-api/lib/resolvers.js @@ -33,6 +33,7 @@ module.exports = (data) => { const mutations = [ 'createDeploymentGroup', 'updateDeploymentGroup', + 'deleteDeploymentGroup', 'provisionManifest', 'scale', 'stopServices', diff --git a/packages/portal-api/lib/watch/machines.js b/packages/portal-api/lib/watch/machines.js index e10b8587..7b7b2cbd 100644 --- a/packages/portal-api/lib/watch/machines.js +++ b/packages/portal-api/lib/watch/machines.js @@ -155,7 +155,7 @@ module.exports = class MachineWatcher { }); } - createInstance ({ machine, instances, service }, cb) { + createInstance ({ deploymentGroup, machine, instances, service }, cb) { console.error(`-> detected that machine ${machine.name} was created`); const status = (machine.state || '').toUpperCase(); @@ -167,6 +167,7 @@ module.exports = class MachineWatcher { const instance = { name: machine.name, status, + deploymentGroupId: deploymentGroup.id, machineId: machine.id }; @@ -523,7 +524,13 @@ module.exports = class MachineWatcher { this.createInstance : this.updateInstance; - createOrUpdateInstance.call(this, { machine, instances, instance, service }, handleCreateOrUpdatedInstance); + createOrUpdateInstance.call(this, { + deploymentGroup, + machine, + instances, + instance, + service + }, handleCreateOrUpdatedInstance); } onChange (machine) {