From 9730b5733a964d263346f582ced17fbdf4ca113d Mon Sep 17 00:00:00 2001 From: JUDIT GRESKOVITS Date: Thu, 6 Jul 2017 16:28:05 +0100 Subject: [PATCH] feat(cp-gql-mock-server): Implement transitional states --- packages/cp-gql-mock-server/src/resolvers.js | 105 ++++++++++++------- 1 file changed, 66 insertions(+), 39 deletions(-) diff --git a/packages/cp-gql-mock-server/src/resolvers.js b/packages/cp-gql-mock-server/src/resolvers.js index 09d76026..f877a315 100644 --- a/packages/cp-gql-mock-server/src/resolvers.js +++ b/packages/cp-gql-mock-server/src/resolvers.js @@ -183,71 +183,98 @@ const scale = options => { }; }; -const restartServices = options => { - const service = getServices({ id: options.ids[0] }); - return service; -}; - const updateInstancesStatus = (is, status) => { is.forEach(i => { const instance = instances.filter(instance => instance.id === i.id)[0]; instance.status = status; }); - return null; }; -const updateServiceStatus = (serviceId, status) => { - return Promise.all([getServices({ id: serviceId }), getServices({ parentId: serviceId })]) - .then(services => services.reduce((services, service) => - services.concat(service), [])) - .then(services => Promise.all( - services.reduce((instances, service) => - service.instances ? instances.concat(service.instances()) : instances, []))) - .then(instances => updateInstancesStatus(instances.reduce((is, i) => - is.concat(i), []), status)) - .then(() => Promise.all([ +const updateServiceStatus = (ss, status) => { + ss.forEach(s => { + const service = services.filter(service => service.id === s.id)[0]; + service.status = status; + }); + return null; +} + +const updateServiceAndInstancesStatus = (serviceId, serviceStatus, instancesStatus) => { + return Promise.all([ getServices({ id: serviceId }), getServices({ parentId: serviceId }) - ])) + ]) + .then(services => services.reduce((services, service) => + services.concat(service), [])) + .then(services => { + updateServiceStatus(services, serviceStatus); + return Promise.all( + services.reduce((instances, service) => + service.instances ? instances.concat(service.instances()) : instances, [])) + .then(instances => updateInstancesStatus(instances.reduce((is, i) => + is.concat(i), []), instancesStatus)) + }) + .then(() => Promise.all([ + getUnfilteredServices({ id: serviceId }), + getUnfilteredServices({ parentId: serviceId }) + ])) .then(services => services.reduce((services, service) => services.concat(service), [])); }; -const deleteServices = options => { - const serviceId = options.ids[0]; - const deleteService = getServices({ id: serviceId }).then(services => { - const service = services.shift(); - return service.instances().then(instances => { - if (instances.length) { - updateInstancesStatus(instances, 'DELETED'); - return [service]; - } +const handleStatusUpdateRequest = (serviceId, + transitionalServiceStatus, transitionalInstancesStatus, + serviceStatus, instancesStatus) => { + // this is what we need to delay + const timeout = setTimeout(() => { + updateServiceAndInstancesStatus(serviceId, + serviceStatus, instancesStatus); + }, 5000); + // this is what we'll need to return + return updateServiceAndInstancesStatus(serviceId, + transitionalServiceStatus, transitionalInstancesStatus); +} - return getUnfilteredServices({ parent: serviceId }).then(services => { - return Promise.all( - services.map(service => service.instances()) - ).then(instances => { - const is = instances.reduce((is, i) => is.concat(i), []); - updateInstancesStatus(is, 'DELETED'); - return [service]; - }); - }); - }); - }); +const deleteServices = options => { + // service transitional 'DELETING' + // instances transitional 'STOPPING' + // service 'DELETED' + // instances 'DELETED' + const deleteService = handleStatusUpdateRequest(options.ids[0], + 'DELETING', 'STOPPING', 'DELETED', 'DELETED'); return Promise.resolve(deleteService); }; const stopServices = options => { - const stopService = updateServiceStatus(options.ids[0], 'STOPPED'); + // service transitional 'STOPPING' + // instances transitional 'STOPPING' + // service 'STOPPED' + // instances 'STOPPED' + const stopService = handleStatusUpdateRequest(options.ids[0], + 'STOPPING', 'STOPPING', 'STOPPED', 'STOPPED'); return Promise.resolve(stopService); }; const startServices = options => { - const startService = updateServiceStatus(options.ids[0], 'RUNNING'); + // service transitional ... + // instances transitional ... + // service 'ACTIVE' + // instances 'RUNNING' + const startService = handleStatusUpdateRequest(options.ids[0], + 'PROVISIONING', 'PROVISIONING', 'ACTIVE', 'RUNNING'); return Promise.resolve(startService); }; +const restartServices = options => { + // service transitional 'RESTARTING' + // instances transitional 'STOPPING' + // service 'ACTIVE' + // instances 'RUNNING' + const restartService = handleStatusUpdateRequest(options.ids[0], + 'RESTARTING', 'STOPPING', 'ACTIVE', 'RUNNING'); + return Promise.resolve(restartService); +}; + module.exports = { portal: getPortal, deploymentGroups: getDeploymentGroups,