-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JRD-331] Routes, controllers, bo and ipe-client added
- Loading branch information
Flamínio Maranhão
committed
Sep 11, 2015
1 parent
1e5a02a
commit 61c5b81
Showing
4 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
var CJM = require('carbono-json-messages'); | ||
var uuid = require('node-uuid'); | ||
var bo = require('../lib/mission-control-bo'); | ||
|
||
module.exports = function (app, etcdManager) { | ||
|
||
this.createContainer = function(req, res, next) { | ||
|
||
var projectId = req.body.projectId; | ||
var machineAlias = req.body.machineAlias; | ||
|
||
// Check data consistency | ||
// @todo Create an definitive helper for handling errors. | ||
// There are too much repeated code in the repositories. | ||
if (!projectId || !machineAlias) { | ||
var cjm = new CJM({apiVersion: '1.0'}); | ||
res.status(400); | ||
var error = { | ||
code: 400, | ||
message: 'Missing mandatory parameters', | ||
errors: 'projectId and machineAlias should have an valid value', | ||
}; | ||
cjm.setError(error); | ||
res.json(cjm); | ||
res.end(); | ||
return; | ||
}; | ||
|
||
// Get the IPE module URL | ||
var ipeURL = etcdManager.getIpeUrl(); | ||
if (!ipeURL) { | ||
var cjm = new CJM({apiVersion: '1.0'}); | ||
var error = { | ||
code: 500, | ||
message: 'Internal data is missing', | ||
errors: 'ipeURL is not set in etcdManager, I can\'t send the request!' | ||
}; | ||
res.status(500); | ||
cjm.setError(error); | ||
res.json(cjm); | ||
res.end(); | ||
return; | ||
}; | ||
|
||
// Now we can make the call to the business object. | ||
bo.createUserContainer(ipeURL, projectId, machineAlias, | ||
/** | ||
* Callback function | ||
*/ | ||
function (err, ret) { | ||
var cjm = new CJM({apiVersion: '1.0'}); | ||
try { | ||
if (err) { | ||
res.status(400); | ||
var error = { | ||
code: 400, | ||
message: 'Could not create container', | ||
errors: err, | ||
}; | ||
cjm.setError(error); | ||
} else { | ||
// Probably an empty answer, but let's send it anyway | ||
cjm.setData( | ||
{ | ||
id: uuid.v4(), | ||
items: [ret], | ||
} | ||
); | ||
} | ||
res.json(cjm); | ||
res.end(); | ||
} catch (e) { | ||
// Unknown error... | ||
var error = { | ||
code: 500, | ||
message: 'Unknown internal error', | ||
errors: e, | ||
}; | ||
cjm.setError(error); | ||
res.json(cjm); | ||
res.status(500).end(); | ||
} | ||
} | ||
); | ||
}; | ||
|
||
return this; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
var request = require('request'); | ||
var CJM = require('carbono-json-messages'); | ||
|
||
/** | ||
* Creates and/or retrieves an docker instance and | ||
* returns a callback function. | ||
* | ||
* @todo Parse errors and throw them. | ||
* @todo Use carbono-json-messages | ||
* | ||
* @param {string} ipeURL - URL retrieved from etcd. | ||
* @param {string} proj - Id of the project | ||
* @param {onbject} cb - Callback function that receives errors and/or | ||
* other information | ||
* | ||
* @return function (err, res) | ||
*/ | ||
exports.create = function (ipeURL, proj, alias, cb) { | ||
if (ipeURL) { | ||
var url = ipeURL + '/container'; | ||
var headers = { | ||
'Content-Type': 'application/json', | ||
}; | ||
var aux = { | ||
apiVersion: '1.0', | ||
id: 'xyzzy-wyzzy', | ||
data: { | ||
id: '1337', | ||
items: | ||
[ | ||
{ | ||
projectId: proj, | ||
machineAlias: alias | ||
}, | ||
], | ||
}, | ||
}; | ||
var load = { | ||
url: url, | ||
headers: headers, | ||
json: aux, | ||
}; | ||
|
||
/** | ||
* Callback function that tries to parse (JSON) the body object | ||
* | ||
* @param {Object} err - Error object | ||
* @param {Object} httpResponse - Response object | ||
* @param {string} body - String containing a JSON structure | ||
*/ | ||
var _cb = function (err, httpResponse, body) { | ||
try { | ||
body = JSON.parse(body); | ||
} catch (err) { | ||
this.err = err; | ||
} | ||
cb(err, body); | ||
}; | ||
request.post(load, _cb); | ||
|
||
} else { | ||
// If there is no IPE reference, nothing can be done. Sends an Error. | ||
var cjm = new CJM({apiVersion: '1.0'}); | ||
try { | ||
var err = { | ||
code: 400, | ||
message: 'ipeURL is required for this operation', | ||
}; | ||
cjm.setError(err); | ||
cb(cjm, null); | ||
} catch (e) { | ||
cb(e, null); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
module.exports = function (app) { | ||
var infra = app.controllers.infra; | ||
|
||
app.get('/createContainer', infra.createContainer); | ||
|
||
return this; | ||
}; |