Skip to content

Commit

Permalink
compute: add promise support (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
callmehiphop authored and stephenplusplus committed Oct 17, 2016
1 parent f5a5e6c commit dfdf7ca
Show file tree
Hide file tree
Showing 36 changed files with 2,514 additions and 606 deletions.
17 changes: 17 additions & 0 deletions packages/google-cloud-compute/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ zone.createVM(name, { os: 'ubuntu' }, function(err, vm, operation) {
// Virtual machine created!
});
});

// Promises are also supported by omitting callbacks.
zone.createVM(name)
.then(function(data) {
var vm = data[0];
var operation = data[1];

return operation.promise();
})
.then(function() {
// Virtual machine created!
});

// It's also possible to integrate with third-party Promise libraries.
var gce = require('@google-cloud/compute')({
promise: require('bluebird')
});
```


Expand Down
2 changes: 1 addition & 1 deletion packages/google-cloud-compute/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"compute engine"
],
"dependencies": {
"@google-cloud/common": "^0.6.0",
"@google-cloud/common": "^0.7.0",
"arrify": "^1.0.0",
"async": "^2.0.1",
"create-error-class": "^3.0.2",
Expand Down
47 changes: 47 additions & 0 deletions packages/google-cloud-compute/src/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ function Address(region, name) {
* // `operation` is an Operation object that can be used to check the
* // of the request.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* address.create().then(function(data) {
* var address = data[0];
* var operation = data[1];
* var apiResponse = data[2];
* });
*/
create: true,

Expand All @@ -70,6 +79,13 @@ function Address(region, name) {
*
* @example
* address.exists(function(err, exists) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* address.exists().then(function(data) {
* var exists = data[0];
* });
*/
exists: true,

Expand All @@ -89,6 +105,14 @@ function Address(region, name) {
* address.get(function(err, address, apiResponse) {
* // `address` is an Address object.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* address.get().then(function(data) {
* var address = data[0];
* var apiResponse = data[1];
* });
*/
get: true,

Expand All @@ -106,6 +130,14 @@ function Address(region, name) {
*
* @example
* address.getMetadata(function(err, metadata, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* address.getMetadata().then(function(data) {
* var metadata = data[0];
* var apiResponse = data[1];
* });
*/
getMetadata: true
};
Expand Down Expand Up @@ -140,6 +172,14 @@ util.inherits(Address, common.ServiceObject);
* // `operation` is an Operation object that can be used to check the status
* // of the request.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* address.delete().then(function(data) {
* var operation = data[0];
* var apiResponse = data[1];
* });
*/
Address.prototype.delete = function(callback) {
callback = callback || common.util.noop;
Expand All @@ -162,4 +202,11 @@ Address.prototype.delete = function(callback) {
});
};

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(Address);

module.exports = Address;
63 changes: 61 additions & 2 deletions packages/google-cloud-compute/src/autoscaler.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,31 @@ function Autoscaler(zone, name) {
* @param {object} config - See {module:compute/zone#createAutoscaler}.
*
* @example
* autoscaler.create({
* var config = {
* coolDown: 30,
* cpu: 80,
* loadBalance: 40,
* maxReplicas: 5,
* minReplicas: 0,
* target: 'instance-group-manager-1'
* }, function(err, autoscaler, operation, apiResponse) {
* };
*
* var callback = function(err, autoscaler, operation, apiResponse) {
* // `autoscaler` is an Autoscaler object.
*
* // `operation` is an Operation object that can be used to check the
* // of the request.
* };
*
* autoscaler.create(config, callback);
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* autoscaler.create(config).then(function(data) {
* var autoscaler = data[0];
* var operation = data[1];
* var apiResponse = data[2];
* });
*/
create: true,
Expand All @@ -76,6 +89,13 @@ function Autoscaler(zone, name) {
*
* @example
* autoscaler.exists(function(err, exists) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* autoscaler.exists().then(function(data) {
* var exists = data[0];
* });
*/
exists: true,

Expand All @@ -95,6 +115,14 @@ function Autoscaler(zone, name) {
* autoscaler.get(function(err, autoscaler, apiResponse) {
* // `autoscaler` is an Autoscaler object.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* autoscaler.get().then(function(data) {
* var autoscaler = data[0];
* var apiResponse = data[1];
* });
*/
get: true,

Expand All @@ -112,6 +140,14 @@ function Autoscaler(zone, name) {
*
* @example
* autoscaler.getMetadata(function(err, metadata, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* autoscaler.getMetadata().then(function(data) {
* var metadata = data[0];
* var apiResponse = data[1];
* });
*/
getMetadata: true
};
Expand Down Expand Up @@ -146,6 +182,14 @@ util.inherits(Autoscaler, common.ServiceObject);
* // `operation` is an Operation object that can be used to check the status
* // of the request.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* autoscaler.delete().then(function(data) {
* var operation = data[0];
* var apiResponse = data[1];
* });
*/
Autoscaler.prototype.delete = function(callback) {
callback = callback || common.util.noop;
Expand Down Expand Up @@ -187,6 +231,14 @@ Autoscaler.prototype.delete = function(callback) {
* // `operation` is an Operation object that can be used to check the status
* // of the request.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* autoscaler.setMetadata(metadata).then(function(data) {
* var operation = data[0];
* var apiResponse = data[1];
* });
*/
Autoscaler.prototype.setMetadata = function(metadata, callback) {
var zone = this.zone;
Expand Down Expand Up @@ -217,4 +269,11 @@ Autoscaler.prototype.setMetadata = function(metadata, callback) {
});
};

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(Autoscaler);

module.exports = Autoscaler;
58 changes: 58 additions & 0 deletions packages/google-cloud-compute/src/disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ function Disk(zone, name) {
* // `operation` is an Operation object that can be used to check the
* // status of the request.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* disk.create(config).then(function(data) {
* var disk = data[0];
* var operation = data[1];
* var apiResponse = data[2];
* });
*/
create: true,

Expand All @@ -82,6 +91,13 @@ function Disk(zone, name) {
*
* @example
* disk.exists(function(err, exists) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* disk.exists().then(function(data) {
* var exists = data[0];
* });
*/
exists: true,

Expand All @@ -101,6 +117,14 @@ function Disk(zone, name) {
* disk.get(function(err, disk, apiResponse) {
* // `disk` is a Disk object.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* disk.get().then(function(data) {
* var disk = data[0];
* var apiResponse = data[1];
* });
*/
get: true,

Expand All @@ -118,6 +142,14 @@ function Disk(zone, name) {
*
* @example
* disk.getMetadata(function(err, metadata, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* disk.getMetadata().then(function(data) {
* var metadata = data[0];
* var apiResponse = data[1];
* });
*/
getMetadata: true
};
Expand Down Expand Up @@ -182,6 +214,15 @@ Disk.formatName_ = function(zone, name) {
* }
*
* disk.createSnapshot('new-snapshot-name', callback);
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* disk.createSnapshot('new-snapshot-name').then(function(data) {
* var snapshot = data[0];
* var operation = data[1];
* var apiResponse = data[2];
* });
*/
Disk.prototype.createSnapshot = function(name, options, callback) {
var self = this;
Expand Down Expand Up @@ -229,6 +270,14 @@ Disk.prototype.createSnapshot = function(name, options, callback) {
* // `operation` is an Operation object that can be used to check the status
* // of the request.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* disk.delete().then(function(data) {
* var operation = data[0];
* var apiResponse = data[1];
* });
*/
Disk.prototype.delete = function(callback) {
var zone = this.zone;
Expand Down Expand Up @@ -263,4 +312,13 @@ Disk.prototype.snapshot = function(name) {
return new Snapshot(this, name);
};

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(Disk, {
exclude: ['snapshot']
});

module.exports = Disk;
Loading

0 comments on commit dfdf7ca

Please sign in to comment.