From dd1124a608668aa9eb3f6e3300ccccc3e2958562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81rgio=20Ramos?= Date: Thu, 20 Jul 2017 11:57:33 +0100 Subject: [PATCH] feat(cp-gql-schema): scale mutation --- packages/cp-gql-mock-server/package.json | 3 + packages/cp-gql-mock-server/src/resolvers.js | 100 ++++++++++++++----- yarn.lock | 83 +++++++++------ 3 files changed, 133 insertions(+), 53 deletions(-) diff --git a/packages/cp-gql-mock-server/package.json b/packages/cp-gql-mock-server/package.json index 199b3f0c..24327fe8 100644 --- a/packages/cp-gql-mock-server/package.json +++ b/packages/cp-gql-mock-server/package.json @@ -14,6 +14,7 @@ "dev": "nodemon src/index.js" }, "dependencies": { + "build-array": "^1.0.0", "camel-case": "^3.0.0", "good": "^7.2.0", "good-console": "^6.4.0", @@ -24,7 +25,9 @@ "joyent-cp-gql-schema": "^1.0.4", "js-yaml": "^3.8.4", "lodash.find": "^4.6.0", + "lodash.findindex": "^4.6.0", "lodash.flatten": "^4.4.0", + "lodash.random": "^3.2.0", "lodash.uniq": "^4.5.0", "param-case": "^2.1.1", "uuid": "^3.1.0" diff --git a/packages/cp-gql-mock-server/src/resolvers.js b/packages/cp-gql-mock-server/src/resolvers.js index c2be7af1..abd4742e 100644 --- a/packages/cp-gql-mock-server/src/resolvers.js +++ b/packages/cp-gql-mock-server/src/resolvers.js @@ -1,8 +1,11 @@ const { v4: uuid } = require('uuid'); const paramCase = require('param-case'); const camelCase = require('camel-case'); +const buildArray = require('build-array'); const lfind = require('lodash.find'); +const findIndex = require('lodash.findindex'); const flatten = require('lodash.flatten'); +const random = require('lodash.random'); const uniq = require('lodash.uniq'); const yaml = require('js-yaml'); @@ -121,26 +124,34 @@ const createDeploymentGroup = ({ name }) => { const deleteDeploymentGroup = options => { const dgId = options.id; - const deleteDeploymentGroup = getServices({ deploymentGroupId: dgId}) - .then(services => Promise.all( - services.map(service => handleStatusUpdateRequest( - service.id, - 'DELETING', - 'STOPPING', - 'DELETED', - 'DELETED' - )) - )) + const deleteDeploymentGroup = getServices({ deploymentGroupId: dgId }) + .then(services => + Promise.all( + services.map(service => + handleStatusUpdateRequest( + service.id, + 'DELETING', + 'STOPPING', + 'DELETED', + 'DELETED' + ) + ) + ) + ) .then(() => { - const deploymentGroup = deploymentGroups.filter(dg => dg.id === dgId).shift(); + const deploymentGroup = deploymentGroups + .filter(dg => dg.id === dgId) + .shift(); deploymentGroup.status = 'DELETING'; return deploymentGroup; - return ({ deploymentGroupId: dgId }); - return getDeploymentGroups({id: dgId}); + return { deploymentGroupId: dgId }; + return getDeploymentGroups({ id: dgId }); }); const timeout = setTimeout(() => { - const deploymentGroup = deploymentGroups.filter(dg => dg.id === dgId).shift(); + const deploymentGroup = deploymentGroups + .filter(dg => dg.id === dgId) + .shift(); deploymentGroup.status = 'DELETED'; }, 5000); @@ -172,18 +183,56 @@ const createServicesFromManifest = ({ deploymentGroupId, raw }) => { return Promise.resolve(undefined); }; -const scale = options => { - const service = getServices({ id: options.serviceId })[0]; +const scale = ({ serviceId, replicas }) => { + const serviceIndex = findIndex(services, ['id', serviceId]); + const currentScale = instances.filter( + find({ + serviceId + }) + ).length; - return { - scale: [ - { - id: service.id, - serviceName: service.name, - replicas: 2 - } - ] + const version = { + id: uuid() }; + + if (currentScale === replicas) { + return version; + } + + services[serviceIndex].status = 'SCALING'; + + const up = n => { + buildArray(n).forEach((_, i) => { + instances.push({ + id: uuid(), + name: `${services[serviceIndex].slug}_${currentScale + i}`, + serviceId, + deploymentGroupId: services[serviceIndex].deploymentGroupId, + status: 'ACTIVE', + healthy: 'UNKNOWN' + }); + }); + }; + + const down = n => { + buildArray(n).forEach((_, i) => { + instances.splice(findIndex(instances, ['serviceId', serviceId]), 1); + }); + }; + + setTimeout(() => { + const diff = replicas - currentScale; + + if (diff >= 0) { + up(diff); + } else { + down(Math.abs(diff)); + } + + services[serviceIndex].status = 'ACTIVE'; + }, random(1500, 3000)); + + return version; }; const updateInstancesStatus = (is, status) => { @@ -336,7 +385,8 @@ module.exports = { type: options.type, format: options.format })), - deleteDeploymentGroup: (options, request, fn) => fn(null, deleteDeploymentGroup(options)), + deleteDeploymentGroup: (options, request, fn) => + fn(null, deleteDeploymentGroup(options)), deleteServices: (options, request, fn) => fn(null, deleteServices(options)), scale: (options, reguest, fn) => fn(null, scale(options)), restartServices: (options, request, fn) => fn(null, restartServices(options)), diff --git a/yarn.lock b/yarn.lock index 12aafab3..26b6f7c3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1766,12 +1766,12 @@ camelcase@^4.0.0, camelcase@^4.1.0: resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" caniuse-db@^1.0.30000187, caniuse-db@^1.0.30000634, caniuse-db@^1.0.30000639: - version "1.0.30000703" - resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000703.tgz#c7d899b8adea6da1aa587355d39ac8533f878403" + version "1.0.30000704" + resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000704.tgz#8c5aa6fed8058e65c70f2c1f5d63f7088650705c" caniuse-lite@^1.0.30000701: - version "1.0.30000703" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000703.tgz#666e8c3f1e4f7abb1d16d48e04e7e9e8df934925" + version "1.0.30000704" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000704.tgz#adb6ea01134515663682db93abab291d4c02946b" capture-stack-trace@^1.0.0: version "1.0.0" @@ -2199,8 +2199,8 @@ content-type@^1.0.2, content-type@~1.0.2: resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.2.tgz#b7d113aee7a8dd27bd21133c4dc2529df1721eed" content@3.x.x: - version "3.0.4" - resolved "https://registry.yarnpkg.com/content/-/content-3.0.4.tgz#ca3dde04480f12519b71526ec44bd488ddfb3fef" + version "3.0.5" + resolved "https://registry.yarnpkg.com/content/-/content-3.0.5.tgz#9e147dc7c838c4de9483096845ddb4de455ec509" dependencies: boom "5.x.x" @@ -3723,11 +3723,11 @@ extsprintf@1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550" -extsprintf@1.2.0, extsprintf@^1.2.0: +extsprintf@1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.2.0.tgz#5ad946c22f5b32ba7f8cd7426711c6e8a3fc2529" -extsprintf@1.3.0: +extsprintf@1.3.0, extsprintf@^1.2.0: version "1.3.0" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05" @@ -4303,8 +4303,8 @@ hapi-swagger@^7.7.0: swagger-parser "^3.4.1" hapi@^16.4.3: - version "16.4.3" - resolved "https://registry.yarnpkg.com/hapi/-/hapi-16.4.3.tgz#be4daaf2dcdbee97957ce503061b09765078aa05" + version "16.5.0" + resolved "https://registry.yarnpkg.com/hapi/-/hapi-16.5.0.tgz#89e4770f0034e3b69ee99ed929cc5b573805f303" dependencies: accept "2.x.x" ammo "2.x.x" @@ -4322,7 +4322,7 @@ hapi@^16.4.3: podium "1.x.x" shot "3.x.x" statehood "5.x.x" - subtext "4.x.x" + subtext "5.x.x" topo "2.x.x" har-schema@^1.0.5: @@ -5833,6 +5833,10 @@ lodash.pick@^4.4.0: version "4.4.0" resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3" +lodash.random@^3.2.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/lodash.random/-/lodash.random-3.2.0.tgz#96e24e763333199130d2c9e2fd57f91703cc262d" + lodash.remove@^4.7.0: version "4.7.0" resolved "https://registry.yarnpkg.com/lodash.remove/-/lodash.remove-4.7.0.tgz#f31d31e7c39a0690d5074ec0d3627162334ee626" @@ -5960,8 +5964,8 @@ markdown-escapes@^1.0.0: resolved "https://registry.yarnpkg.com/markdown-escapes/-/markdown-escapes-1.0.1.tgz#1994df2d3af4811de59a6714934c2b2292734518" markdown-table@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/markdown-table/-/markdown-table-1.1.0.tgz#1f5ae61659ced8808d882554c32e8b3f38dd1143" + version "1.1.1" + resolved "https://registry.yarnpkg.com/markdown-table/-/markdown-table-1.1.1.tgz#4b3dd3a133d1518b8ef0dbc709bf2a1b4824bc8c" matcher@^0.1.1: version "0.1.2" @@ -7275,8 +7279,8 @@ rc@1.1.7, rc@^1.0.1, rc@^1.1.6, rc@^1.1.7: strip-json-comments "~2.0.1" react-apollo@^1.4.2: - version "1.4.4" - resolved "https://registry.yarnpkg.com/react-apollo/-/react-apollo-1.4.4.tgz#31749237dfa133f47a688e0b7a91a479a7f0d8f3" + version "1.4.6" + resolved "https://registry.yarnpkg.com/react-apollo/-/react-apollo-1.4.6.tgz#b63002879680d0ebdcfb7623acf9df75a0e43bb7" dependencies: apollo-client "^1.4.0" graphql-anywhere "^3.0.0" @@ -8425,7 +8429,7 @@ squad@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/squad/-/squad-1.1.3.tgz#da09f68d1e0b0a60dca172878fe1f3c9e5272401" -sshpk-agent@1.4.2, sshpk-agent@^1.3.0: +sshpk-agent@1.4.2: version "1.4.2" resolved "https://registry.yarnpkg.com/sshpk-agent/-/sshpk-agent-1.4.2.tgz#5739cc08f48f98c53950a1715d20872a53804541" dependencies: @@ -8434,7 +8438,16 @@ sshpk-agent@1.4.2, sshpk-agent@^1.3.0: readable-stream "^2.1.4" sshpk ">=1.9.1 < 1.11.0" -sshpk@1.10.2, "sshpk@>=1.9.1 < 1.11.0", sshpk@^1.7.0, sshpk@^1.8.3: +sshpk-agent@^1.3.0: + version "1.6.0" + resolved "https://registry.yarnpkg.com/sshpk-agent/-/sshpk-agent-1.6.0.tgz#56da869e02ad757991c247327e80b261682399af" + dependencies: + assert-plus "^1.0.0" + mooremachine "^2.0.1" + readable-stream "^2.1.4" + sshpk ">=1.13.0 < 1.14.0" + +sshpk@1.10.2, "sshpk@>=1.9.1 < 1.11.0": version "1.10.2" resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.10.2.tgz#d5a804ce22695515638e798dbe23273de070a5fa" dependencies: @@ -8449,6 +8462,20 @@ sshpk@1.10.2, "sshpk@>=1.9.1 < 1.11.0", sshpk@^1.7.0, sshpk@^1.8.3: jsbn "~0.1.0" tweetnacl "~0.14.0" +"sshpk@>=1.13.0 < 1.14.0", sshpk@^1.7.0, sshpk@^1.8.3: + version "1.13.1" + resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.13.1.tgz#512df6da6287144316dc4c18fe1cf1d940739be3" + dependencies: + asn1 "~0.2.3" + assert-plus "^1.0.0" + dashdash "^1.12.0" + getpass "^0.1.1" + optionalDependencies: + bcrypt-pbkdf "^1.0.0" + ecc-jsbn "~0.1.1" + jsbn "~0.1.0" + tweetnacl "~0.14.0" + stack-utils@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/stack-utils/-/stack-utils-1.0.1.tgz#d4f33ab54e8e38778b0ca5cfd3b3afb12db68620" @@ -8462,8 +8489,8 @@ state-toggle@^1.0.0: resolved "https://registry.yarnpkg.com/state-toggle/-/state-toggle-1.0.0.tgz#d20f9a616bb4f0c3b98b91922d25b640aa2bc425" statehood@5.x.x: - version "5.0.2" - resolved "https://registry.yarnpkg.com/statehood/-/statehood-5.0.2.tgz#c6b3baa16ed8b121d3f09a3ffa85e22195a7f2a9" + version "5.0.3" + resolved "https://registry.yarnpkg.com/statehood/-/statehood-5.0.3.tgz#c07a75620db5379b60d2edd47f538002a8ac7dd6" dependencies: boom "5.x.x" cryptiles "3.x.x" @@ -8745,12 +8772,12 @@ stylelint@^7.0.0, stylelint@^7.0.3, stylelint@^7.11.1: table "^4.0.1" stylis@^3.2.1: - version "3.2.5" - resolved "https://registry.yarnpkg.com/stylis/-/stylis-3.2.5.tgz#132087969b2c9e2c71504eaed24d3f1987bb5d0f" + version "3.2.6" + resolved "https://registry.yarnpkg.com/stylis/-/stylis-3.2.6.tgz#6291df64aae62e9f537ab24b7d8a62b2428a34e9" -subtext@4.x.x: - version "4.4.1" - resolved "https://registry.yarnpkg.com/subtext/-/subtext-4.4.1.tgz#2fcec945de429283c3d18b151ff0fa1f1b87aec9" +subtext@5.x.x: + version "5.0.0" + resolved "https://registry.yarnpkg.com/subtext/-/subtext-5.0.0.tgz#9c3f083018bb1586b167ad8cfd87083f5ccdfe0f" dependencies: boom "5.x.x" content "3.x.x" @@ -8895,8 +8922,8 @@ tap-xunit@^1.7.0: xtend "~4.0.0" tapable@^0.2.5, tapable@~0.2.5: - version "0.2.6" - resolved "https://registry.yarnpkg.com/tapable/-/tapable-0.2.6.tgz#206be8e188860b514425375e6f1ae89bfb01fd8d" + version "0.2.7" + resolved "https://registry.yarnpkg.com/tapable/-/tapable-0.2.7.tgz#e46c0daacbb2b8a98b9b0cea0f4052105817ed5c" tar-fs@~1.12.0: version "1.12.0" @@ -9250,8 +9277,8 @@ typedarray@^0.0.6, typedarray@~0.0.5: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" ua-parser-js@^0.7.9: - version "0.7.13" - resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.13.tgz#cd9dd2f86493b3f44dbeeef3780fda74c5ee14be" + version "0.7.14" + resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.14.tgz#110d53fa4c3f326c121292bbeac904d2e03387ca" uglify-js@^2.6, uglify-js@^2.8.27: version "2.8.29"