joyent-portal/packages/portal-api/lib/data/transform.js

220 lines
5.8 KiB
JavaScript
Raw Normal View History

2017-05-27 19:35:38 +03:00
'use strict';
2017-07-05 16:33:16 +03:00
const ParamCase = require('param-case');
const Uuid = require('uuid/v4');
2017-07-05 16:33:16 +03:00
const clean = (v) => {
return JSON.parse(JSON.stringify(v));
};
2017-05-27 19:35:38 +03:00
2017-06-05 23:54:44 +03:00
exports.fromPortal = function ({ portal, datacenter, deploymentGroups, user }) {
2017-05-27 19:35:38 +03:00
return {
id: portal.id,
2017-06-05 23:54:44 +03:00
user,
2017-05-27 19:35:38 +03:00
datacenter,
2017-06-08 00:35:45 +03:00
deploymentGroups
2017-05-27 19:35:38 +03:00
};
};
exports.toPortal = function (clientPortal) {
return {
2017-06-05 23:54:44 +03:00
user_id: clientPortal.user ? clientPortal.user.id : '',
2017-05-27 19:35:38 +03:00
datacenter_id: clientPortal.datacenter ? clientPortal.datacenter.id : '',
deployment_group_ids: clientPortal.deploymentGroups ? clientPortal.deploymentGroups.map((deploymentGroup) => {
return deploymentGroup.id;
}) : []
};
};
exports.fromDeploymentGroup = function (deploymentGroup) {
2017-05-27 19:35:38 +03:00
return {
id: deploymentGroup.id,
name: deploymentGroup.name,
slug: deploymentGroup.slug,
services: deploymentGroup.services,
version: deploymentGroup.version,
2017-07-05 16:33:16 +03:00
history: deploymentGroup.history,
imported: deploymentGroup.imported,
status: deploymentGroup.status
};
};
exports.toDeploymentGroup = function (clientDeploymentGroup) {
return clean({
id: clientDeploymentGroup.id,
name: clientDeploymentGroup.name,
slug: clientDeploymentGroup.slug ?
clientDeploymentGroup.slug :
clientDeploymentGroup.name ?
ParamCase(clientDeploymentGroup.name) :
undefined,
service_ids: Array.isArray(clientDeploymentGroup.services) ? clientDeploymentGroup.services.map(({ id }) => {
return id;
}).filter(Boolean) : undefined,
version_id: clientDeploymentGroup.version ? clientDeploymentGroup.version.id : undefined,
history_version_ids: Array.isArray(clientDeploymentGroup.history) ? clientDeploymentGroup.history.map(({ id }) => {
return id;
}).filter(Boolean) : undefined,
imported: clientDeploymentGroup.imported,
status: clientDeploymentGroup.status || 'ACTIVE'
});
};
2017-05-27 19:35:38 +03:00
exports.fromService = function ({ service, instances, branches, packages }) {
return {
id: service.id,
hash: service.version_hash,
deploymentGroupId: service.deployment_group_id,
name: service.name,
slug: service.slug,
instances,
connections: service.service_dependency_ids,
branches: branches,
2017-07-05 16:33:16 +03:00
config: service.config ? service.config : undefined,
2017-07-05 16:33:16 +03:00
status: service.status,
hasPlan: service.has_plan
};
};
2017-05-27 19:35:38 +03:00
exports.toService = function (clientService) {
2017-07-05 16:33:16 +03:00
return clean({
id: clientService.id,
2017-07-05 16:33:16 +03:00
version_hash: clientService.hash,
deployment_group_id: clientService.deploymentGroupId,
name: clientService.name,
slug: clientService.slug,
2017-07-05 16:33:16 +03:00
instance_ids: clientService.instances ?
clientService.instances.map((instance) => {
return instance.id;
}) :
undefined,
service_dependency_ids: clientService.connections,
2017-07-13 18:22:05 +03:00
branches: clientService.branches,
2017-07-05 16:33:16 +03:00
config: clientService.config ? clientService.config : undefined,
2017-07-05 16:33:16 +03:00
status: clientService.status,
has_plan: clientService.hasPlan
});
2017-05-27 19:35:38 +03:00
};
exports.toVersion = function (clientVersion) {
2017-07-05 16:33:16 +03:00
return clean({
2017-05-27 19:35:38 +03:00
id: clientVersion.id,
created: clientVersion.created || Date.now(),
manifest_id: (clientVersion.manifest || {}).id,
deployment_group_id: clientVersion.deploymentGroupId,
2017-07-05 16:33:16 +03:00
service_scales: clientVersion.scale ? clientVersion.scale : undefined,
plan: clientVersion.plan ? clientVersion.plan : undefined,
error: clientVersion.version
});
2017-05-27 19:35:38 +03:00
};
exports.fromVersion = function (version) {
return {
id: version.id,
created: version.created,
deploymentGroupId: version.deployment_group_id,
manifest: version.manifest,
2017-07-05 16:33:16 +03:00
scale: version.service_scales,
plan: version.plan,
error: version.error
2017-05-27 19:35:38 +03:00
};
};
exports.toManifest = function (clientManifest) {
return clean({
id: clientManifest.id,
deployment_group_id: clientManifest.deploymentGroupId,
created: clientManifest.created || Date.now(),
type: clientManifest.type,
format: clientManifest.format,
2017-07-05 16:33:16 +03:00
environment: clientManifest.environment,
files: clientManifest.files ? clientManifest.files.map((m) => {
return Object.assign({}, m, {
id: m.id || Uuid()
});
}) : undefined,
raw: clientManifest.raw
});
};
exports.fromManifest = function (manifest) {
return {
id: manifest.id,
deploymentGroupId: manifest.deployment_group_id,
created: manifest.created,
type: manifest.type,
format: manifest.format,
2017-07-05 16:33:16 +03:00
environment: manifest.environment,
files: manifest.files,
raw: manifest.raw,
json: manifest.json
};
};
exports.fromPackage = function (packages) {
return packages;
};
exports.toPackage = function (packages) {
return packages;
};
2017-08-22 19:55:37 +03:00
exports.fromInstance = function ({ instance, metrics }) {
return {
id: instance.id,
name: instance.name,
machineId: instance.machine_id,
primaryIp: instance.primary_ip,
serviceId: instance.service_id,
deploymentGroupId: instance.deployment_group_id,
2017-07-05 16:33:16 +03:00
status: instance.status,
healthy: instance.healthy,
watches: instance.watches,
2017-08-22 19:55:37 +03:00
jobs: instance.jobs,
metrics
};
};
exports.toInstance = function (clientInstance) {
2017-07-05 16:33:16 +03:00
return clean({
id: clientInstance.id,
name: clientInstance.name,
machine_id: clientInstance.machineId,
primary_ip: clientInstance.primaryIp,
deployment_group_id: clientInstance.deploymentGroupId,
service_id: clientInstance.serviceId,
2017-07-05 16:33:16 +03:00
status: clientInstance.status,
healthy: clientInstance.healthy,
watches: clientInstance.watches,
jobs: clientInstance.jobs
2017-07-05 16:33:16 +03:00
});
};
2017-06-05 23:54:44 +03:00
exports.fromUser = function (user) {
return {
id: user.id,
firstName: user.first_name,
lastName: user.last_name,
email: user.email,
login: user.login
};
};
exports.toUser = function (clientUser) {
return {
id: clientUser.id,
first_name: clientUser.firstName,
last_name: clientUser.lastName,
email: clientUser.email,
login: clientUser.login
};
};