1
0
Fork 0
mirror of https://github.com/mmumshad/ansible-playable.git synced 2025-03-09 23:38:54 +00:00

Initial Commit

This commit is contained in:
Mumshad Mannambeth 2017-06-07 13:36:44 -04:00
commit c92f737237
273 changed files with 16964 additions and 0 deletions

View file

@ -0,0 +1,15 @@
'use strict';
var express = require('express');
var controller = require('./project.controller');
var router = express.Router();
router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -0,0 +1,86 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var projectCtrlStub = {
index: 'projectCtrl.index',
show: 'projectCtrl.show',
create: 'projectCtrl.create',
upsert: 'projectCtrl.upsert',
patch: 'projectCtrl.patch',
destroy: 'projectCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var projectIndex = proxyquire('./index.js', {
express: {
Router() {
return routerStub;
}
},
'./project.controller': projectCtrlStub
});
describe('Project API Router:', function() {
it('should return an express router instance', function() {
expect(projectIndex).to.equal(routerStub);
});
describe('GET /api/projects', function() {
it('should route to project.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'projectCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/projects/:id', function() {
it('should route to project.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'projectCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/projects', function() {
it('should route to project.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'projectCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/projects/:id', function() {
it('should route to project.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'projectCtrl.upsert')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/projects/:id', function() {
it('should route to project.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'projectCtrl.patch')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/projects/:id', function() {
it('should route to project.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'projectCtrl.destroy')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,152 @@
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/projects -> index
* POST /api/projects -> create
* GET /api/projects/:id -> show
* PUT /api/projects/:id -> upsert
* PATCH /api/projects/:id -> patch
* DELETE /api/projects/:id -> destroy
*/
'use strict';
import jsonpatch from 'fast-json-patch';
import Project from './project.model';
var ansibleTool = require('../../components/ansible/ansible_tool');
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if(entity) {
return res.status(statusCode).json(entity);
}
return null;
};
}
function patchUpdates(patches) {
return function(entity) {
try {
// eslint-disable-next-line prefer-reflect
jsonpatch.apply(entity, patches, /*validate*/ true);
} catch(err) {
return Promise.reject(err);
}
return entity.save();
};
}
function removeEntity(res) {
return function(entity) {
if(entity) {
return entity.remove()
.then(() => {
res.status(204).end();
});
}
};
}
function handleEntityNotFound(res) {
return function(entity) {
if(!entity) {
res.status(404).end();
return null;
}
return entity;
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}
// Gets a list of Projects
export function index(req, res) {
console.log("Getting projects list");
return Project.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Gets a single Project from the DB
export function show(req, res) {
return Project.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Creates a new Project in the DB
export function create(req, res) {
var ansibleEngine = req.body.ansibleEngine;
console.log("Ansible Engine " + JSON.stringify(ansibleEngine));
if(ansibleEngine.ansibleHost){
ansibleTool.getAnsibleVersion(
function(version){
req.body.ansibleVersion = version;
ansibleTool.createProjectFolder(ansibleEngine.projectFolder,
function(){
return Project.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
},
function(data){
res.status(500).send(data)
}, ansibleEngine);
//res.write(data);
//res.end()
},
function(data){
res.status(500).send("" + data);
},ansibleEngine
)
}else{
return Project.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
}
// Upserts the given Project in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Project.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing Project in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Project.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a Project from the DB
export function destroy(req, res) {
return Project.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -0,0 +1,35 @@
/**
* Project model events
*/
'use strict';
import {EventEmitter} from 'events';
var ProjectEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
ProjectEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(Project) {
for(var e in events) {
let event = events[e];
Project.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
ProjectEvents.emit(event + ':' + doc._id, doc);
ProjectEvents.emit(event, doc);
};
}
export {registerEvents};
export default ProjectEvents;

View file

@ -0,0 +1,190 @@
'use strict';
/* globals describe, expect, it, beforeEach, afterEach */
var app = require('../..');
import request from 'supertest';
var newProject;
describe('Project API:', function() {
describe('GET /api/projects', function() {
var projects;
beforeEach(function(done) {
request(app)
.get('/api/projects')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
projects = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(projects).to.be.instanceOf(Array);
});
});
describe('POST /api/projects', function() {
beforeEach(function(done) {
request(app)
.post('/api/projects')
.send({
name: 'New Project',
info: 'This is the brand new project!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newProject = res.body;
done();
});
});
it('should respond with the newly created project', function() {
expect(newProject.name).to.equal('New Project');
expect(newProject.info).to.equal('This is the brand new project!!!');
});
});
describe('GET /api/projects/:id', function() {
var project;
beforeEach(function(done) {
request(app)
.get(`/api/projects/${newProject._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
project = res.body;
done();
});
});
afterEach(function() {
project = {};
});
it('should respond with the requested project', function() {
expect(project.name).to.equal('New Project');
expect(project.info).to.equal('This is the brand new project!!!');
});
});
describe('PUT /api/projects/:id', function() {
var updatedProject;
beforeEach(function(done) {
request(app)
.put(`/api/projects/${newProject._id}`)
.send({
name: 'Updated Project',
info: 'This is the updated project!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedProject = res.body;
done();
});
});
afterEach(function() {
updatedProject = {};
});
it('should respond with the updated project', function() {
expect(updatedProject.name).to.equal('Updated Project');
expect(updatedProject.info).to.equal('This is the updated project!!!');
});
it('should respond with the updated project on a subsequent GET', function(done) {
request(app)
.get(`/api/projects/${newProject._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let project = res.body;
expect(project.name).to.equal('Updated Project');
expect(project.info).to.equal('This is the updated project!!!');
done();
});
});
});
describe('PATCH /api/projects/:id', function() {
var patchedProject;
beforeEach(function(done) {
request(app)
.patch(`/api/projects/${newProject._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched Project' },
{ op: 'replace', path: '/info', value: 'This is the patched project!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patchedProject = res.body;
done();
});
});
afterEach(function() {
patchedProject = {};
});
it('should respond with the patched project', function() {
expect(patchedProject.name).to.equal('Patched Project');
expect(patchedProject.info).to.equal('This is the patched project!!!');
});
});
describe('DELETE /api/projects/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/projects/${newProject._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
it('should respond with 404 when project does not exist', function(done) {
request(app)
.delete(`/api/projects/${newProject._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -0,0 +1,22 @@
'use strict';
import mongoose from 'mongoose';
import {registerEvents} from './project.events';
var ProjectSchema = new mongoose.Schema({
name: String,
ansibleEngine: {},
ansibleVersion : String,
creationTime: Date,
info: String,
active: Boolean,
ansible_data: String, //YAML Format
ansible_data_json: {}, //JSON Format
inventory_data: String, //YAML Format
inventory_data_json: {}, //JSON Format
roles_data: String, //YAML Format
roles_data_json: {} //JSON Format
});
registerEvents(ProjectSchema);
export default mongoose.model('Project', ProjectSchema);