Initial portal-data

This commit is contained in:
geek 2017-05-11 15:18:51 -05:00 committed by Sérgio Ramos
parent fd19fff435
commit 3fd18589a9
5 changed files with 250 additions and 0 deletions

3
portal-data/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
.DS_Store
npm-debug.log

1
portal-data/README.md Normal file
View File

@ -0,0 +1 @@
# portal-data

164
portal-data/lib/index.js Normal file
View File

@ -0,0 +1,164 @@
'use strict';
const Hoek = require('hoek');
const Penseur = require('penseur');
const internals = {
defaults: {
name: 'portal'
}
};
module.exports = class Data {
constructor (options) {
const settings = Hoek.applyToDefaults(options || {}, internals.defaults);
const name = settings.name;
delete settings.name;
// Penseur will assert that the options are correct
this._db = new Penseur.Db(name, settings);
}
connect (cb) {
return new Promise((resolve, reject) => {
this._db.establish(['activities', 'datacenters', 'deployments', 'manifests', 'metrics'], (err) => {
if (typeof cb === 'function') {
return cb(err);
}
if (err) {
return reject(err);
}
resolve();
});
});
}
createDeployment (deployment) {
return new Promise((resolve, reject) => {
deployment.services = [];
deployment.state = { current: 'stopped' };
this._db.deployments.insert(deployment, (err, key) => {
if (err) {
return reject(err);
}
deployment.id = key;
resolve(deployment);
});
});
}
getDeployment (id) {
return new Promise((resolve, reject) => {
this._db.deployments.get(id, (err, deployment) => {
return err ? reject(err) : resolve(deployment);
});
});
}
updateDeployment (deployment) {
return new Promise((resolve, reject) => {
this._db.deployments.update(deployment.id, deployment, (err) => {
return err ? reject(err) : resolve(deployment);
});
});
}
deleteDeployment (id) {
return new Promise((resolve, reject) => {
this._db.deployments.remove(id, (err) => {
return err ? reject(err) : resolve();
});
});
}
getDeployments () {
return new Promise((resolve, reject) => {
this._db.deployments.all((err, deployments) => {
return err ? reject(err) : resolve(deployments);
});
});
}
getDatacenters () {
return new Promise((resolve, reject) => {
this._db.datacenters.all((err, datacenters) => {
return err ? reject(err) : resolve(datacenters || []);
});
});
}
createManifest (deploymentId, manifest) {
return new Promise((resolve, reject) => {
manifest.deploymentId = deploymentId;
manifest.created = Date.now();
this._db.manifests.insert(manifest, (err, id) => {
if (err) {
return reject(err);
}
manifest.id = id;
resolve(manifest);
});
});
}
getManifest (id) {
return new Promise((resolve, reject) => {
this._db.manifests.get(id, (err, manifest) => {
return err ? reject(err) : resolve(manifest);
});
});
}
getActivities (deploymentId) {
return new Promise((resolve, reject) => {
this._db.activities.query({ deploymentId }, (err, activities) => {
return err ? reject(err) : resolve(activities || []);
});
});
}
getMetrics (deploymentId) {
return new Promise((resolve, reject) => {
this._db.metrics.query({ deploymentId }, (err, metrics) => {
return err ? reject(err) : resolve(metrics || []);
});
});
}
getServices (deploymentId) {
return new Promise((resolve, reject) => {
this._db.deployments.get(deploymentId, { filter: 'services' }, (err, deployment) => {
return err ? reject(err) : resolve(deployment.services);
});
});
}
updateService (deploymentId, service) {
return new Promise((resolve, reject) => {
this._db.deployments.get(deploymentId, { filter: 'services' }, (err, deployment) => {
if (err) {
return reject(err);
}
const services = deployment.services.map((currentService) => {
if (currentService.name === service.name) {
currentService.count = service.count;
}
return currentService;
});
this._db.deployments.update(deploymentId, { services }, (err, keys) => {
return err ? reject(err) : resolve(service);
});
});
});
}
};

25
portal-data/package.json Normal file
View File

@ -0,0 +1,25 @@
{
"name": "portal-data",
"version": "1.0.0",
"description": "portal data layer",
"main": "lib",
"scripts": {
"bootstrap": "node ./bootstrap-data",
"lint": "belly-button",
"rethinkdb-up": "docker run -d -p 8080:8080 -p 28015:28015 -p 29015:29015 --name rethinkdb rethinkdb",
"rethinkdb-down": "docker rm -f rethinkdb",
"test": "npm run lint && lab -t 40"
},
"keywords": [],
"author": "wyatt",
"license": "MPL-2.0",
"devDependencies": {
"belly-button": "^3.1.0",
"code": "^4.0.0",
"lab": "^13.0.4"
},
"dependencies": {
"hoek": "^4.1.1",
"penseur": "^7.8.1"
}
}

57
portal-data/test/index.js Normal file
View File

@ -0,0 +1,57 @@
'use strict';
const Code = require('code');
const Lab = require('lab');
const PortalData = require('../');
// Test shortcuts
const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;
const internals = {
options: { test: true, name: 'test' }
};
describe('createDeployment()', () => {
it('creates a deployment record in the deployment table', (done) => {
const data = new PortalData(internals.options);
data.connect().then(() => {
const deployment = {
name: 'User Services',
datacenter: 'us-sw-1'
};
data.createDeployment(deployment).then((deployment) => {
expect(deployment.id).to.exist();
done();
});
});
});
});
describe('getDeployment()', () => {
it('will retrieve an existing deployment', (done) => {
const data = new PortalData(internals.options);
data.connect().then(() => {
const deployment = {
name: 'User Services',
datacenter: 'us-sw-1'
};
data.createDeployment(deployment).then((deployment) => {
expect(deployment.id).to.exist();
data.getDeployment(deployment.id).then((retrievedDeployment) => {
expect(deployment).to.equal(retrievedDeployment);
done();
});
});
});
});
});