From 2e60b91c63ca1e5414c13027713e14ee3b39ce29 Mon Sep 17 00:00:00 2001 From: Matej Kramny Date: Mon, 25 Aug 2014 00:02:34 +0100 Subject: [PATCH] Add playbooks via GUI --- Vagrantfile | 1 + lib/routes/index.js | 1 + lib/routes/playbook/playbook.js | 64 ++++++++++++++++++++++ lib/routes/playbook/playbooks.js | 29 +++++++++- lib/views/abstract.jade | 1 + lib/views/layout.jade | 2 +- lib/views/playbook/add.jade | 22 +++++++- lib/views/playbook/view.jade | 3 + public/js/controllers/playbook/add.js | 8 +++ public/js/controllers/playbook/playbook.js | 7 +++ public/js/factories/playbook.js | 40 ++++++++++++++ public/js/routes/playbooks.js | 40 +++++++++++++- 12 files changed, 210 insertions(+), 8 deletions(-) create mode 100644 lib/routes/playbook/playbook.js create mode 100644 lib/views/abstract.jade create mode 100644 lib/views/playbook/view.jade create mode 100644 public/js/controllers/playbook/add.js create mode 100644 public/js/controllers/playbook/playbook.js create mode 100644 public/js/factories/playbook.js diff --git a/Vagrantfile b/Vagrantfile index 73d3df42..fef66222 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -5,6 +5,7 @@ VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "hashicorp/precise32" + config.vm.network :forwarded_port, guest: 3000, host: 3000 config.vm.provider "virtualbox" do |vb| # vb.gui = true diff --git a/lib/routes/index.js b/lib/routes/index.js index ab80db89..b38d2652 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -11,6 +11,7 @@ exports.router = function(app) { ]); templates.add('homepage') + templates.add('abstract') templates.setup(); app.get('/', layout); diff --git a/lib/routes/playbook/playbook.js b/lib/routes/playbook/playbook.js new file mode 100644 index 00000000..a02012f6 --- /dev/null +++ b/lib/routes/playbook/playbook.js @@ -0,0 +1,64 @@ +var models = require('../../models') +var mongoose = require('mongoose') +var express = require('express') + +exports.unauthorized = function (app, template) { + template([ + 'view' + ], { + prefix: 'playbook' + }); +} + +exports.httpRouter = function (app) { +} + +exports.router = function (app) { + var playbook = express.Router(); + + playbook.get('/', view) + .put('/', save) + .delete('/', remove) + + app.param('playbook_id', getPlaybook) + app.use('/playbook/:playbook_id', playbook); +} + +function getPlaybook (req, res, next, id) { + models.Playbook.findOne({ + _id: id + }, function (err, playbook) { + if (err || !playbook) { + return res.send(404); + } + + req.playbook = playbook; + next(); + }); +} + +function view (req, res) { + res.send(req.playbook); +} + +function save (req, res) { + req.playbook.name = req.body.name; + req.playbook.location = req.body.location; + + if (req.body.vault_password.length > 0) { + req.playbook.vault_password = req.body.vault_password; + } + + try { + req.playbook.credential = mongoose.Types.ObjectId(req.body.credential); + } catch (e) {} + + req.playbook.save(); + res.send(201); +} + +function remove (req, res) { + req.playbook.remove(function (err) { + res.send(201); + }) +} \ No newline at end of file diff --git a/lib/routes/playbook/playbooks.js b/lib/routes/playbook/playbooks.js index 4754f4e2..58a3cd16 100644 --- a/lib/routes/playbook/playbooks.js +++ b/lib/routes/playbook/playbooks.js @@ -1,4 +1,7 @@ var models = require('../../models') +var mongoose = require('mongoose') + +var playbook = require('./playbook') exports.unauthorized = function (app, template) { template([ @@ -6,19 +9,39 @@ exports.unauthorized = function (app, template) { ], { prefix: 'playbook' }); + + playbook.unauthorized(app, template); } -exports.httpRouter = function () { - +exports.httpRouter = function (app) { + playbook.httpRouter(app); } exports.router = function (app) { app.get('/playbooks', getPlaybooks) + .post('/playbooks', addPlaybook) + + playbook.router(app); } function getPlaybooks (req, res) { models.Playbook.find({ - }, function (err, playbooks) { + }).sort('-created').exec(function (err, playbooks) { res.send(playbooks) }) +} + +function addPlaybook (req, res) { + var playbook = new models.Playbook({ + name: req.body.name, + location: req.body.location, + vault_password: req.body.vault_password + }) + try { + playbook.credential = mongoose.Types.ObjectId(req.body.credential) + } catch (e) {} + + playbook.save(function () { + res.send(201); + }) } \ No newline at end of file diff --git a/lib/views/abstract.jade b/lib/views/abstract.jade new file mode 100644 index 00000000..f31be5cc --- /dev/null +++ b/lib/views/abstract.jade @@ -0,0 +1 @@ +ui-view(autoscroll="false") \ No newline at end of file diff --git a/lib/views/layout.jade b/lib/views/layout.jade index 34597b35..b339fc64 100644 --- a/lib/views/layout.jade +++ b/lib/views/layout.jade @@ -22,7 +22,7 @@ html button.btn.btn-block.btn-default(ui-sref="addPlaybook") Add Playbook li(ng-repeat="playbook in playbooks") - a(ui-sref="playbook({ pid: playbook._id })") {{ playbook.name }} + a(ui-sref="playbook.view({ playbook_id: playbook._id })") {{ playbook.name }} .col-sm-9.col-lg-10 block content diff --git a/lib/views/playbook/add.jade b/lib/views/playbook/add.jade index cf33d3dd..d261aadc 100644 --- a/lib/views/playbook/add.jade +++ b/lib/views/playbook/add.jade @@ -1 +1,21 @@ -h1 Add Playbook \ No newline at end of file +h1 Add Playbook + +form.form-horizontal + .form-group + label.control-label.col-sm-4 Name + .col-sm-7 + input.form-control(type="text" placeholder="Playbook Name" ng-model="playbook.data.name") + + .form-group + label.control-label.col-sm-4 Location + .col-sm-7 + input.form-control(type="text" placeholder="Git Location" ng-model="playbook.data.location") + + .form-group + label.control-label.col-sm-4 Vault Password + .col-sm-7 + input.form-control(type="text" placeholder="Vault Password" ng-model="playbook.data.vault_password") + + .form-group + .col-sm-7.col-sm-offset-4 + button.btn.btn-default(ng-click="playbook.add()") Add \ No newline at end of file diff --git a/lib/views/playbook/view.jade b/lib/views/playbook/view.jade new file mode 100644 index 00000000..33b5d06c --- /dev/null +++ b/lib/views/playbook/view.jade @@ -0,0 +1,3 @@ +h1 {{ playbook.data.name }} + +pre: code: {{ playbook }} \ No newline at end of file diff --git a/public/js/controllers/playbook/add.js b/public/js/controllers/playbook/add.js new file mode 100644 index 00000000..72f70e93 --- /dev/null +++ b/public/js/controllers/playbook/add.js @@ -0,0 +1,8 @@ +define([ + 'app', + 'factories/playbook' +], function(app) { + app.registerController('AddPlaybookCtrl', ['$scope', 'Playbook', function($scope, Playbook) { + $scope.playbook = new Playbook(); + }]); +}); \ No newline at end of file diff --git a/public/js/controllers/playbook/playbook.js b/public/js/controllers/playbook/playbook.js new file mode 100644 index 00000000..46d59b3e --- /dev/null +++ b/public/js/controllers/playbook/playbook.js @@ -0,0 +1,7 @@ +define([ + 'app' +], function(app) { + app.registerController('PlaybookCtrl', ['$scope', function($scope) { + console.log($scope.playbook); + }]); +}); \ No newline at end of file diff --git a/public/js/factories/playbook.js b/public/js/factories/playbook.js new file mode 100644 index 00000000..cc8c1af7 --- /dev/null +++ b/public/js/factories/playbook.js @@ -0,0 +1,40 @@ +define(['app'], function (app) { + app.registerFactory('Playbook', ['$http', function ($http) { + var Playbook = function (id, cb) { + if (!id) { + return; + } + + this.id = id; + + this.get(cb); + } + + Playbook.prototype.save = function () { + $http.put('/playbook/'+this.data._id, this.data); + } + + Playbook.prototype.add = function () { + $http.post('/playbooks', this.data); + } + + Playbook.prototype.delete = function () { + $http.delete('/playbook/'+this.data._id, this.data); + } + + Playbook.prototype.get = function (cb) { + var self = this; + + $http.get('/playbook/'+this.id) + .success(function (data, status) { + self.data = data; + cb(); + }) + .error(function (data, status) { + cb(data, status); + }) + } + + return Playbook; + }]) +}) \ No newline at end of file diff --git a/public/js/routes/playbooks.js b/public/js/routes/playbooks.js index e670a99e..11bf344c 100644 --- a/public/js/routes/playbooks.js +++ b/public/js/routes/playbooks.js @@ -1,12 +1,46 @@ define([ - 'app' + 'app', + 'factories/playbook' ], function(app) { - app.config(function($stateProvider) { + app.config(function($stateProvider, $couchPotatoProvider) { $stateProvider .state('addPlaybook', { url: '/add', pageTitle: 'Add Playbook', - templateUrl: "/view/playbook/add" + templateUrl: "/view/playbook/add", + controller: 'AddPlaybookCtrl', + resolve: { + dummy: $couchPotatoProvider.resolve(['controllers/playbook/add']) + } + }) + + .state('playbook', { + abstract: true, + url: '/playbook/:playbook_id', + templateUrl: '/view/abstract', + controller: function ($scope, playbook) { + $scope.playbook = playbook; + }, + resolve: { + playbook: function (Playbook, $stateParams, $q) { + var deferred = $q.defer(); + + var playbook = new Playbook($stateParams.playbook_id, function (err, errStatus) { + deferred.resolve(playbook); + }); + + return deferred.promise; + } + } + }) + + .state('playbook.view', { + url: '/', + controller: 'PlaybookCtrl', + templateUrl: '/view/playbook/view', + resolve: { + dummy: $couchPotatoProvider.resolve(['controllers/playbook/playbook']) + } }) }) }) \ No newline at end of file