Skip to content
Permalink
Browse files

Refactoring client.request to take options and callback

  • Loading branch information...
kenperkins committed Apr 23, 2013
1 parent c84c226 commit d96f7b113cfb53c56e147d1dc6a6c774687d1afe
Showing with 1,389 additions and 853 deletions.
  1. +20 −14 lib/pkgcloud/amazon/compute/client/groups.js
  2. +18 −9 lib/pkgcloud/amazon/compute/client/images.js
  3. +16 −7 lib/pkgcloud/amazon/compute/client/index.js
  4. +12 −8 lib/pkgcloud/amazon/compute/client/keys.js
  5. +52 −44 lib/pkgcloud/amazon/compute/client/servers.js
  6. +26 −13 lib/pkgcloud/amazon/storage/client/containers.js
  7. +83 −59 lib/pkgcloud/amazon/storage/client/files.js
  8. +22 −25 lib/pkgcloud/amazon/storage/client/index.js
  9. +21 −9 lib/pkgcloud/azure/compute/client/images.js
  10. +30 −29 lib/pkgcloud/azure/compute/client/index.js
  11. +12 −8 lib/pkgcloud/azure/compute/client/keys.js
  12. +2 −2 lib/pkgcloud/azure/compute/client/servers.js
  13. +17 −6 lib/pkgcloud/azure/database/client/databases.js
  14. +20 −29 lib/pkgcloud/azure/database/client/index.js
  15. +38 −7 lib/pkgcloud/azure/storage/client/containers.js
  16. +61 −29 lib/pkgcloud/azure/storage/client/files.js
  17. +20 −29 lib/pkgcloud/azure/storage/client/index.js
  18. +32 −10 lib/pkgcloud/azure/utils/azureApi.js
  19. +4 −4 lib/pkgcloud/azure/utils/sharedkey.js
  20. +11 −3 lib/pkgcloud/common/aws-signature.js
  21. +38 −72 lib/pkgcloud/core/base/client.js
  22. +2 −2 lib/pkgcloud/iriscouch/database/client/index.js
  23. +14 −6 lib/pkgcloud/joyent/compute/client/flavors.js
  24. +14 −6 lib/pkgcloud/joyent/compute/client/images.js
  25. +11 −7 lib/pkgcloud/joyent/compute/client/index.js
  26. +20 −8 lib/pkgcloud/joyent/compute/client/keys.js
  27. +39 −24 lib/pkgcloud/joyent/compute/client/servers.js
  28. +10 −5 lib/pkgcloud/mongohq/database/client/databases.js
  29. +8 −5 lib/pkgcloud/mongohq/database/client/index.js
  30. +18 −10 lib/pkgcloud/mongolab/database/client/accounts.js
  31. +20 −10 lib/pkgcloud/mongolab/database/client/databases.js
  32. +12 −7 lib/pkgcloud/mongolab/database/client/index.js
  33. +12 −17 lib/pkgcloud/openstack/client.js
  34. +14 −6 lib/pkgcloud/openstack/compute/client/flavors.js
  35. +22 −9 lib/pkgcloud/openstack/compute/client/images.js
  36. +21 −9 lib/pkgcloud/openstack/compute/client/index.js
  37. +20 −8 lib/pkgcloud/openstack/compute/client/keys.js
  38. +61 −27 lib/pkgcloud/openstack/compute/client/servers.js
  39. +7 −2 lib/pkgcloud/rackspace/cdn/client/index.js
  40. +10 −13 lib/pkgcloud/rackspace/client.js
  41. +14 −6 lib/pkgcloud/rackspace/compute/client/flavors.js
  42. +25 −10 lib/pkgcloud/rackspace/compute/client/images.js
  43. +8 −5 lib/pkgcloud/rackspace/compute/client/index.js
  44. +61 −29 lib/pkgcloud/rackspace/compute/client/servers.js
  45. +21 −7 lib/pkgcloud/rackspace/database/client/databases.js
  46. +14 −6 lib/pkgcloud/rackspace/database/client/flavors.js
  47. +8 −5 lib/pkgcloud/rackspace/database/client/index.js
  48. +42 −14 lib/pkgcloud/rackspace/database/client/instances.js
  49. +47 −15 lib/pkgcloud/rackspace/database/client/users.js
  50. +42 −17 lib/pkgcloud/rackspace/storage/client/containers.js
  51. +44 −24 lib/pkgcloud/rackspace/storage/client/files.js
  52. +10 −2 lib/pkgcloud/rackspace/storage/client/index.js
  53. +9 −3 lib/pkgcloud/rackspace/storage/file.js
  54. +4 −4 lib/pkgcloud/redistogo/database/client/index.js
  55. +8 −6 lib/pkgcloud/telefonica/compute/client.js
  56. +8 −8 test/common/base/client-test.js
  57. +1 −1 test/common/compute/base-test.js
  58. +35 −30 test/common/compute/server-test.js
  59. +2 −2 test/helpers/azureNock.js
  60. +10 −10 test/rackspace/databases/databases-test.js
  61. +63 −29 test/rackspace/databases/errors-test.js
  62. +8 −8 test/rackspace/databases/instances-test.js
  63. +15 −15 test/rackspace/databases/users-test.js
@@ -22,8 +22,10 @@ exports.listGroups = function (options, callback) {
var self = this;
options = options || {};

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

@@ -67,9 +69,10 @@ exports.addGroup = function (options, callback) {
GroupName: options.name,
GroupDescription: options.description
},
callback,
function (body, res) {
return callback(null, true);
function (err, body, res) {
return err
? callback(err)
: callback(null, true);
}
);
};
@@ -85,9 +88,10 @@ exports.destroyGroup = function (name, callback) {
return this.query(
'DeleteSecurityGroup',
{ GroupName: name },
callback,
function (body, res) {
return callback(null, true);
function (err, body, res) {
return err
? callback(err)
: callback(null, true);
}
);
};
@@ -119,9 +123,10 @@ exports.addRules = function (options, callback) {
return this.query(
'AuthorizeSecurityGroupIngress',
rules ,
callback,
function (body, res) {
return callback(null, true);
function (err, body, res) {
return err
? callback(err)
: callback(null, true);
}
);
};
@@ -153,9 +158,10 @@ exports.delRules = function (options, callback) {
return this.query(
'RevokeSecurityGroupIngress',
rules ,
callback,
function (body, res) {
return callback(null, true);
function (err, body, res) {
return err
? callback(err)
: callback(null, true);
}
);
};
@@ -34,10 +34,12 @@ exports.getImages = function getImages(options, callback) {
}
}

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

@@ -54,7 +56,10 @@ exports.getImage = function getImage(image, callback) {
imageId = image instanceof base.Image ? image.id : image,
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) {
return image ? new compute.Image(self, image) : null;
})[0];
@@ -86,8 +91,10 @@ exports.createImage = function createImage(options, callback) {
? options.server.id : options.server,
query = { InstanceId: serverId, Name: options.name };

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

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

self.query('DeleteSnapshot', query, callback, function (body, res) {
callback(null, { ok: query.snapshotId }, res);
self.query('DeleteSnapshot', query, function (err, body, res) {
return err
? callback(err)
: callback(null, { ok: query.snapshotId }, res);
});
});
};
@@ -7,6 +7,7 @@

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

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({
method: 'POST',
path: [],
headers: { },
body: utile.mixin({ Action: action }, query)
}, errback, function (body, res) {
}, function (err, body, res) {
if (err) {
return callback(err);
}
var parser = new xml2js.Parser();

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

Client.prototype.url = function () {
var args = Array.prototype.slice.call(arguments);
return ['https://' + this.serversUrl].concat(args).join('/');
Client. prototype.getUrl = function (options) {
options = options || {};

return urlJoin('https://' + this.serversUrl,
(typeof options === 'string'
? options
: options.path));
};
@@ -24,8 +24,10 @@ exports.listKeys = function (options, callback) {
var self = this;
options = options || {};

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

@@ -69,9 +71,10 @@ exports.addKey = function (options, callback) {
KeyName: options.name,
PublicKeyMaterial: utile.base64.encode(options.key)
},
callback,
function (body, res) {
return callback(null, true);
function (err, body, res) {
return err
? callback(err)
: callback(null, true);
}
);
};
@@ -87,9 +90,10 @@ exports.destroyKey = function (name, callback) {
return this.query(
'DeleteKeyPair',
{ KeyName: name },
callback,
function (body, res) {
return callback(null, true);
function (err, body, res) {
return err
? callback(err)
: callback(null, true);
}
);
};
@@ -44,32 +44,31 @@ exports.getLimits = function getLimits(callback) {
exports._getDetails = function getDetails(details, callback) {
var self = this;

async.parallel([
function getName(callback) {
self.query(
'DescribeInstanceAttribute',
{ InstanceId: details.instanceId, Attribute: 'userData' },
callback,
function (body, res) {
var meta = new Buffer(
body.userData.value || '',
'base64'
).toString();

try {
meta = JSON.parse(meta);
} catch (e) {
meta = {};
}

details.name = meta.name;
callback(null);
}
);
}
], function () {
callback(null, details);
});
self.query(
'DescribeInstanceAttribute', {
InstanceId: details.instanceId,
Attribute: 'userData'
},
function (err, body, res) {
if (err) {
// disregard the errors, if any
return callback(null, details);
}

var meta = new Buffer(
body.userData.value || '',
'base64'
).toString();

try {
meta = JSON.parse(meta);
} catch (e) {
meta = {};
}

details.name = meta.name;
callback(null, details);
});
};

//
@@ -81,7 +80,11 @@ exports._getDetails = function getDetails(details, callback) {
//
exports.getServers = function getServers(callback) {
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 = [];

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

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

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

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

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

0 comments on commit d96f7b1

Please sign in to comment.
You can’t perform that action at this time.