http statusCode support

- use express instead of connect
- Return http request status code from shovel proxies

Change-Id: I562c8c738f3e383eb85034469a61ae1f462ae8cd
This commit is contained in:
Andre Keedy 2016-03-08 14:46:34 -05:00
parent 50ec0f2000
commit 3a9e73320c
7 changed files with 86 additions and 14 deletions

View File

@ -45,12 +45,14 @@ module.exports.driversGet = function driversGet(req, res) {
return ironic.get_driver_list(token); return ironic.get_driver_list(token);
}). }).
then(function (result) { then(function (result) {
res.status(ironic.getStatus());
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(result); res.end(result);
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -70,11 +72,13 @@ module.exports.ironicnodesGet = function ironicnodesGet(req, res) {
}). }).
then(function (result) { then(function (result) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(ironic.getStatus());
res.end(result); res.end(result);
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -94,11 +98,13 @@ module.exports.ironicchassisGet = function ironicchassisGet(req, res) {
}). }).
then(function (result) { then(function (result) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(ironic.getStatus());
res.end(result); res.end(result);
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -118,11 +124,13 @@ module.exports.ironicnodeGet = function ironicnodeGet(req, res) {
}). }).
then(function (result) { then(function (result) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(ironic.getStatus());
res.end(result); res.end(result);
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -143,11 +151,13 @@ module.exports.ironicnodePatch = function ironicnodePatch(req, res) {
}). }).
then(function (result) { then(function (result) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(ironic.getStatus());
res.end(result); res.end(result);
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); 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). return monorail.request_catalogs_get(req.swagger.params.identifier.value).
then(function (catalogs) { then(function (catalogs) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(monorail.getStatus());
res.end(catalogs); res.end(catalogs);
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -182,11 +194,13 @@ module.exports.catalogsbysourceGet = function catalogsbysourceGet(req, res) {
req.swagger.params.source.value). req.swagger.params.source.value).
then(function (catalogs) { then(function (catalogs) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(monorail.getStatus());
res.end(catalogs); res.end(catalogs);
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); 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). return monorail.request_node_get(req.swagger.params.identifier.value).
then(function (node) { then(function (node) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(monorail.getStatus());
res.end(node); res.end(node);
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -224,11 +240,13 @@ module.exports.nodesGet = function nodesGet(req, res) {
}) })
.then(function (discoveredNodes) { .then(function (discoveredNodes) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(monorail.getStatus());
res.end(JSON.stringify(discoveredNodes)); res.end(JSON.stringify(discoveredNodes));
}); });
}) })
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.status(500);
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(err)); 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) return monorail.request_poller_data_get(sel[0].id)
.then(function (data) { .then(function (data) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(monorail.getStatus());
res.end(data); res.end(data);
}); });
} }
@ -261,6 +280,7 @@ module.exports.getSeldata = function getSeldata(req, res,next) {
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -419,6 +439,7 @@ module.exports.registerpost = function registerpost(req, res) {
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -460,6 +481,7 @@ module.exports.unregisterdel = function unregisterdel(req, res) {
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -475,6 +497,7 @@ module.exports.configsetmono = function configsetmono(req, res) {
if (setConfig('monorail', req.body)) { if (setConfig('monorail', req.body)) {
res.end('success'); res.end('success');
} else { } else {
res.status(500);
res.end('failed to update monorail config'); res.end('failed to update monorail config');
} }
}; };
@ -490,6 +513,7 @@ module.exports.configsetkeystone = function configsetkeystone(req, res) {
if (setConfig('keystone', req.body)) { if (setConfig('keystone', req.body)) {
res.end('success'); res.end('success');
} else { } else {
res.status(500);
res.end('failed to update keystone config'); res.end('failed to update keystone config');
} }
}; };
@ -515,6 +539,7 @@ module.exports.configsetironic = function configsetironic(req, res) {
if (setConfig('ironic', req.body)) { if (setConfig('ironic', req.body)) {
res.end('success'); res.end('success');
} else { } else {
res.status(500);
res.end('failed to update ironic config'); res.end('failed to update ironic config');
} }
}; };
@ -540,6 +565,7 @@ module.exports.configsetglance = function configsetglance(req, res) {
if (setConfig('glance', req.body)) { if (setConfig('glance', req.body)) {
res.end('success'); res.end('success');
} else { } else {
res.status(500);
res.end('failed to update glance config'); res.end('failed to update glance config');
} }
}; };
@ -555,6 +581,7 @@ module.exports.configset = function configset(req, res) {
if (setConfig('shovel', req.body) === true) { if (setConfig('shovel', req.body) === true) {
res.end('success'); res.end('success');
} else { } else {
res.status(500);
res.end('failed to update shovel config'); res.end('failed to update shovel config');
} }
}; };
@ -604,6 +631,7 @@ module.exports.configget = function configget(req, res) {
} catch (err) { } catch (err) {
logger.error(err); logger.error(err);
res.setHeader('content-type', 'text/plain'); res.setHeader('content-type', 'text/plain');
res.status(500);
res.end('failed to get config'); res.end('failed to get config');
} }
}); });
@ -628,6 +656,7 @@ module.exports.imagesGet = function imagesGet(req, res) {
.catch(function (err) { .catch(function (err) {
logger.error({ message: err, path: req.url }); logger.error({ message: err, path: req.url });
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -641,9 +670,11 @@ module.exports.deployOS = function deployOS(req, res) {
return monorail.runWorkFlow(req.swagger.params.identifier.value, return monorail.runWorkFlow(req.swagger.params.identifier.value,
req.body.name,req.body) req.body.name,req.body)
.then(function(result) { .then(function(result) {
res.status(monorail.getStatus());
res.end(result); res.end(result);
}) })
.catch(function(err) { .catch(function(err) {
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -656,13 +687,15 @@ module.exports.workflowStatus = function workflowStatus(req,res) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
return monorail.getWorkFlowActive(req.swagger.params.identifier.value) return monorail.getWorkFlowActive(req.swagger.params.identifier.value)
.then(function(data) { .then(function(data) {
if (data) { res.status(monorail.getStatus());
if (monorail.getStatus() === 200 && data) {
res.end(JSON.stringify({jobStatus:'Running'})); res.end(JSON.stringify({jobStatus:'Running'}));
} else { } else {
res.end(JSON.stringify({jobStatus:'Currently there is no job running on this node'})); res.end(JSON.stringify({jobStatus:'Currently there is no job running on this node'}));
} }
}) })
.catch(function(err) { .catch(function(err) {
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };
@ -727,9 +760,11 @@ module.exports.runAnsible = function runAnsible(req, res) {
'Graph.Ansible.' + req.body.name,null); 'Graph.Ansible.' + req.body.name,null);
}) })
.then(function(result) { .then(function(result) {
res.status(monorail.getStatus());
res.end(result); res.end(result);
}) })
.catch(function(err) { .catch(function(err) {
res.status(500);
res.end(JSON.stringify(err)); res.end(JSON.stringify(err));
}); });
}; };

View File

@ -2,10 +2,14 @@
/*eslint-env node*/ /*eslint-env node*/
//global
var statusCode;
/* http client */ /* http client */
var HttpClient = { var HttpClient = {
Get: function (msg, output) { Get: function (msg, output) {
'use strict'; 'use strict';
statusCode = 0;
var http = require('http'); var http = require('http');
var options = { var options = {
hostname: msg.host, hostname: msg.host,
@ -25,9 +29,11 @@ var HttpClient = {
}); });
response.on('error', function (err) { response.on('error', function (err) {
var errorMessage = { errorMessage: { hostname: msg.host, message: err } }; var errorMessage = { errorMessage: { hostname: msg.host, message: err } };
statusCode = response.statusCode;
output(errorMessage); output(errorMessage);
}); });
response.on('end', function () { response.on('end', function () {
statusCode = response.statusCode;
output(null, body); output(null, body);
}); });
}; };
@ -42,6 +48,7 @@ var HttpClient = {
}, },
Post: function (msg, output) { Post: function (msg, output) {
'use strict'; 'use strict';
statusCode = 0;
var http = require('http'); var http = require('http');
var options = { var options = {
hostname: msg.host, hostname: msg.host,
@ -70,10 +77,12 @@ var HttpClient = {
body += chunk; body += chunk;
}); });
response.on('error', function (e) { response.on('error', function (e) {
statusCode = response.statusCode;
var errorMessage = { errorMessage: { hostname: msg.host, message: e } }; var errorMessage = { errorMessage: { hostname: msg.host, message: e } };
output(errorMessage); output(errorMessage);
}); });
response.on('end', function () { response.on('end', function () {
statusCode = response.statusCode;
output(null, body); output(null, body);
}); });
}; };
@ -91,6 +100,7 @@ var HttpClient = {
}, },
Delete: function (msg, output) { Delete: function (msg, output) {
'use strict'; 'use strict';
statusCode = 0;
var http = require('http'); var http = require('http');
var options = { var options = {
hostname: msg.host, hostname: msg.host,
@ -114,10 +124,12 @@ var HttpClient = {
body += chunk; body += chunk;
}); });
response.on('error', function (err) { response.on('error', function (err) {
statusCode = response.statusCode;
var errorMessage = { errorMessage: { hostname: msg.host, message: err } }; var errorMessage = { errorMessage: { hostname: msg.host, message: err } };
output(errorMessage); output(errorMessage);
}); });
response.on('end', function () { response.on('end', function () {
statusCode = response.statusCode;
output(null, body); output(null, body);
}); });
}; };
@ -132,6 +144,7 @@ var HttpClient = {
}, },
Put: function (msg, output) { Put: function (msg, output) {
'use strict'; 'use strict';
statusCode = 0;
var http = require('http'); var http = require('http');
var options = { var options = {
hostname: msg.host, hostname: msg.host,
@ -160,10 +173,12 @@ var HttpClient = {
body += chunk; body += chunk;
}); });
response.on('error', function (e) { response.on('error', function (e) {
statusCode = response.statusCode;
var errorMessage = { errorMessage: { hostname: msg.host, message: e } }; var errorMessage = { errorMessage: { hostname: msg.host, message: e } };
output(errorMessage); output(errorMessage);
}); });
response.on('end', function () { response.on('end', function () {
statusCode = response.statusCode;
output(null, body); output(null, body);
}); });
}; };
@ -181,6 +196,7 @@ var HttpClient = {
}, },
Patch: function (msg, output) { Patch: function (msg, output) {
'use strict'; 'use strict';
statusCode = 0;
var http = require('http'); var http = require('http');
var options = { var options = {
hostname: msg.host, hostname: msg.host,
@ -209,10 +225,12 @@ var HttpClient = {
body += chunk; body += chunk;
}); });
response.on('error', function (err) { response.on('error', function (err) {
statusCode = response.statusCode;
var errorMessage = { errorMessage: { hostname: msg.host, message: err } }; var errorMessage = { errorMessage: { hostname: msg.host, message: err } };
output(errorMessage); output(errorMessage);
}); });
response.on('end', function () { response.on('end', function () {
statusCode = response.statusCode;
output(null, body); output(null, body);
}); });
}; };
@ -227,6 +245,10 @@ var HttpClient = {
request.write(msg.data); request.write(msg.data);
} }
request.end(); request.end();
},
getStatus: function() {
'use strict';
return statusCode;
} }
}; };
module.exports = Object.create(HttpClient); module.exports = Object.create(HttpClient);

View File

@ -16,7 +16,6 @@ var request = {
data: '', data: '',
api: '' api: ''
}; };
/* /*
* Monorail wrapper functions * Monorail wrapper functions
*/ */
@ -175,6 +174,10 @@ var MonorailWrapper = {
request.path = pfx + '/workflows'; request.path = pfx + '/workflows';
request.data = JSON.stringify(content); request.data = JSON.stringify(content);
return client.PutAsync(request); return client.PutAsync(request);
},
getStatus: function () {
'use strict';
return client.getStatus();
} }
}; };
module.exports = Object.create(MonorailWrapper); module.exports = Object.create(MonorailWrapper);

View File

@ -24,6 +24,10 @@ var glanceWrapper = {
request.token = token; request.token = token;
request.path = pfx + '/images'; request.path = pfx + '/images';
return client.GetAsync(request); return client.GetAsync(request);
},
getStatus: function () {
'use strict';
return client.getStatus();
} }
}; };
module.exports = Object.create(glanceWrapper); module.exports = Object.create(glanceWrapper);

View File

@ -103,6 +103,10 @@ var ironicWrapper = {
request.token = token; request.token = token;
request.path = pfx + '/drivers'; request.path = pfx + '/drivers';
return client.GetAsync(request); return client.GetAsync(request);
},
getStatus: function () {
'use strict';
return client.getStatus();
} }
}; };

View File

@ -57,6 +57,10 @@ var KeystoneAuthentication = {
} }
}); });
return client.PostAsync(request); return client.PostAsync(request);
},
getStatus: function () {
'use strict';
return client.getStatus();
} }
}; };
module.exports = Object.create(KeystoneAuthentication); module.exports = Object.create(KeystoneAuthentication);

View File

@ -515,7 +515,7 @@ describe('****SHOVEL API Interface****', function () {
.post('/api/1.1/register') .post('/api/1.1/register')
.send(body) .send(body)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -539,7 +539,7 @@ describe('****SHOVEL API Interface****', function () {
request(url) request(url)
.patch('/api/1.1/ironic/node/123') .patch('/api/1.1/ironic/node/123')
.send([{}]) .send([{}])
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -552,7 +552,7 @@ describe('****SHOVEL API Interface****', function () {
request(url) request(url)
.post('/api/1.1/deployos/123') .post('/api/1.1/deployos/123')
.send([{}]) .send([{}])
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -564,7 +564,7 @@ describe('****SHOVEL API Interface****', function () {
it('/worflow-status/{identifier} should return error message', function (done) { it('/worflow-status/{identifier} should return error message', function (done) {
request(url) request(url)
.get('/api/1.1/worflow-status/123') .get('/api/1.1/worflow-status/123')
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -579,7 +579,7 @@ describe('****SHOVEL API Interface****', function () {
.send({name: 'runExample',vars: {}, .send({name: 'runExample',vars: {},
playbookPath: 'main.yml' playbookPath: 'main.yml'
}) })
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -629,7 +629,7 @@ describe('****SHOVEL API Interface****', function () {
.post('/api/1.1/register') .post('/api/1.1/register')
.send(body) .send(body)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -646,7 +646,7 @@ describe('****SHOVEL API Interface****', function () {
.post('/api/1.1/register') .post('/api/1.1/register')
.send(body) .send(body)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -664,7 +664,7 @@ describe('****SHOVEL API Interface****', function () {
.post('/api/1.1/register') .post('/api/1.1/register')
.send(body) .send(body)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -682,7 +682,7 @@ describe('****SHOVEL API Interface****', function () {
.post('/api/1.1/register') .post('/api/1.1/register')
.send(body) .send(body)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -701,7 +701,7 @@ describe('****SHOVEL API Interface****', function () {
.post('/api/1.1/register') .post('/api/1.1/register')
.send(body) .send(body)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -721,7 +721,7 @@ describe('****SHOVEL API Interface****', function () {
.post('/api/1.1/register') .post('/api/1.1/register')
.send(body) .send(body)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;
@ -742,7 +742,7 @@ describe('****SHOVEL API Interface****', function () {
.post('/api/1.1/register') .post('/api/1.1/register')
.send(body) .send(body)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(500)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
throw err; throw err;