From 3a9e73320c2194bcc0a0ba71b2fe6b283c505492 Mon Sep 17 00:00:00 2001 From: Andre Keedy Date: Tue, 8 Mar 2016 14:46:34 -0500 Subject: [PATCH] http statusCode support - use express instead of connect - Return http request status code from shovel proxies Change-Id: I562c8c738f3e383eb85034469a61ae1f462ae8cd --- controllers/Shovel.js | 37 ++++++++++++++++++++++++++++++++++- lib/api/client.js | 22 +++++++++++++++++++++ lib/api/monorail/monorail.js | 5 ++++- lib/api/openstack/glance.js | 4 ++++ lib/api/openstack/ironic.js | 4 ++++ lib/api/openstack/keystone.js | 4 ++++ test/controllers/Shovel.js | 24 +++++++++++------------ 7 files changed, 86 insertions(+), 14 deletions(-) diff --git a/controllers/Shovel.js b/controllers/Shovel.js index 3db0363..7ba3afd 100644 --- a/controllers/Shovel.js +++ b/controllers/Shovel.js @@ -45,12 +45,14 @@ module.exports.driversGet = function driversGet(req, res) { return ironic.get_driver_list(token); }). then(function (result) { + res.status(ironic.getStatus()); res.setHeader('Content-Type', 'application/json'); res.end(result); }) .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -70,11 +72,13 @@ module.exports.ironicnodesGet = function ironicnodesGet(req, res) { }). then(function (result) { res.setHeader('Content-Type', 'application/json'); + res.status(ironic.getStatus()); res.end(result); }) .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -94,11 +98,13 @@ module.exports.ironicchassisGet = function ironicchassisGet(req, res) { }). then(function (result) { res.setHeader('Content-Type', 'application/json'); + res.status(ironic.getStatus()); res.end(result); }) .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -118,11 +124,13 @@ module.exports.ironicnodeGet = function ironicnodeGet(req, res) { }). then(function (result) { res.setHeader('Content-Type', 'application/json'); + res.status(ironic.getStatus()); res.end(result); }) .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -143,11 +151,13 @@ module.exports.ironicnodePatch = function ironicnodePatch(req, res) { }). then(function (result) { res.setHeader('Content-Type', 'application/json'); + res.status(ironic.getStatus()); res.end(result); }) .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -162,11 +172,13 @@ module.exports.catalogsGet = function catalogsGet(req, res) { return monorail.request_catalogs_get(req.swagger.params.identifier.value). then(function (catalogs) { res.setHeader('Content-Type', 'application/json'); + res.status(monorail.getStatus()); res.end(catalogs); }) .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -182,11 +194,13 @@ module.exports.catalogsbysourceGet = function catalogsbysourceGet(req, res) { req.swagger.params.source.value). then(function (catalogs) { res.setHeader('Content-Type', 'application/json'); + res.status(monorail.getStatus()); res.end(catalogs); }) .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -201,11 +215,13 @@ module.exports.nodeGet = function nodeGet(req, res) { return monorail.request_node_get(req.swagger.params.identifier.value). then(function (node) { res.setHeader('Content-Type', 'application/json'); + res.status(monorail.getStatus()); res.end(node); }) .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -224,11 +240,13 @@ module.exports.nodesGet = function nodesGet(req, res) { }) .then(function (discoveredNodes) { res.setHeader('Content-Type', 'application/json'); + res.status(monorail.getStatus()); res.end(JSON.stringify(discoveredNodes)); }); }) .catch(function (err) { logger.error({ message: err, path: req.url }); + res.status(500); res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(err)); }); @@ -252,6 +270,7 @@ module.exports.getSeldata = function getSeldata(req, res,next) { return monorail.request_poller_data_get(sel[0].id) .then(function (data) { res.setHeader('Content-Type', 'application/json'); + res.status(monorail.getStatus()); res.end(data); }); } @@ -261,6 +280,7 @@ module.exports.getSeldata = function getSeldata(req, res,next) { .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -419,6 +439,7 @@ module.exports.registerpost = function registerpost(req, res) { .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -460,6 +481,7 @@ module.exports.unregisterdel = function unregisterdel(req, res) { .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -475,6 +497,7 @@ module.exports.configsetmono = function configsetmono(req, res) { if (setConfig('monorail', req.body)) { res.end('success'); } else { + res.status(500); res.end('failed to update monorail config'); } }; @@ -490,6 +513,7 @@ module.exports.configsetkeystone = function configsetkeystone(req, res) { if (setConfig('keystone', req.body)) { res.end('success'); } else { + res.status(500); res.end('failed to update keystone config'); } }; @@ -515,6 +539,7 @@ module.exports.configsetironic = function configsetironic(req, res) { if (setConfig('ironic', req.body)) { res.end('success'); } else { + res.status(500); res.end('failed to update ironic config'); } }; @@ -540,6 +565,7 @@ module.exports.configsetglance = function configsetglance(req, res) { if (setConfig('glance', req.body)) { res.end('success'); } else { + res.status(500); res.end('failed to update glance config'); } }; @@ -555,6 +581,7 @@ module.exports.configset = function configset(req, res) { if (setConfig('shovel', req.body) === true) { res.end('success'); } else { + res.status(500); res.end('failed to update shovel config'); } }; @@ -604,6 +631,7 @@ module.exports.configget = function configget(req, res) { } catch (err) { logger.error(err); res.setHeader('content-type', 'text/plain'); + res.status(500); res.end('failed to get config'); } }); @@ -628,6 +656,7 @@ module.exports.imagesGet = function imagesGet(req, res) { .catch(function (err) { logger.error({ message: err, path: req.url }); res.setHeader('Content-Type', 'application/json'); + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -641,9 +670,11 @@ module.exports.deployOS = function deployOS(req, res) { return monorail.runWorkFlow(req.swagger.params.identifier.value, req.body.name,req.body) .then(function(result) { + res.status(monorail.getStatus()); res.end(result); }) .catch(function(err) { + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -656,13 +687,15 @@ module.exports.workflowStatus = function workflowStatus(req,res) { res.setHeader('Content-Type', 'application/json'); return monorail.getWorkFlowActive(req.swagger.params.identifier.value) .then(function(data) { - if (data) { + res.status(monorail.getStatus()); + if (monorail.getStatus() === 200 && data) { res.end(JSON.stringify({jobStatus:'Running'})); } else { res.end(JSON.stringify({jobStatus:'Currently there is no job running on this node'})); } }) .catch(function(err) { + res.status(500); res.end(JSON.stringify(err)); }); }; @@ -727,9 +760,11 @@ module.exports.runAnsible = function runAnsible(req, res) { 'Graph.Ansible.' + req.body.name,null); }) .then(function(result) { + res.status(monorail.getStatus()); res.end(result); }) .catch(function(err) { + res.status(500); res.end(JSON.stringify(err)); }); }; diff --git a/lib/api/client.js b/lib/api/client.js index 6502292..cbe0c21 100644 --- a/lib/api/client.js +++ b/lib/api/client.js @@ -2,10 +2,14 @@ /*eslint-env node*/ +//global +var statusCode; + /* http client */ var HttpClient = { Get: function (msg, output) { 'use strict'; + statusCode = 0; var http = require('http'); var options = { hostname: msg.host, @@ -25,9 +29,11 @@ var HttpClient = { }); response.on('error', function (err) { var errorMessage = { errorMessage: { hostname: msg.host, message: err } }; + statusCode = response.statusCode; output(errorMessage); }); response.on('end', function () { + statusCode = response.statusCode; output(null, body); }); }; @@ -42,6 +48,7 @@ var HttpClient = { }, Post: function (msg, output) { 'use strict'; + statusCode = 0; var http = require('http'); var options = { hostname: msg.host, @@ -70,10 +77,12 @@ var HttpClient = { body += chunk; }); response.on('error', function (e) { + statusCode = response.statusCode; var errorMessage = { errorMessage: { hostname: msg.host, message: e } }; output(errorMessage); }); response.on('end', function () { + statusCode = response.statusCode; output(null, body); }); }; @@ -91,6 +100,7 @@ var HttpClient = { }, Delete: function (msg, output) { 'use strict'; + statusCode = 0; var http = require('http'); var options = { hostname: msg.host, @@ -114,10 +124,12 @@ var HttpClient = { body += chunk; }); response.on('error', function (err) { + statusCode = response.statusCode; var errorMessage = { errorMessage: { hostname: msg.host, message: err } }; output(errorMessage); }); response.on('end', function () { + statusCode = response.statusCode; output(null, body); }); }; @@ -132,6 +144,7 @@ var HttpClient = { }, Put: function (msg, output) { 'use strict'; + statusCode = 0; var http = require('http'); var options = { hostname: msg.host, @@ -160,10 +173,12 @@ var HttpClient = { body += chunk; }); response.on('error', function (e) { + statusCode = response.statusCode; var errorMessage = { errorMessage: { hostname: msg.host, message: e } }; output(errorMessage); }); response.on('end', function () { + statusCode = response.statusCode; output(null, body); }); }; @@ -181,6 +196,7 @@ var HttpClient = { }, Patch: function (msg, output) { 'use strict'; + statusCode = 0; var http = require('http'); var options = { hostname: msg.host, @@ -209,10 +225,12 @@ var HttpClient = { body += chunk; }); response.on('error', function (err) { + statusCode = response.statusCode; var errorMessage = { errorMessage: { hostname: msg.host, message: err } }; output(errorMessage); }); response.on('end', function () { + statusCode = response.statusCode; output(null, body); }); }; @@ -227,6 +245,10 @@ var HttpClient = { request.write(msg.data); } request.end(); + }, + getStatus: function() { + 'use strict'; + return statusCode; } }; module.exports = Object.create(HttpClient); diff --git a/lib/api/monorail/monorail.js b/lib/api/monorail/monorail.js index fc506e2..36ed602 100644 --- a/lib/api/monorail/monorail.js +++ b/lib/api/monorail/monorail.js @@ -16,7 +16,6 @@ var request = { data: '', api: '' }; - /* * Monorail wrapper functions */ @@ -175,6 +174,10 @@ var MonorailWrapper = { request.path = pfx + '/workflows'; request.data = JSON.stringify(content); return client.PutAsync(request); + }, + getStatus: function () { + 'use strict'; + return client.getStatus(); } }; module.exports = Object.create(MonorailWrapper); diff --git a/lib/api/openstack/glance.js b/lib/api/openstack/glance.js index 3351b50..71496f4 100644 --- a/lib/api/openstack/glance.js +++ b/lib/api/openstack/glance.js @@ -24,6 +24,10 @@ var glanceWrapper = { request.token = token; request.path = pfx + '/images'; return client.GetAsync(request); + }, + getStatus: function () { + 'use strict'; + return client.getStatus(); } }; module.exports = Object.create(glanceWrapper); diff --git a/lib/api/openstack/ironic.js b/lib/api/openstack/ironic.js index 57425a3..8bf0926 100644 --- a/lib/api/openstack/ironic.js +++ b/lib/api/openstack/ironic.js @@ -103,6 +103,10 @@ var ironicWrapper = { request.token = token; request.path = pfx + '/drivers'; return client.GetAsync(request); + }, + getStatus: function () { + 'use strict'; + return client.getStatus(); } }; diff --git a/lib/api/openstack/keystone.js b/lib/api/openstack/keystone.js index d0eb8ba..a16ff16 100644 --- a/lib/api/openstack/keystone.js +++ b/lib/api/openstack/keystone.js @@ -57,6 +57,10 @@ var KeystoneAuthentication = { } }); return client.PostAsync(request); + }, + getStatus: function () { + 'use strict'; + return client.getStatus(); } }; module.exports = Object.create(KeystoneAuthentication); diff --git a/test/controllers/Shovel.js b/test/controllers/Shovel.js index 3ab824f..4715765 100644 --- a/test/controllers/Shovel.js +++ b/test/controllers/Shovel.js @@ -515,7 +515,7 @@ describe('****SHOVEL API Interface****', function () { .post('/api/1.1/register') .send(body) .expect('Content-Type', /json/) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -539,7 +539,7 @@ describe('****SHOVEL API Interface****', function () { request(url) .patch('/api/1.1/ironic/node/123') .send([{}]) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -552,7 +552,7 @@ describe('****SHOVEL API Interface****', function () { request(url) .post('/api/1.1/deployos/123') .send([{}]) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -564,7 +564,7 @@ describe('****SHOVEL API Interface****', function () { it('/worflow-status/{identifier} should return error message', function (done) { request(url) .get('/api/1.1/worflow-status/123') - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -579,7 +579,7 @@ describe('****SHOVEL API Interface****', function () { .send({name: 'runExample',vars: {}, playbookPath: 'main.yml' }) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -629,7 +629,7 @@ describe('****SHOVEL API Interface****', function () { .post('/api/1.1/register') .send(body) .expect('Content-Type', /json/) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -646,7 +646,7 @@ describe('****SHOVEL API Interface****', function () { .post('/api/1.1/register') .send(body) .expect('Content-Type', /json/) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -664,7 +664,7 @@ describe('****SHOVEL API Interface****', function () { .post('/api/1.1/register') .send(body) .expect('Content-Type', /json/) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -682,7 +682,7 @@ describe('****SHOVEL API Interface****', function () { .post('/api/1.1/register') .send(body) .expect('Content-Type', /json/) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -701,7 +701,7 @@ describe('****SHOVEL API Interface****', function () { .post('/api/1.1/register') .send(body) .expect('Content-Type', /json/) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -721,7 +721,7 @@ describe('****SHOVEL API Interface****', function () { .post('/api/1.1/register') .send(body) .expect('Content-Type', /json/) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err; @@ -742,7 +742,7 @@ describe('****SHOVEL API Interface****', function () { .post('/api/1.1/register') .send(body) .expect('Content-Type', /json/) - .expect(200) + .expect(500) .end(function (err, res) { if (err) { throw err;