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
				
			
		
							
								
								
									
										220
									
								
								server/api/custom_module/custom_module.controller.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										220
									
								
								server/api/custom_module/custom_module.controller.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,220 @@ | |||
| /** | ||||
|  * Using Rails-like standard naming convention for endpoints. | ||||
|  * GET     /api/custom_modules              ->  index | ||||
|  * POST    /api/custom_modules              ->  create | ||||
|  * GET     /api/custom_modules/:id          ->  show | ||||
|  * PUT     /api/custom_modules/:id          ->  upsert | ||||
|  * PATCH   /api/custom_modules/:id          ->  patch | ||||
|  * DELETE  /api/custom_modules/:id          ->  destroy | ||||
|  */ | ||||
| 
 | ||||
| 'use strict'; | ||||
| 
 | ||||
| import jsonpatch from 'fast-json-patch'; | ||||
| import CustomModule from './custom_module.model'; | ||||
| var ssh2_exec = require('../../components/ssh/ssh2_exec'); | ||||
| var scp2_exec = require('../../components/scp/scp_exec'); | ||||
| 
 | ||||
| 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 CustomModules
 | ||||
| export function index(req, res) { | ||||
| 
 | ||||
|   var ansibleEngine = req.body.ansibleEngine; | ||||
| 
 | ||||
|   if(!ansibleEngine.customModules){ | ||||
|     return res.status(500).send("Custom Modules Folder not defined in Ansible Engine") | ||||
|   } | ||||
| 
 | ||||
|   var command = 'ls "' + ansibleEngine.customModules + '"'; | ||||
| 
 | ||||
|   ssh2_exec.executeCommand(command, | ||||
|     null, | ||||
|     function(data){ | ||||
|       res.send(data) | ||||
|     }, | ||||
|     function(data){ | ||||
|       res.status(500).send(data) | ||||
|     }, | ||||
|     ansibleEngine | ||||
|   ); | ||||
| 
 | ||||
|   /*return CustomModule.find().exec() | ||||
|    .then(respondWithResult(res)) | ||||
|    .catch(handleError(res));*/ | ||||
| } | ||||
| 
 | ||||
| // Gets a single CustomModule from the DB
 | ||||
| export function show(req, res) { | ||||
|   console.log("Show " + req.params.custom_module); | ||||
|   var ansibleEngine = req.body.ansibleEngine; | ||||
| 
 | ||||
|   if(!ansibleEngine.customModules){ | ||||
|     res.status(500).send("Custom Modules Folder not defined in Ansible Engine") | ||||
|   } | ||||
| 
 | ||||
|   var command = 'cat "' + ansibleEngine.customModules + '"/' + req.params.custom_module; | ||||
| 
 | ||||
|   if(req.params.custom_module === 'template.py'){ | ||||
|     command = 'cat ' + '/opt/ehc-builder-scripts/ansible_modules/template.py'; | ||||
|   } | ||||
| 
 | ||||
| 
 | ||||
|   ssh2_exec.executeCommand(command, | ||||
|     null, | ||||
|     function(data){ | ||||
|       res.send(data); | ||||
|     }, | ||||
|     function(data){ | ||||
|       res.status(500).send(data) | ||||
|     }, | ||||
|     ansibleEngine | ||||
|   ); | ||||
| 
 | ||||
|   /*return CustomModule.findById(req.params.custom_module).exec() | ||||
|    .then(handleEntityNotFound(res)) | ||||
|    .then(respondWithResult(res)) | ||||
|    .catch(handleError(res));*/ | ||||
| } | ||||
| 
 | ||||
| // Test Module
 | ||||
| export function testModule(req, res) { | ||||
| 
 | ||||
|   var ansibleEngine = req.body.ansibleEngine; | ||||
| 
 | ||||
|   var moduleArgs = req.body.moduleArgs; | ||||
| 
 | ||||
|   if(!ansibleEngine.customModules){ | ||||
|     res.status(500).send("Custom Modules Folder not defined in Ansible Engine") | ||||
|   } | ||||
| 
 | ||||
|   var command = '/opt/ansible/ansible-devel/hacking/test-module -m "' + ansibleEngine.customModules + '/' + req.params.custom_module + "\" -a '" + JSON.stringify(moduleArgs) + "'"; | ||||
| 
 | ||||
|   console.log("Command=" + command); | ||||
| 
 | ||||
|   ssh2_exec.executeCommand(command, | ||||
|     null, | ||||
|     function(data){ | ||||
|       res.send(data); | ||||
|     }, | ||||
|     function(data){ | ||||
|       res.status(500).send(data) | ||||
|     }, | ||||
|     ansibleEngine | ||||
|   ); | ||||
| 
 | ||||
|   /*return CustomModule.findById(req.params.custom_module).exec() | ||||
|    .then(handleEntityNotFound(res)) | ||||
|    .then(respondWithResult(res)) | ||||
|    .catch(handleError(res));*/ | ||||
| } | ||||
| 
 | ||||
| // Creates a new CustomModule in the DB
 | ||||
| export function create(req, res) { | ||||
| 
 | ||||
|   console.log("Create"); | ||||
| 
 | ||||
|   var custom_module_name =  req.params.custom_module; | ||||
|   var custom_module_code =  req.body.custom_module_code; | ||||
| 
 | ||||
|   var ansibleEngine = req.body.ansibleEngine; | ||||
| 
 | ||||
|   if(!ansibleEngine.customModules){ | ||||
|     res.status(500).send("Custom Modules Folder not defined in Ansible Engine") | ||||
|   } | ||||
| 
 | ||||
|   console.log("Custom module name " + "\"" + ansibleEngine.customModules + '/' + custom_module_name + "\"") | ||||
| 
 | ||||
|   scp2_exec.createFileOnScriptEngine(custom_module_code,ansibleEngine.customModules + '/' + custom_module_name, | ||||
|     function(){ | ||||
|       res.send("Saved") | ||||
|     },function(err){ | ||||
|       res.status(500).send("Failed to create file on target") | ||||
|     }, | ||||
|     ansibleEngine | ||||
|   ); | ||||
| 
 | ||||
|   /*return CustomModule.create(req.body) | ||||
|    .then(respondWithResult(res, 201)) | ||||
|    .catch(handleError(res));*/ | ||||
| } | ||||
| 
 | ||||
| // Upserts the given CustomModule in the DB at the specified ID
 | ||||
| export function upsert(req, res) { | ||||
|   if(req.body._id) { | ||||
|     Reflect.deleteProperty(req.body, '_id'); | ||||
|   } | ||||
|   return CustomModule.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 CustomModule in the DB
 | ||||
| export function patch(req, res) { | ||||
|   if(req.body._id) { | ||||
|     Reflect.deleteProperty(req.body, '_id'); | ||||
|   } | ||||
|   return CustomModule.findById(req.params.id).exec() | ||||
|     .then(handleEntityNotFound(res)) | ||||
|     .then(patchUpdates(req.body)) | ||||
|     .then(respondWithResult(res)) | ||||
|     .catch(handleError(res)); | ||||
| } | ||||
| 
 | ||||
| // Deletes a CustomModule from the DB
 | ||||
| export function destroy(req, res) { | ||||
|   return CustomModule.findById(req.params.id).exec() | ||||
|     .then(handleEntityNotFound(res)) | ||||
|     .then(removeEntity(res)) | ||||
|     .catch(handleError(res)); | ||||
| } | ||||
							
								
								
									
										35
									
								
								server/api/custom_module/custom_module.events.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								server/api/custom_module/custom_module.events.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,35 @@ | |||
| /** | ||||
|  * CustomModule model events | ||||
|  */ | ||||
| 
 | ||||
| 'use strict'; | ||||
| 
 | ||||
| import {EventEmitter} from 'events'; | ||||
| var CustomModuleEvents = new EventEmitter(); | ||||
| 
 | ||||
| // Set max event listeners (0 == unlimited)
 | ||||
| CustomModuleEvents.setMaxListeners(0); | ||||
| 
 | ||||
| // Model events
 | ||||
| var events = { | ||||
|   save: 'save', | ||||
|   remove: 'remove' | ||||
| }; | ||||
| 
 | ||||
| // Register the event emitter to the model events
 | ||||
| function registerEvents(CustomModule) { | ||||
|   for(var e in events) { | ||||
|     let event = events[e]; | ||||
|     CustomModule.post(e, emitEvent(event)); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| function emitEvent(event) { | ||||
|   return function(doc) { | ||||
|     CustomModuleEvents.emit(event + ':' + doc._id, doc); | ||||
|     CustomModuleEvents.emit(event, doc); | ||||
|   }; | ||||
| } | ||||
| 
 | ||||
| export {registerEvents}; | ||||
| export default CustomModuleEvents; | ||||
							
								
								
									
										190
									
								
								server/api/custom_module/custom_module.integration.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										190
									
								
								server/api/custom_module/custom_module.integration.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,190 @@ | |||
| 'use strict'; | ||||
| 
 | ||||
| /* globals describe, expect, it, beforeEach, afterEach */ | ||||
| 
 | ||||
| var app = require('../..'); | ||||
| import request from 'supertest'; | ||||
| 
 | ||||
| var newCustomModule; | ||||
| 
 | ||||
| describe('CustomModule API:', function() { | ||||
|   describe('GET /api/custom_modules', function() { | ||||
|     var customModules; | ||||
| 
 | ||||
|     beforeEach(function(done) { | ||||
|       request(app) | ||||
|         .get('/api/custom_modules') | ||||
|         .expect(200) | ||||
|         .expect('Content-Type', /json/) | ||||
|         .end((err, res) => { | ||||
|           if(err) { | ||||
|             return done(err); | ||||
|           } | ||||
|           customModules = res.body; | ||||
|           done(); | ||||
|         }); | ||||
|     }); | ||||
| 
 | ||||
|     it('should respond with JSON array', function() { | ||||
|       expect(customModules).to.be.instanceOf(Array); | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('POST /api/custom_modules', function() { | ||||
|     beforeEach(function(done) { | ||||
|       request(app) | ||||
|         .post('/api/custom_modules') | ||||
|         .send({ | ||||
|           name: 'New CustomModule', | ||||
|           info: 'This is the brand new customModule!!!' | ||||
|         }) | ||||
|         .expect(201) | ||||
|         .expect('Content-Type', /json/) | ||||
|         .end((err, res) => { | ||||
|           if(err) { | ||||
|             return done(err); | ||||
|           } | ||||
|           newCustomModule = res.body; | ||||
|           done(); | ||||
|         }); | ||||
|     }); | ||||
| 
 | ||||
|     it('should respond with the newly created customModule', function() { | ||||
|       expect(newCustomModule.name).to.equal('New CustomModule'); | ||||
|       expect(newCustomModule.info).to.equal('This is the brand new customModule!!!'); | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('GET /api/custom_modules/:id', function() { | ||||
|     var customModule; | ||||
| 
 | ||||
|     beforeEach(function(done) { | ||||
|       request(app) | ||||
|         .get(`/api/custom_modules/${newCustomModule._id}`) | ||||
|         .expect(200) | ||||
|         .expect('Content-Type', /json/) | ||||
|         .end((err, res) => { | ||||
|           if(err) { | ||||
|             return done(err); | ||||
|           } | ||||
|           customModule = res.body; | ||||
|           done(); | ||||
|         }); | ||||
|     }); | ||||
| 
 | ||||
|     afterEach(function() { | ||||
|       customModule = {}; | ||||
|     }); | ||||
| 
 | ||||
|     it('should respond with the requested customModule', function() { | ||||
|       expect(customModule.name).to.equal('New CustomModule'); | ||||
|       expect(customModule.info).to.equal('This is the brand new customModule!!!'); | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('PUT /api/custom_modules/:id', function() { | ||||
|     var updatedCustomModule; | ||||
| 
 | ||||
|     beforeEach(function(done) { | ||||
|       request(app) | ||||
|         .put(`/api/custom_modules/${newCustomModule._id}`) | ||||
|         .send({ | ||||
|           name: 'Updated CustomModule', | ||||
|           info: 'This is the updated customModule!!!' | ||||
|         }) | ||||
|         .expect(200) | ||||
|         .expect('Content-Type', /json/) | ||||
|         .end(function(err, res) { | ||||
|           if(err) { | ||||
|             return done(err); | ||||
|           } | ||||
|           updatedCustomModule = res.body; | ||||
|           done(); | ||||
|         }); | ||||
|     }); | ||||
| 
 | ||||
|     afterEach(function() { | ||||
|       updatedCustomModule = {}; | ||||
|     }); | ||||
| 
 | ||||
|     it('should respond with the updated customModule', function() { | ||||
|       expect(updatedCustomModule.name).to.equal('Updated CustomModule'); | ||||
|       expect(updatedCustomModule.info).to.equal('This is the updated customModule!!!'); | ||||
|     }); | ||||
| 
 | ||||
|     it('should respond with the updated customModule on a subsequent GET', function(done) { | ||||
|       request(app) | ||||
|         .get(`/api/custom_modules/${newCustomModule._id}`) | ||||
|         .expect(200) | ||||
|         .expect('Content-Type', /json/) | ||||
|         .end((err, res) => { | ||||
|           if(err) { | ||||
|             return done(err); | ||||
|           } | ||||
|           let customModule = res.body; | ||||
| 
 | ||||
|           expect(customModule.name).to.equal('Updated CustomModule'); | ||||
|           expect(customModule.info).to.equal('This is the updated customModule!!!'); | ||||
| 
 | ||||
|           done(); | ||||
|         }); | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('PATCH /api/custom_modules/:id', function() { | ||||
|     var patchedCustomModule; | ||||
| 
 | ||||
|     beforeEach(function(done) { | ||||
|       request(app) | ||||
|         .patch(`/api/custom_modules/${newCustomModule._id}`) | ||||
|         .send([ | ||||
|           { op: 'replace', path: '/name', value: 'Patched CustomModule' }, | ||||
|           { op: 'replace', path: '/info', value: 'This is the patched customModule!!!' } | ||||
|         ]) | ||||
|         .expect(200) | ||||
|         .expect('Content-Type', /json/) | ||||
|         .end(function(err, res) { | ||||
|           if(err) { | ||||
|             return done(err); | ||||
|           } | ||||
|           patchedCustomModule = res.body; | ||||
|           done(); | ||||
|         }); | ||||
|     }); | ||||
| 
 | ||||
|     afterEach(function() { | ||||
|       patchedCustomModule = {}; | ||||
|     }); | ||||
| 
 | ||||
|     it('should respond with the patched customModule', function() { | ||||
|       expect(patchedCustomModule.name).to.equal('Patched CustomModule'); | ||||
|       expect(patchedCustomModule.info).to.equal('This is the patched customModule!!!'); | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('DELETE /api/custom_modules/:id', function() { | ||||
|     it('should respond with 204 on successful removal', function(done) { | ||||
|       request(app) | ||||
|         .delete(`/api/custom_modules/${newCustomModule._id}`) | ||||
|         .expect(204) | ||||
|         .end(err => { | ||||
|           if(err) { | ||||
|             return done(err); | ||||
|           } | ||||
|           done(); | ||||
|         }); | ||||
|     }); | ||||
| 
 | ||||
|     it('should respond with 404 when customModule does not exist', function(done) { | ||||
|       request(app) | ||||
|         .delete(`/api/custom_modules/${newCustomModule._id}`) | ||||
|         .expect(404) | ||||
|         .end(err => { | ||||
|           if(err) { | ||||
|             return done(err); | ||||
|           } | ||||
|           done(); | ||||
|         }); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
							
								
								
									
										13
									
								
								server/api/custom_module/custom_module.model.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								server/api/custom_module/custom_module.model.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,13 @@ | |||
| 'use strict'; | ||||
| 
 | ||||
| import mongoose from 'mongoose'; | ||||
| import {registerEvents} from './custom_module.events'; | ||||
| 
 | ||||
| var CustomModuleSchema = new mongoose.Schema({ | ||||
|   name: String, | ||||
|   info: String, | ||||
|   active: Boolean | ||||
| }); | ||||
| 
 | ||||
| registerEvents(CustomModuleSchema); | ||||
| export default mongoose.model('CustomModule', CustomModuleSchema); | ||||
							
								
								
									
										16
									
								
								server/api/custom_module/index.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								server/api/custom_module/index.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,16 @@ | |||
| 'use strict'; | ||||
| 
 | ||||
| var express = require('express'); | ||||
| var controller = require('./custom_module.controller'); | ||||
| 
 | ||||
| var router = express.Router(); | ||||
| 
 | ||||
| router.post('/query', controller.index); | ||||
| router.post('/:custom_module/test', controller.testModule); | ||||
| router.post('/:custom_module/get', controller.show); | ||||
| router.post('/:custom_module', controller.create); | ||||
| router.put('/:id', controller.upsert); | ||||
| router.patch('/:id', controller.patch); | ||||
| router.delete('/:id', controller.destroy); | ||||
| 
 | ||||
| module.exports = router; | ||||
							
								
								
									
										86
									
								
								server/api/custom_module/index.spec.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								server/api/custom_module/index.spec.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,86 @@ | |||
| 'use strict'; | ||||
| 
 | ||||
| /* globals sinon, describe, expect, it */ | ||||
| 
 | ||||
| var proxyquire = require('proxyquire').noPreserveCache(); | ||||
| 
 | ||||
| var customModuleCtrlStub = { | ||||
|   index: 'customModuleCtrl.index', | ||||
|   show: 'customModuleCtrl.show', | ||||
|   create: 'customModuleCtrl.create', | ||||
|   upsert: 'customModuleCtrl.upsert', | ||||
|   patch: 'customModuleCtrl.patch', | ||||
|   destroy: 'customModuleCtrl.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 customModuleIndex = proxyquire('./index.js', { | ||||
|   express: { | ||||
|     Router() { | ||||
|       return routerStub; | ||||
|     } | ||||
|   }, | ||||
|   './custom_module.controller': customModuleCtrlStub | ||||
| }); | ||||
| 
 | ||||
| describe('CustomModule API Router:', function() { | ||||
|   it('should return an express router instance', function() { | ||||
|     expect(customModuleIndex).to.equal(routerStub); | ||||
|   }); | ||||
| 
 | ||||
|   describe('GET /api/custom_modules', function() { | ||||
|     it('should route to customModule.controller.index', function() { | ||||
|       expect(routerStub.get | ||||
|         .withArgs('/', 'customModuleCtrl.index') | ||||
|         ).to.have.been.calledOnce; | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('GET /api/custom_modules/:id', function() { | ||||
|     it('should route to customModule.controller.show', function() { | ||||
|       expect(routerStub.get | ||||
|         .withArgs('/:id', 'customModuleCtrl.show') | ||||
|         ).to.have.been.calledOnce; | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('POST /api/custom_modules', function() { | ||||
|     it('should route to customModule.controller.create', function() { | ||||
|       expect(routerStub.post | ||||
|         .withArgs('/', 'customModuleCtrl.create') | ||||
|         ).to.have.been.calledOnce; | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('PUT /api/custom_modules/:id', function() { | ||||
|     it('should route to customModule.controller.upsert', function() { | ||||
|       expect(routerStub.put | ||||
|         .withArgs('/:id', 'customModuleCtrl.upsert') | ||||
|         ).to.have.been.calledOnce; | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('PATCH /api/custom_modules/:id', function() { | ||||
|     it('should route to customModule.controller.patch', function() { | ||||
|       expect(routerStub.patch | ||||
|         .withArgs('/:id', 'customModuleCtrl.patch') | ||||
|         ).to.have.been.calledOnce; | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe('DELETE /api/custom_modules/:id', function() { | ||||
|     it('should route to customModule.controller.destroy', function() { | ||||
|       expect(routerStub.delete | ||||
|         .withArgs('/:id', 'customModuleCtrl.destroy') | ||||
|         ).to.have.been.calledOnce; | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue