-
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.
Merged in JRD-331-mc-acionar-ipe (pull request #1)
[JRD-331] mc acionar ipe
- Loading branch information
Showing
8 changed files
with
283 additions
and
16 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,94 @@ | ||
'use strict'; | ||
var CJM = require('carbono-json-messages'); | ||
var uuid = require('node-uuid'); | ||
var bo = require('../lib/mission-control-bo'); | ||
var pjson = require('../../package.json'); | ||
|
||
module.exports = function (app, etcdManager) { | ||
|
||
this.createContainer = function (req, res) { | ||
|
||
var projectId = req.body.projectId; | ||
var machineAlias = req.body.machineAlias; | ||
var cjm = null; | ||
var error = null; | ||
|
||
// Check data consistency | ||
// @todo Create an definitive helper for handling errors. | ||
// There are too much repeated code in the repositories. | ||
if (!projectId || !machineAlias) { | ||
cjm = new CJM({apiVersion: pjson.version}); | ||
res.status(400); | ||
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) { | ||
cjm = new CJM({apiVersion: pjson.version}); | ||
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: pjson.version}); | ||
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
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,77 @@ | ||
'use strict'; | ||
var request = require('request'); | ||
var CJM = require('carbono-json-messages'); | ||
var pjson = require('../../package.json'); | ||
|
||
/** | ||
* 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: pjson.version, | ||
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: pjson.version}); | ||
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.post('/createContainer', infra.createContainer); | ||
|
||
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
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