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:
commit
c92f737237
273 changed files with 16964 additions and 0 deletions
152
server/api/project/project.controller.js
Normal file
152
server/api/project/project.controller.js
Normal 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));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue