diff --git a/src/routes/volumes.js b/src/routes/volumes.js index 24ee76fad..6a7348f3a 100644 --- a/src/routes/volumes.js +++ b/src/routes/volumes.js @@ -5,7 +5,8 @@ exports = module.exports = { get, del, list, - update + update, + load }; const assert = require('assert'), @@ -15,6 +16,18 @@ const assert = require('assert'), HttpError = require('connect-lastmile').HttpError, HttpSuccess = require('connect-lastmile').HttpSuccess; +function load(req, res, next) { + assert.strictEqual(typeof req.params.id, 'string'); + + volumes.get(req.params.id, function (error, result) { + if (error) return next(BoxError.toHttpError(error)); + + req.resource = result; + + next(); + }); +} + function add(req, res, next) { assert.strictEqual(typeof req.body, 'object'); @@ -32,11 +45,7 @@ function add(req, res, next) { function get(req, res, next) { assert.strictEqual(typeof req.params.id, 'string'); - volumes.get(req.params.id, function (error, result) { - if (error) return next(BoxError.toHttpError(error)); - - next(new HttpSuccess(200, result)); - }); + next(new HttpSuccess(200, req.resource)); } function update(req, res, next) { @@ -46,7 +55,7 @@ function update(req, res, next) { if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be a string')); if (typeof req.body.hostPath !== 'string') return next(new HttpError(400, 'hostPath must be a string')); - volumes.update(req.params.id, req.body, function (error, result) { + volumes.update(req.resource, req.body, function (error, result) { if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(200, result)); @@ -56,7 +65,7 @@ function update(req, res, next) { function del(req, res, next) { assert.strictEqual(typeof req.params.id, 'string'); - volumes.del(req.params.id, auditSource.fromRequest(req), function (error) { + volumes.del(req.resource, auditSource.fromRequest(req), function (error) { if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(204)); diff --git a/src/server.js b/src/server.js index c3d51deb0..6a1d2da76 100644 --- a/src/server.js +++ b/src/server.js @@ -301,10 +301,10 @@ function initializeExpressSync() { // volume routes router.post('/api/v1/volumes', json, token, authorizeAdmin, routes.volumes.add); - router.get ('/api/v1/volumes/:id', token, authorizeAdmin, routes.volumes.get); - router.del ('/api/v1/volumes/:id', token, authorizeAdmin, routes.volumes.del); router.get ('/api/v1/volumes', token, authorizeAdmin, routes.volumes.list); - router.put ('/api/v1/volumes/:id', json, token, authorizeAdmin, routes.volumes.update); + router.get ('/api/v1/volumes/:id', token, authorizeAdmin, routes.volumes.load, routes.volumes.get); + router.del ('/api/v1/volumes/:id', token, authorizeAdmin, routes.volumes.load, routes.volumes.del); + router.put ('/api/v1/volumes/:id', json, token, authorizeAdmin, routes.volumes.load, routes.volumes.update); // addon routes router.get ('/api/v1/services', token, authorizeAdmin, routes.services.getAll); diff --git a/src/test/volumes-test.js b/src/test/volumes-test.js index 4763f11c1..4ab5f1dc2 100644 --- a/src/test/volumes-test.js +++ b/src/test/volumes-test.js @@ -32,7 +32,7 @@ function cleanup(done) { describe('Volumes', function () { before(setup); after(cleanup); - let volumeId; + let volume; it('cannot add bad name', function (done) { volumes.add('music/is', '/tmp/music', AUDIT_SOURCE, function (error) { @@ -52,7 +52,7 @@ describe('Volumes', function () { volumes.add('music', '/mnt/music', AUDIT_SOURCE, function (error, id) { expect(error).to.be(null); expect(id).to.be.a('string'); - volumeId = id; + volume = { id, name: 'music', hostPath: '/mnt/music' }; done(); }); }); @@ -72,9 +72,9 @@ describe('Volumes', function () { }); it('can get volume', function (done) { - volumes.get(volumeId, function (error, volume) { + volumes.get(volume.id, function (error, result) { expect(error).to.be(null); - expect(volume.hostPath).to.be('/mnt/music'); + expect(result.hostPath).to.be('/mnt/music'); done(); }); }); @@ -91,17 +91,17 @@ describe('Volumes', function () { expect(error).to.be(null); expect(result).to.be.an(Array); expect(result.length).to.be(1); - expect(result[0].id).to.be(volumeId); + expect(result[0].id).to.be(volume.id); expect(result[0].hostPath).to.be('/mnt/music'); done(); }); }); it('can update volume', function (done) { - volumes.update(volumeId, { name: 'ratm', hostPath: '/media/music' }, AUDIT_SOURCE, function (error) { + volumes.update(volume, { name: 'ratm', hostPath: '/media/music' }, AUDIT_SOURCE, function (error) { expect(error).to.be(null); - volumes.get(volumeId, function (error, volume) { + volumes.get(volume.id, function (error, volume) { expect(error).to.be(null); expect(volume.name).to.be('ratm'); expect(volume.hostPath).to.be('/media/music'); @@ -118,7 +118,7 @@ describe('Volumes', function () { }); it('can del volume', function (done) { - volumes.del(volumeId, AUDIT_SOURCE, function (error) { + volumes.del(volume, AUDIT_SOURCE, function (error) { expect(error).to.be(null); done(); }); diff --git a/src/volumes.js b/src/volumes.js index 5be9460cd..bc1b0cef5 100644 --- a/src/volumes.js +++ b/src/volumes.js @@ -12,7 +12,8 @@ const assert = require('assert'), BoxError = require('./boxerror.js'), volumedb = require('./volumedb.js'), eventlog = require('./eventlog.js'), - uuid = require('uuid'); + uuid = require('uuid'), + _ = require('underscore'); function validateName(name) { assert.strictEqual(typeof name, 'string'); @@ -74,22 +75,22 @@ function list(callback) { }); } -function del(id, auditSource, callback) { - assert.strictEqual(typeof id, 'string'); +function del(volume, auditSource, callback) { + assert.strictEqual(typeof volume, 'object'); assert.strictEqual(typeof auditSource, 'object'); assert.strictEqual(typeof callback, 'function'); - volumedb.del(id, function (error) { + volumedb.del(volume.id, function (error) { if (error) return callback(error); - eventlog.add(eventlog.ACTION_VOLUME_REMOVE, auditSource, { id }); + eventlog.add(eventlog.ACTION_VOLUME_REMOVE, auditSource, { volume }); return callback(null); }); } -function update(id, data, auditSource, callback) { - assert.strictEqual(typeof id, 'string'); +function update(volume, data, auditSource, callback) { + assert.strictEqual(typeof volume, 'object'); assert.strictEqual(typeof data, 'object'); assert.strictEqual(typeof auditSource, 'object'); assert.strictEqual(typeof callback, 'function'); @@ -100,10 +101,12 @@ function update(id, data, auditSource, callback) { error = validateHostPath(data.hostPath); if (error) return callback(error); - volumedb.update(id, { name: data.name, hostPath: data.hostPath }, function (error) { + volumedb.update(volume.id, { name: data.name, hostPath: data.hostPath }, function (error) { if (error) return callback(error); - eventlog.add(eventlog.ACTION_VOLUME_UPDATE, auditSource, { id, data }); + const newVolume = _.extend({}, volume, data); + + eventlog.add(eventlog.ACTION_VOLUME_UPDATE, auditSource, { volume: newVolume }); return callback(null); });