This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved to base minimalistic implementation to provide support for all …
…api endpoints
- Loading branch information
Showing
13 changed files
with
103 additions
and
429 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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,66 @@ | ||
exports.createClient = function(options) { | ||
var request = require('request') | ||
, crypto = require('crypto'); | ||
|
||
module.exports = function cloudstack(options) { | ||
if (!options) { | ||
throw Error('Missing parameter "options".') | ||
options = {}; | ||
} | ||
|
||
return new (require('./cloudstackClient'))(options); | ||
var apiUri = options.apiUri || process.env.CLOUDSTACK_API_URI | ||
, apiKey = options.apiKey || process.env.CLOUDSTACK_API_KEY | ||
, apiSecret = options.apiSecret || process.env.CLOUDSTACK_API_SECRET; | ||
|
||
this.exec = function(cmd, params, callback) { | ||
var paramString = genSignedParamString( | ||
apiKey, | ||
apiSecret, | ||
cmd, | ||
params | ||
); | ||
|
||
var uri = apiUri + '?' + paramString; | ||
request(uri, function(err, res, body) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
var parsedBody = JSON.parse(body); | ||
|
||
if (res.statusCode == 200) { | ||
var result = parsedBody[cmd.toLowerCase() + 'response']; | ||
|
||
return callback(null, result); | ||
} | ||
|
||
// TODO: need all the error condition here | ||
callback(parsedBody); | ||
}); | ||
}; | ||
|
||
var genSignedParamString = function(apiKey, apiSecret, cmd, params) { | ||
params.apiKey = apiKey; | ||
params.command = cmd; | ||
params.response = 'json'; | ||
|
||
var paramKeys = []; | ||
for(var key in params) { | ||
if(params.hasOwnProperty(key)){ | ||
paramKeys.push(key); | ||
}; | ||
}; | ||
|
||
paramKeys.sort(); | ||
|
||
var qsParameters = []; | ||
for(var i = 0; i < paramKeys.length; i++) { | ||
key = paramKeys[i]; | ||
qsParameters.push(key + '=' + encodeURIComponent(params[key])); | ||
} | ||
|
||
var queryString = qsParameters.join('&') | ||
, cryptoAlg = crypto.createHmac('sha1', apiSecret) | ||
, signature = cryptoAlg.update(queryString.toLowerCase()).digest('base64'); | ||
|
||
return queryString + '&signature=' + encodeURIComponent(signature); | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.