2017-06-09 07:26:25 +03:00
|
|
|
const { v4: uuid } = require('uuid');
|
|
|
|
const paramCase = require('param-case');
|
|
|
|
const camelCase = require('camel-case');
|
|
|
|
const yaml = require('js-yaml');
|
|
|
|
|
2017-05-18 21:21:33 +03:00
|
|
|
const {
|
|
|
|
datacenter,
|
|
|
|
portal,
|
|
|
|
deploymentGroups,
|
|
|
|
services,
|
|
|
|
instances
|
|
|
|
} = require('./data.json');
|
|
|
|
|
|
|
|
const find = (query = {}) => item =>
|
|
|
|
Object.keys(query).every(key => item[key] === query[key]);
|
|
|
|
|
|
|
|
const cleanQuery = (q = {}) => JSON.parse(JSON.stringify(q));
|
|
|
|
|
2017-06-08 00:15:48 +03:00
|
|
|
const getInstances = query =>
|
|
|
|
Promise.resolve(instances.filter(find(cleanQuery(query))));
|
|
|
|
|
|
|
|
const getServices = query => {
|
|
|
|
const instancesResolver = ({ id }) => query =>
|
|
|
|
getInstances(
|
|
|
|
Object.assign({}, query, {
|
|
|
|
serviceId: id
|
2017-05-18 21:21:33 +03:00
|
|
|
})
|
2017-06-08 00:15:48 +03:00
|
|
|
);
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-08 00:15:48 +03:00
|
|
|
const addNestedResolvers = service =>
|
|
|
|
Object.assign({}, service, {
|
|
|
|
instances: instancesResolver(service)
|
|
|
|
});
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-08 00:15:48 +03:00
|
|
|
return Promise.resolve(
|
|
|
|
services.filter(find(cleanQuery(query))).map(addNestedResolvers)
|
|
|
|
);
|
|
|
|
};
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-08 00:15:48 +03:00
|
|
|
const getDeploymentGroups = query => {
|
|
|
|
const servicesResolver = ({ id }) => query =>
|
|
|
|
getServices(
|
|
|
|
Object.assign({}, query, {
|
|
|
|
deploymentGroupId: id
|
|
|
|
})
|
|
|
|
);
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-08 00:15:48 +03:00
|
|
|
const addNestedResolvers = dg =>
|
|
|
|
Object.assign({}, dg, {
|
|
|
|
services: servicesResolver(dg)
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve(
|
|
|
|
deploymentGroups.filter(find(cleanQuery(query))).map(addNestedResolvers)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPortal = () =>
|
|
|
|
Promise.resolve(
|
|
|
|
Object.assign({}, portal, {
|
|
|
|
datacenter,
|
|
|
|
deploymentGroups: getDeploymentGroups
|
|
|
|
})
|
|
|
|
);
|
2017-05-18 21:21:33 +03:00
|
|
|
|
2017-06-09 07:26:25 +03:00
|
|
|
const createDeploymentGroup = ({ name }) => {
|
|
|
|
const dg = {
|
|
|
|
id: uuid(),
|
|
|
|
slug: paramCase(name),
|
|
|
|
name
|
|
|
|
};
|
|
|
|
|
|
|
|
deploymentGroups.push(dg);
|
|
|
|
|
|
|
|
return Promise.resolve(dg);
|
|
|
|
};
|
|
|
|
|
|
|
|
const createServicesFromManifest = ({ deploymentGroupId, raw }) => {
|
|
|
|
const manifest = yaml.safeLoad(raw);
|
|
|
|
|
|
|
|
Object.keys(manifest).forEach(name => {
|
|
|
|
const service = {
|
|
|
|
id: uuid(),
|
|
|
|
deploymentGroup: deploymentGroupId,
|
|
|
|
slug: paramCase(name),
|
|
|
|
name
|
|
|
|
};
|
|
|
|
|
|
|
|
const instance = {
|
|
|
|
id: uuid(),
|
|
|
|
name: camelCase(`${service.slug}_01`),
|
|
|
|
service: service.id,
|
|
|
|
deploymentGroup: deploymentGroupId
|
|
|
|
};
|
|
|
|
|
|
|
|
services.push(service);
|
|
|
|
instances.push(instance);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve(undefined);
|
|
|
|
};
|
|
|
|
|
2017-06-19 15:10:57 +03:00
|
|
|
const deleteServices = options => {
|
|
|
|
const service = getServices({ id: options.ids[0] });
|
|
|
|
return service;
|
|
|
|
};
|
2017-06-16 17:12:28 +03:00
|
|
|
|
|
|
|
const scale = options => {
|
|
|
|
const service = getServices({ id: options.serviceId })[0];
|
2017-06-19 15:10:57 +03:00
|
|
|
|
2017-06-16 17:12:28 +03:00
|
|
|
return {
|
|
|
|
scale: [
|
|
|
|
{
|
|
|
|
id: service.id,
|
|
|
|
serviceName: service.name,
|
|
|
|
replicas: 2
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-06-19 15:10:57 +03:00
|
|
|
const restartServices = options => {
|
|
|
|
const service = getServices({ id: options.ids[0] });
|
|
|
|
return service;
|
|
|
|
};
|
|
|
|
|
|
|
|
const stopServices = options => {
|
|
|
|
const service = getServices({ id: options.ids[0] });
|
|
|
|
return service;
|
|
|
|
};
|
|
|
|
|
|
|
|
const startServices = options => {
|
|
|
|
const service = getServices({ id: options.ids[0] });
|
|
|
|
return service;
|
|
|
|
};
|
|
|
|
|
2017-05-18 21:21:33 +03:00
|
|
|
module.exports = {
|
2017-06-08 00:15:48 +03:00
|
|
|
portal: getPortal,
|
|
|
|
deploymentGroups: getDeploymentGroups,
|
2017-06-16 15:41:30 +03:00
|
|
|
deploymentGroup: query => getDeploymentGroups(query).then(dgs => dgs.shift()),
|
2017-06-08 00:15:48 +03:00
|
|
|
services: getServices,
|
|
|
|
service: query => getServices(query).then(services => services.shift()),
|
|
|
|
instances: getInstances,
|
2017-06-09 07:26:25 +03:00
|
|
|
instance: query => getInstances(query).then(instances => instances.shift()),
|
|
|
|
createDeploymentGroup,
|
|
|
|
provisionManifest: options =>
|
|
|
|
createServicesFromManifest(options).then(() => ({
|
|
|
|
id: uuid(),
|
|
|
|
type: options.type,
|
|
|
|
format: options.format
|
2017-06-16 17:12:28 +03:00
|
|
|
})),
|
|
|
|
deleteServices: (options, request, fn) => fn(null, deleteServices(options)),
|
2017-06-19 15:10:57 +03:00
|
|
|
scale: (options, reguest, fn) => fn(null, scale(options)),
|
|
|
|
restartServices: (options, request, fn) => fn(null, restartServices(options)),
|
|
|
|
stopServices: (options, request, fn) => fn(null, stopServices(options)),
|
|
|
|
startServices: (options, request, fn) => fn(null, startServices(options))
|
2017-05-18 21:21:33 +03:00
|
|
|
};
|