Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Refactoring client.request to take options and callback
Browse files Browse the repository at this point in the history
  • Loading branch information
kenperkins committed Apr 23, 2013
1 parent c84c226 commit d96f7b1
Show file tree
Hide file tree
Showing 63 changed files with 1,389 additions and 853 deletions.
34 changes: 20 additions & 14 deletions lib/pkgcloud/amazon/compute/client/groups.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ exports.listGroups = function (options, callback) {
var self = this; var self = this;
options = options || {}; options = options || {};


return this.query('DescribeSecurityGroups', options, callback, function (body, res) { return this.query('DescribeSecurityGroups', options, function (err, body, res) {
callback(null, self._toArray(body.securityGroupInfo.item)); return err
? callback(err)
: callback(err, self._toArray(body.securityGroupInfo.item));
}); });
}; };


Expand Down Expand Up @@ -67,9 +69,10 @@ exports.addGroup = function (options, callback) {
GroupName: options.name, GroupName: options.name,
GroupDescription: options.description GroupDescription: options.description
}, },
callback, function (err, body, res) {
function (body, res) { return err
return callback(null, true); ? callback(err)
: callback(null, true);
} }
); );
}; };
Expand All @@ -85,9 +88,10 @@ exports.destroyGroup = function (name, callback) {
return this.query( return this.query(
'DeleteSecurityGroup', 'DeleteSecurityGroup',
{ GroupName: name }, { GroupName: name },
callback, function (err, body, res) {
function (body, res) { return err
return callback(null, true); ? callback(err)
: callback(null, true);
} }
); );
}; };
Expand Down Expand Up @@ -119,9 +123,10 @@ exports.addRules = function (options, callback) {
return this.query( return this.query(
'AuthorizeSecurityGroupIngress', 'AuthorizeSecurityGroupIngress',
rules , rules ,
callback, function (err, body, res) {
function (body, res) { return err
return callback(null, true); ? callback(err)
: callback(null, true);
} }
); );
}; };
Expand Down Expand Up @@ -153,9 +158,10 @@ exports.delRules = function (options, callback) {
return this.query( return this.query(
'RevokeSecurityGroupIngress', 'RevokeSecurityGroupIngress',
rules , rules ,
callback, function (err, body, res) {
function (body, res) { return err
return callback(null, true); ? callback(err)
: callback(null, true);
} }
); );
}; };
27 changes: 18 additions & 9 deletions lib/pkgcloud/amazon/compute/client/images.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ exports.getImages = function getImages(options, callback) {
} }
} }


return this.query('DescribeImages', query, callback, function (body, res) { return this.query('DescribeImages', query, function (err, body, res) {
callback(null, self._toArray(body.imagesSet.item).map(function (image) { return err
return new compute.Image(self, image); ? callback(err)
}), res); : callback(null, self._toArray(body.imagesSet.item).map(function (image) {
return new compute.Image(self, image);
}), res);
}); });
}; };


Expand All @@ -54,7 +56,10 @@ exports.getImage = function getImage(image, callback) {
imageId = image instanceof base.Image ? image.id : image, imageId = image instanceof base.Image ? image.id : image,
query = { 'ImageId.1': imageId }; query = { 'ImageId.1': imageId };


return this.query('DescribeImages', query, callback, function (body, res) { return this.query('DescribeImages', query, function (err, body, res) {
if (err) {
return callback(err);
}
var image = self._toArray(body.imagesSet.item).map(function (image) { var image = self._toArray(body.imagesSet.item).map(function (image) {
return image ? new compute.Image(self, image) : null; return image ? new compute.Image(self, image) : null;
})[0]; })[0];
Expand Down Expand Up @@ -86,8 +91,10 @@ exports.createImage = function createImage(options, callback) {
? options.server.id : options.server, ? options.server.id : options.server,
query = { InstanceId: serverId, Name: options.name }; query = { InstanceId: serverId, Name: options.name };


return this.query('CreateImage', query, callback, function (body, res) { return this.query('CreateImage', query, function (err, body, res) {
self.getImage(res.imageId, callback); return err
? callback(err)
: self.getImage(res.imageId, callback);
}); });
}; };


Expand Down Expand Up @@ -116,8 +123,10 @@ exports.destroyImage = function destroyImage(image, callback) {
snapshotId: image.blockDeviceMapping.item.ebs.snapshotId snapshotId: image.blockDeviceMapping.item.ebs.snapshotId
}; };


self.query('DeleteSnapshot', query, callback, function (body, res) { self.query('DeleteSnapshot', query, function (err, body, res) {
callback(null, { ok: query.snapshotId }, res); return err
? callback(err)
: callback(null, { ok: query.snapshotId }, res);
}); });
}); });
}; };
23 changes: 16 additions & 7 deletions lib/pkgcloud/amazon/compute/client/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


var qs = require('querystring'), var qs = require('querystring'),
utile = require('utile'), utile = require('utile'),
urlJoin = require('url-join'),
xml2js = require('xml2js'), xml2js = require('xml2js'),
auth = require('../../../common/auth'), auth = require('../../../common/auth'),
amazon = require('../../client'); amazon = require('../../client');
Expand All @@ -27,17 +28,21 @@ var Client = exports.Client = function (options) {


utile.inherits(Client, amazon.Client); utile.inherits(Client, amazon.Client);


Client.prototype.query = function query(action, query, errback, callback) { Client.prototype.query = function query(action, query, callback) {
return this.request({ return this.request({
method: 'POST', method: 'POST',
path: [],
headers: { }, headers: { },
body: utile.mixin({ Action: action }, query) body: utile.mixin({ Action: action }, query)
}, errback, function (body, res) { }, function (err, body, res) {
if (err) {
return callback(err);
}
var parser = new xml2js.Parser(); var parser = new xml2js.Parser();


parser.parseString(body, function (err, data) { parser.parseString(body, function (err, data) {
return err ? errback(err) : callback(data, res); return err
? callback(err)
: callback(err, data, res);
}); });
}); });
}; };
Expand All @@ -63,7 +68,11 @@ Client.prototype.bootstrapOptions = function (options, keys) {
return result; return result;
}; };


Client.prototype.url = function () { Client. prototype.getUrl = function (options) {
var args = Array.prototype.slice.call(arguments); options = options || {};
return ['https://' + this.serversUrl].concat(args).join('/');
return urlJoin('https://' + this.serversUrl,
(typeof options === 'string'
? options
: options.path));
}; };
20 changes: 12 additions & 8 deletions lib/pkgcloud/amazon/compute/client/keys.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ exports.listKeys = function (options, callback) {
var self = this; var self = this;
options = options || {}; options = options || {};


return this.query('DescribeKeyPairs', options, callback, function (body, res) { return this.query('DescribeKeyPairs', options, function (err, body, res) {
callback(null, self._toArray(body.keySet.item)); return err
? callback(err)
: callback(null, self._toArray(body.keySet.item));
}); });
}; };


Expand Down Expand Up @@ -69,9 +71,10 @@ exports.addKey = function (options, callback) {
KeyName: options.name, KeyName: options.name,
PublicKeyMaterial: utile.base64.encode(options.key) PublicKeyMaterial: utile.base64.encode(options.key)
}, },
callback, function (err, body, res) {
function (body, res) { return err
return callback(null, true); ? callback(err)
: callback(null, true);
} }
); );
}; };
Expand All @@ -87,9 +90,10 @@ exports.destroyKey = function (name, callback) {
return this.query( return this.query(
'DeleteKeyPair', 'DeleteKeyPair',
{ KeyName: name }, { KeyName: name },
callback, function (err, body, res) {
function (body, res) { return err
return callback(null, true); ? callback(err)
: callback(null, true);
} }
); );
}; };
96 changes: 52 additions & 44 deletions lib/pkgcloud/amazon/compute/client/servers.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,32 +44,31 @@ exports.getLimits = function getLimits(callback) {
exports._getDetails = function getDetails(details, callback) { exports._getDetails = function getDetails(details, callback) {
var self = this; var self = this;


async.parallel([ self.query(
function getName(callback) { 'DescribeInstanceAttribute', {
self.query( InstanceId: details.instanceId,
'DescribeInstanceAttribute', Attribute: 'userData'
{ InstanceId: details.instanceId, Attribute: 'userData' }, },
callback, function (err, body, res) {
function (body, res) { if (err) {
var meta = new Buffer( // disregard the errors, if any
body.userData.value || '', return callback(null, details);
'base64' }
).toString();

var meta = new Buffer(
try { body.userData.value || '',
meta = JSON.parse(meta); 'base64'
} catch (e) { ).toString();
meta = {};
} try {

meta = JSON.parse(meta);
details.name = meta.name; } catch (e) {
callback(null); meta = {};
} }
);
} details.name = meta.name;
], function () { callback(null, details);
callback(null, details); });
});
}; };


// //
Expand All @@ -81,7 +80,11 @@ exports._getDetails = function getDetails(details, callback) {
// //
exports.getServers = function getServers(callback) { exports.getServers = function getServers(callback) {
var self = this; var self = this;
return this.query('DescribeInstances', {}, callback, function (body, res) { return self.query('DescribeInstances', {}, function (err, body, res) {
if (err) {
return callback(err);
}

var servers = []; var servers = [];


if (!body || !body.reservationSet || !body.reservationSet.item) { if (!body || !body.reservationSet || !body.reservationSet.item) {
Expand All @@ -98,13 +101,11 @@ exports.getServers = function getServers(callback) {
servers, servers,
self._getDetails.bind(self), self._getDetails.bind(self),
function finish(err, servers) { function finish(err, servers) {
if (err) { return err
return callback(err); ? callback(err)
} : callback(null, servers.map(function (server) {

return new compute.Server(self, server);
callback(null, servers.map(function (server) { }), res);
return new compute.Server(self, server);
}), res);
} }
); );
}); });
Expand Down Expand Up @@ -180,9 +181,11 @@ exports.createServer = function createServer(options, callback) {
return this.query( return this.query(
'RunInstances', 'RunInstances',
createOptions, createOptions,
callback, function (err, body, res) {
function (body, res) {
var server; var server;
if (err) {
return callback(err);
}


self._toArray(body.instancesSet.item).forEach(function (instance) { self._toArray(body.instancesSet.item).forEach(function (instance) {
instance.meta = meta; instance.meta = meta;
Expand All @@ -207,9 +210,10 @@ exports.destroyServer = function destroyServer(server, callback) {
return this.query( return this.query(
'TerminateInstances', 'TerminateInstances',
{ InstanceId: serverId }, { InstanceId: serverId },
callback, function (err, body, res) {
function (body, res) { return err
callback(null, { ok: serverId }, res); ? callback && callback(err)
: callback && callback(null, { ok: serverId }, res);
} }
); );
}; };
Expand All @@ -236,10 +240,13 @@ exports.getServer = function getServer(server, callback) {
'Filter.1.Value.4': 64, // stopping 'Filter.1.Value.4': 64, // stopping
'Filter.1.Value.5': 80 // stopped 'Filter.1.Value.5': 80 // stopped
}, },
callback, function (err, body, res) {
function (body, res) {
var server; var server;


if (err) {
return callback(err);
}

self._toArray(body.reservationSet.item).forEach(function (reservation) { self._toArray(body.reservationSet.item).forEach(function (reservation) {
self._toArray(reservation.instancesSet.item).forEach(function (instance) { self._toArray(reservation.instancesSet.item).forEach(function (instance) {
server = instance; server = instance;
Expand Down Expand Up @@ -272,9 +279,10 @@ exports.rebootServer = function rebootServer(server, callback) {
return this.query( return this.query(
'RebootInstances', 'RebootInstances',
{ InstanceId: serverId }, { InstanceId: serverId },
callback, function (err, body, res) {
function (body, res) { return err
callback(null, { ok: serverId }, res); ? callback(err)
: callback(null, { ok: serverId }, res);
} }
); );
}; };
Expand Down
Loading

0 comments on commit d96f7b1

Please sign in to comment.