Last active
August 29, 2015 14:13
-
-
Save felippemr/a14be8d8ee0f862a9ad3 to your computer and use it in GitHub Desktop.
Meteor integration with cloudstack
This file contains hidden or 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
var request = Meteor.npmRequire('request') | |
, crypto = Meteor.npmRequire('crypto'); | |
cloudstack = function (options) { | |
if (!options) { | |
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; | |
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; | |
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) { | |
console.log(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 contains hidden or 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
if (Meteor.isClient) { | |
// counter starts at 0 | |
Session.setDefault('counter', 0); | |
Template.hello.helpers({ | |
counter: function () { | |
return Session.get('counter'); | |
} | |
}); | |
Template.hello.events({ | |
'click button': function () { | |
// increment the counter when button is clicked | |
Session.set('counter', Session.get('counter') + 1); | |
} | |
}); | |
} | |
if (Meteor.isServer) { | |
Meteor.startup(function () { | |
var cs_api = new cloudstack({}); | |
cs_api.exec('listProjects', {}, function(error, result) { | |
for (i in result.project){ | |
project = result.project[i] | |
console.log(project); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment