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

207 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-05-27 19:35:38 +03:00
'use strict';
const Yamljs = require('yamljs');
2017-07-05 16:33:16 +03:00
const ParamCase = require('param-case');
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, 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,
2017-07-05 16:33:16 +03:00
parent: service.parent_id,
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) {
// wat??
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,
parent_id: clientService.parent ? clientService.parent : undefined,
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,
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,
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 {
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,
raw: clientManifest.raw,
json: clientManifest.json || Yamljs.parse(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,
raw: manifest.raw,
json: manifest.json
};
};
exports.fromPackage = function (packages) {
return packages;
};
exports.toPackage = function (packages) {
return packages;
};
exports.fromInstance = function (instance) {
return {
id: instance.id,
name: instance.name,
machineId: instance.machine_id,
2017-07-05 16:33:16 +03:00
status: instance.status,
healthy: instance.healthy,
watchers: instance.watchers,
jobs: instance.jobs
};
};
exports.toInstance = function (clientInstance) {
2017-07-05 16:33:16 +03:00
return clean({
id: clientInstance.id,
name: clientInstance.name,
machine_id: clientInstance.machineId,
2017-07-05 16:33:16 +03:00
status: clientInstance.status,
healthy: clientInstance.healthy,
watchers: clientInstance.watchers,
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
};
};