Add playbooks via GUI

This commit is contained in:
Matej Kramny 2014-08-25 00:02:34 +01:00
parent 7c466863f2
commit 2e60b91c63
12 changed files with 210 additions and 8 deletions

1
Vagrantfile vendored
View File

@ -5,6 +5,7 @@ VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "hashicorp/precise32" config.vm.box = "hashicorp/precise32"
config.vm.network :forwarded_port, guest: 3000, host: 3000
config.vm.provider "virtualbox" do |vb| config.vm.provider "virtualbox" do |vb|
# vb.gui = true # vb.gui = true

View File

@ -11,6 +11,7 @@ exports.router = function(app) {
]); ]);
templates.add('homepage') templates.add('homepage')
templates.add('abstract')
templates.setup(); templates.setup();
app.get('/', layout); app.get('/', layout);

View File

@ -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);
})
}

View File

@ -1,4 +1,7 @@
var models = require('../../models') var models = require('../../models')
var mongoose = require('mongoose')
var playbook = require('./playbook')
exports.unauthorized = function (app, template) { exports.unauthorized = function (app, template) {
template([ template([
@ -6,19 +9,39 @@ exports.unauthorized = function (app, template) {
], { ], {
prefix: 'playbook' prefix: 'playbook'
}); });
playbook.unauthorized(app, template);
} }
exports.httpRouter = function () { exports.httpRouter = function (app) {
playbook.httpRouter(app);
} }
exports.router = function (app) { exports.router = function (app) {
app.get('/playbooks', getPlaybooks) app.get('/playbooks', getPlaybooks)
.post('/playbooks', addPlaybook)
playbook.router(app);
} }
function getPlaybooks (req, res) { function getPlaybooks (req, res) {
models.Playbook.find({ models.Playbook.find({
}, function (err, playbooks) { }).sort('-created').exec(function (err, playbooks) {
res.send(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);
})
}

1
lib/views/abstract.jade Normal file
View File

@ -0,0 +1 @@
ui-view(autoscroll="false")

View File

@ -22,7 +22,7 @@ html
button.btn.btn-block.btn-default(ui-sref="addPlaybook") Add Playbook button.btn.btn-block.btn-default(ui-sref="addPlaybook") Add Playbook
li(ng-repeat="playbook in playbooks") 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 .col-sm-9.col-lg-10
block content block content

View File

@ -1 +1,21 @@
h1 Add Playbook 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

View File

@ -0,0 +1,3 @@
h1 {{ playbook.data.name }}
pre: code: {{ playbook }}

View File

@ -0,0 +1,8 @@
define([
'app',
'factories/playbook'
], function(app) {
app.registerController('AddPlaybookCtrl', ['$scope', 'Playbook', function($scope, Playbook) {
$scope.playbook = new Playbook();
}]);
});

View File

@ -0,0 +1,7 @@
define([
'app'
], function(app) {
app.registerController('PlaybookCtrl', ['$scope', function($scope) {
console.log($scope.playbook);
}]);
});

View File

@ -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;
}])
})

View File

@ -1,12 +1,46 @@
define([ define([
'app' 'app',
'factories/playbook'
], function(app) { ], function(app) {
app.config(function($stateProvider) { app.config(function($stateProvider, $couchPotatoProvider) {
$stateProvider $stateProvider
.state('addPlaybook', { .state('addPlaybook', {
url: '/add', url: '/add',
pageTitle: 'Add Playbook', 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'])
}
}) })
}) })
}) })