2017-07-14 14:17:28 +03:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-13 20:22:00 +03:00
|
|
|
const FindIndex = require('lodash.findindex');
|
|
|
|
|
|
|
|
module.exports = ({ deploymentGroups = [], services = [], instances = [] }) => {
|
2017-07-14 14:17:28 +03:00
|
|
|
const getInstances = (serviceId) => {
|
|
|
|
return (opts, cb) => {
|
|
|
|
cb(null, instances
|
2017-08-28 23:31:38 +03:00
|
|
|
.filter((instance) => {
|
|
|
|
return instance.serviceId === serviceId;
|
|
|
|
})
|
|
|
|
);
|
2017-07-14 14:17:28 +03:00
|
|
|
};
|
2017-07-13 20:22:00 +03:00
|
|
|
};
|
|
|
|
|
2017-07-14 14:17:28 +03:00
|
|
|
const getServices = (deploymentGroupId) => {
|
|
|
|
return (opts, cb) => {
|
|
|
|
cb(null, services
|
2017-08-28 23:31:38 +03:00
|
|
|
.filter((service) => {
|
|
|
|
return service.deploymentGroupId === deploymentGroupId;
|
|
|
|
})
|
|
|
|
.map((service) => {
|
|
|
|
return Object.assign({}, service, {
|
|
|
|
instances: getInstances(service.id)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2017-07-14 14:17:28 +03:00
|
|
|
};
|
2017-07-13 20:22:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const getDeploymentGroups = (opts, cb) => {
|
|
|
|
cb(null, deploymentGroups
|
2017-07-14 14:17:28 +03:00
|
|
|
.map((dg) => {
|
2017-07-13 20:22:00 +03:00
|
|
|
return Object.assign({}, dg, {
|
|
|
|
services: getServices(dg.id)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPortal = (opts, cb) => {
|
|
|
|
cb(null, {
|
|
|
|
deploymentGroups: getDeploymentGroups
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateInstance = (instance, cb) => {
|
|
|
|
const instanceIndex = FindIndex(instances, ['id', instance.id]);
|
|
|
|
const updatedInstance = Object.assign(
|
|
|
|
{},
|
|
|
|
instances[instanceIndex],
|
|
|
|
instance
|
|
|
|
);
|
|
|
|
|
|
|
|
instances[instanceIndex] = updatedInstance;
|
|
|
|
return updatedInstance;
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateService = (service, cb) => {
|
|
|
|
const serviceIndex = FindIndex(service, ['id', service.id]);
|
|
|
|
const updatedService = Object.assign({}, services[serviceIndex], service);
|
|
|
|
|
2017-07-14 14:17:28 +03:00
|
|
|
services[serviceIndex] = updatedService;
|
|
|
|
return updatedService;
|
2017-07-13 20:22:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
getPortal,
|
|
|
|
updateInstance,
|
|
|
|
updateService
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|