Skip to content

Commit

Permalink
Better snapshots implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Panagis Tselentis committed Dec 28, 2018
1 parent fa24e63 commit 3fc42ad
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 17 deletions.
31 changes: 25 additions & 6 deletions api/controllers/SnapshotController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

var KongService = require('../services/KongService');
var SnapshotsService = require('../services/SnapshotsService');
var _ = require('lodash')
var async = require('async');
var fs = require('fs');
var semver = require('semver');
const KongService = require('../services/KongService');
const SnapshotsService = require('../services/SnapshotsService');
const _ = require('lodash')
const async = require('async');
const fs = require('fs');
const semver = require('semver');

module.exports = _.merge(_.cloneDeep(require('../base/Controller')), {

Expand All @@ -28,6 +28,25 @@ module.exports = _.merge(_.cloneDeep(require('../base/Controller')), {
});
},

snapshot: async (req, res) => {
// Get node
sails.models.kongnode.findOne({
id: req.param("node_id")
}).exec(async (err, node) => {
if (err) return res.negotiate(err)
if (!node) return res.badRequest({
message: "Invalid Kong Node"
})
try {
const snapshot = await SnapshotsService.snapshot(req.param('name'), node);
return res.json(snapshot);
} catch (e) {
return res.negotiate(e)
}

});
},

takeSnapShot: function (req, res) {


Expand Down
75 changes: 75 additions & 0 deletions api/services/SnapshotsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,81 @@ var Utils = require('../helpers/utils');

module.exports = {

snapshot: async(snapshotName, node) => {
const entities = {
'services': [],
'routes': [],
'consumers': [],
'plugins': [],
'acls': [],
'upstreams': [],
'certificates': [],
'snis': []
};
const req = {
connection: node
}

const consumersCredentials = {
'basic-auths': [],
'key-auths': [],
'hmac-auths': [],
'jwts': [],
'oauth2': []
}

try {
// Gather entities
for(let entity in entities) {
const result = await KongService.fetch(`/${entity}`, req);
entities[entity] = result.data;

// For each upstream, fetch it's targets
if(entity === 'upstreams' && result.data.length) {
entities[entity].forEach(async (upstream) => {
const targets = await KongService.fetch(`/upstreams/${upstream.id}/targets`, req);
upstream.targets = targets.data;
})
}
}

// Gather consumer credentials
for(let credential in consumersCredentials) {
const result = await KongService.fetch(`/${credential}`, req);
consumersCredentials[credential] = result.data;
}

// Assign credentials to consumers
if(entities.consumers && entities.consumers.length) {
entities.consumers.forEach(consumer => {
consumer.credentials = {};
for(let key in consumersCredentials) {
consumer.credentials[key] = _.filter(consumersCredentials[key], (item) => {
return consumer.id === _.get(item, 'consumer.id');
})
}
})
}

sails.log("SnapshotController:snapshot:entities", entities);

// Create the actual snapshot
return new Promise((resolve, reject) => {
sails.models.snapshot.create({
name: snapshotName || "snap@" + Date.now(),
kong_node_name: node.name,
kong_node_url: Utils.withoutTrailingSlash(node.kong_admin_url),
kong_version: node.kong_version,
data: entities
}).exec(function (err, created) {
if (err) return reject(err);
return resolve(created);
});
})
} catch(err) {
throw err;
}
},

takeSnapShot: function (name, node, cb) {

Expand Down
38 changes: 27 additions & 11 deletions assets/js/app/snapshots/controllers/snapshots-list-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,38 @@


$scope.submitting = true;
SnapshotsService.takeSnapshot($scope.snapshot.name,$scope.node.id)
.then(function(response){
MessageService.success('Snapshot creation may take some time. You will get notified when the job is done!');
$scope.submitting = false;
// $uibModalInstance.dismiss({
// result : response.data
// })
$uibModalInstance.dismiss({
result : response.data
});
}).catch(function(err){
SnapshotsService.snapshot($scope.snapshot.name,$scope.node.id)
.then(function(response){
MessageService.success('Snapshot creation may take some time. You will get notified when the job is done!');
$scope.submitting = false;
// $uibModalInstance.dismiss({
// result : response.data
// })
$uibModalInstance.dismiss({
result : response.data
});
}).catch(function(err){
$scope.submitting = false;
if(err.data && err.data.message) {
$scope.error = err.data.message;
}
});
// SnapshotsService.takeSnapshot($scope.snapshot.name,$scope.node.id)
// .then(function(response){
// MessageService.success('Snapshot creation may take some time. You will get notified when the job is done!');
// $scope.submitting = false;
// // $uibModalInstance.dismiss({
// // result : response.data
// // })
// $uibModalInstance.dismiss({
// result : response.data
// });
// }).catch(function(err){
// $scope.submitting = false;
// if(err.data && err.data.message) {
// $scope.error = err.data.message;
// }
// });
};
}
});
Expand Down
6 changes: 6 additions & 0 deletions assets/js/app/snapshots/snapshots-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
node_id : node_id
});
},
snapshot : function(name,node_id) {
return $http.post('api/snapshots/snapshot',{
name : name,
node_id : node_id
});
},
restoreSnapshot : function(id,imports) {
return $http.post('api/snapshots/' + id + '/restore',{
imports : imports
Expand Down
1 change: 1 addition & 0 deletions config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module.exports.policies = {
SnapshotController: {
'*': ['authenticated'],
'takeSnapShot': ['authenticated', 'isAdmin', 'dynamicNode', 'createUser'],
'snapshot': ['authenticated', 'isAdmin', 'dynamicNode', 'createUser'],
'restore': ['authenticated', 'isAdmin', 'dynamicNode'],
'remove': ['authenticated', 'isAdmin'],
'find': ['authenticated', 'isAdmin']
Expand Down
1 change: 1 addition & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module.exports.routes = {

// Snapshots
'POST /api/snapshots/take': 'SnapshotController.takeSnapShot',
'POST /api/snapshots/snapshot': 'SnapshotController.snapshot',
'GET /api/snapshots/subscribe': 'SnapshotController.subscribe',
'POST /api/snapshots/:id/restore': 'SnapshotController.restore',
'GET /api/snapshots/:id/download': 'SnapshotController.download',
Expand Down

0 comments on commit 3fc42ad

Please sign in to comment.