Skip to content

Commit

Permalink
inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Klerk Signorini committed Aug 24, 2017
1 parent d78d7ad commit dfb1406
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 14 deletions.
23 changes: 23 additions & 0 deletions app/core/applications/relationsApplication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const _ = require('lodash');

const DRelationsSyncerService = require('core/services/RelationsSyncerService');


const RelationsApp = (Entity, REntity, RelationsSyncerService = DRelationsSyncerService) => {

return {
syncer: (req, res, next) => {
const source = 'params.id';

RelationsSyncerService(Entity)(REntity)
.syncer(req, source)
.then(e => res.json(e))
.catch(next);
}

};
};

module.exports = _.curry(RelationsApp);
3 changes: 2 additions & 1 deletion app/core/applications/transforms/strIDtoObjectID.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const {ObjectId} = require('mongorito');

const transfID = (data, key) => {
if(_.has(data, key)) {
data[key] = ObjectId(_.get(data, key));
const id = ObjectId(_.get(data, key));
_.set(data, key, id);
}

return data;
Expand Down
7 changes: 4 additions & 3 deletions app/core/hooks/relationInc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ const DFactoryDBRepository = require('core/repositories/DBRepository');

const {transfID} = require('core/applications/transforms/strIDtoObjectID');


const relationInc = (configs) => (data) => {
const {Entity, field, source} = configs;

const {Entity, field} = configs;
const source = _.get(configs, 'source', `${Entity.name}._id`);

if(_.has(data, source)) {
const inc = _.get(configs, 'inc', 1);
Expand All @@ -22,6 +21,8 @@ const relationInc = (configs) => (data) => {
DBRepository
.increment({_id}, post)
.catch(console.log);

return post;
}
}

Expand Down
1 change: 1 addition & 0 deletions app/core/repositories/DBRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const DBRepository = (Entity, options={}) => {
count (filters = {}, fill = Entity.singleFilled) {
return new Promise((resolve, reject) => {
const filter = findFilledFormat(filters, fill);

return DB.count(filter)
.then(resolve)
.catch(reject);
Expand Down
15 changes: 15 additions & 0 deletions app/core/services/PersistenceServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ const Persistence = (Entity, FactoryDBRepository = DFactoryDBRepository) => {
});
},

count (query, owner, access = Access.ROLE_READ) {
return new Promise((resolve, reject) => {

const prepared = _.assign({},
query,
accessMergeTransform(owner, Entity.access, query, access),
...regexFilterQuery(_.get(query, 'query'))
);

DBRepository.count(prepared)
.then(resolve)
.catch(reject);
});
},

findOne (_id, owner, access = Access.ROLE_READ) {

return new Promise((resolve, reject) => {
Expand Down
35 changes: 35 additions & 0 deletions app/core/services/RelationsSyncerService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const _ = require('lodash');
const DBRepository = require('core/repositories/DBRepository');
const {transfID} = require('core/applications/transforms/strIDtoObjectID');


const RelationsSyncerService = (Entity) => (REntity) => {

return {
syncer: (data, source = '_id') => {

return new Promise((resolve, reject) => {

const _id = _.get(transfID(data, source), source);
const path = `${Entity.name}._id`;

const fielder = `${REntity.name}_count`;

DBRepository(REntity)
.count({[path]:_id}, [path])
.then(total => {
return DBRepository(Entity)
.update({_id}, {[fielder]: total})
.catch(console.log);
})
.then(resolve)
.catch(reject);
});
}

};
};

module.exports = RelationsSyncerService;
2 changes: 1 addition & 1 deletion app/inventory/applications/persistenceServers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ApplicationServers = (Entity, PersistenceServices = DPersistenceServices)

query = filterHooks(query, field, [
{dest: 'id', source: '_id', module: 'swap'},
{dest: 'dc.name', source: 'dc', module: 'swap'},
{dest: 'datacenters.name', source: 'datacenters', module: 'swap'},
{dest: 'os.base', source: 'os', module: 'swap'},
{dest: 'environment', source: 'environment', module: 'swap'},
{dest: 'role', source: 'role', module: 'swap'},
Expand Down
8 changes: 4 additions & 4 deletions app/inventory/entities/Servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const _ = require('lodash');
const Servers = require('../repositories/dao/servers');

const servers = () => {
const resFilled = ['_id', 'updated_at', 'created_at', 'hostname', 'ipv4_private', 'ipv4_public', 'os.base', 'os.dist', 'dc.name', 'dc.region', 'dc.zone', 'role', 'environment', 'auth.name', 'auth.username', 'auth.type', 'tags'];
const resFilled = ['_id', 'updated_at', 'created_at', 'hostname', 'ipv4_private', 'ipv4_public', 'os.base', 'os.dist', 'datacenters.name', 'datacenters.region', 'datacenters.zone', 'role', 'environment', 'auth.name', 'auth.username', 'auth.type', 'tags'];

const singleFilled = [...resFilled, 'cpu', 'memory', 'storage', 'services', 'dc', 'os', 'auth', 'role', 'environment',
const singleFilled = [...resFilled, 'cpu', 'memory', 'storage', 'services', 'datacenters', 'os', 'auth', 'role', 'environment',
'roles', 'owner', 'active', 'status'];

const filled = [..._.slice(singleFilled, 3)]; // delete id
Expand All @@ -23,14 +23,14 @@ const servers = () => {

defaults: {},

mapRelations: [],
mapRelations: ['datacenters'],

hooks: {
after_create: {
relationInc: {
Entity: require('inventory/entities/Datacenter'),
field: 'servers_count',
source: 'dc._id'
source: 'datacenters._id'
}
}
},
Expand Down
11 changes: 10 additions & 1 deletion app/inventory/routers/datacenters/datacenters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

const authenticate = require('identity/profile/middlewares/authenticate');
const Datacenter = require('../../entities/Datacenter');
const Servers = require('../../entities/Servers');

const PersistenceApp = require('core/applications/persistenceApplication')(Datacenter);
const SyncerApp = require('core/applications/relationsApplication')(Datacenter)(Servers);

const AccessApp = require('core/applications/accessApplication')(Datacenter);

module.exports = function (router) {
Expand All @@ -25,5 +29,10 @@ module.exports = function (router) {

.put('/:id/roles/:idu', authenticate(), AccessApp.update)

.delete('/:id/roles/:idu', authenticate(), AccessApp.remove);
.delete('/:id/roles/:idu', authenticate(), AccessApp.remove)

/*
Actions
*/
.patch('/:id/sync_count_servers/', authenticate(), SyncerApp.syncer);
};
2 changes: 2 additions & 0 deletions app/inventory/routers/datacenters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ module.exports = function (router) {
*/
.delete('/teams/:id/datacenters/:idu/roles/:ida', authenticate(), WrapperAccessApp.remove);



};
10 changes: 7 additions & 3 deletions app/inventory/validators/datacenters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ const roles = Joi.object().keys({
email: Joi.string().max(250)
});

const create = {
name: Joi.string().min(3).max(30).required()
};

const scheme = {
name: Joi.string().min(3).max(30).required(),
name: Joi.string().min(3).max(30),
role: Joi.any(),
zones: Joi.array(),
regions: Joi.array(),
Expand All @@ -29,8 +33,8 @@ const scheme = {
};

module.exports = {
create: scheme,
update: scheme,
create: Joi.object().keys(Object.assign({}, scheme, create)),
update: Joi.object().keys(Object.assign({}, scheme)),
delete: {},
list: {}
};
2 changes: 1 addition & 1 deletion app/inventory/validators/servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const schema = Joi.object().keys({
memory: Joi.number().positive().max(1024),
storage: Joi.array().items(storage).unique('name'),
services: Joi.array().items(services).unique('name'),
dc: Joi.object(),
datacenters: Joi.object(),
role: Joi.string().valid('Application', 'Cache', 'Container', 'Database', 'File', 'Loadbalance', 'Monitoring', 'NAT', 'Proxy', 'SMTP', 'VPN', 'Standard').required(),
environment: Joi.string().valid('Production', 'Staging', 'Development', 'UTA', 'Training', 'SandBox').required(),
auth: Joi.array().items(auth),
Expand Down

0 comments on commit dfb1406

Please sign in to comment.